Posts

How to handle new tab using playwright

  Scenario: You click a link or button that opens a new tab, and you want to switch to that tab and perform actions.   const [newPage] = await Promise.all([     context.waitForEvent('page'),         // Waits for a new tab to be opened     page.click('a[target="_blank"]'),     // Simulates clicking the link that opens a new tab   ]);   // Wait for the new tab to load   await newPage.waitForLoadState();   // Do something in the new tab   console.log('New tab title:', await newPage.title());   // Example: take screenshot of the new tab   await newPage.screenshot({ path: 'new-tab.png' });   await browser.close(); })(); Key Concepts: context.waitForEvent('page') : Waits for a new tab or popup. The Promise.all() ensures that the event and click happen together. newPage.waitForLoadState() : Ensures the new tab is fully loaded before interaction.

XPath Axis Family Tree Analogy

Image
  The major XPath axes follow family tree terminology: self::  is you. Downward: child::  are your immediate children. descendant::  are your children, and their children, recursively. descendant-or-self::  (aka  // ): are you and your descendants. Upward: parent::  is your mother  or  father. ancestor::  are your parent, and your parent's parent, recursively. ancestor-or-self::  are you and your ancestors. Sideways (consider elements earlier in the document to be younger): previous-sibling::  are your younger siblings, in age order. following-sibling::  are your older siblings, in age order. previous::  are your younger siblings and their descendants, in age order. following::  are your older siblings and their descendants, in age order. --------------------------------------------------------------------------------------------------------------------- child::  will select the immediate descendants of the ...

How to validate credit cards using a regular expression?

  Again, you should rely on other methods since the regular expressions here will only validate the format. Make use of the   Luhn algorithm  to properly validate a card. VISA: ^4[0-9]{12}(?:[0-9]{3})?$ MasterCard: ^5[1-5][0-9]{14}$ American Express: ^3[47][0-9]{13}$ Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ JCB: ^(?:2131|1800|35\d{3})\d{11}$

How to validate NUMBERS with a regular expression?

  It depends. What type of number? What precision? What length? What do you want as a decimal separator? Etc. The following examples should help you want with the most common tasks. Positive integers of undefined length: ^\d+$ Positive integers of maximum length (10 in our example): ^\d{1,10}$ Positive integers of fixed length (5 in our example): ^\d{5}$ Negative integers of undefined length: ^-\d+$ Negative integers of maximum length (10 in our example): ^-\d{1,10}$ Negative integers of fixed length (5 in our example): ^-\d{5}$ Integers of undefined length: ^-?\d+$ Integers of maximum length (10 in our example): ^-?\d{1,10}$ Integers of fixed length (5 in our example): ^-?\d{5}$ Numbers of undefined length with or without decimals (1234.1234): ^-?\d*\.{0,1}\d+$ Numbers with 2 decimals (.00): ^-?\d*\.\d{2}$ Currency numbers with optional dollar sign and thousand separators and optional 2 decimals ($1,000,00.00, 10000.12, 0.00): ^$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{...

How to validate a DATE with a regular expression?

  Never use a regular expression to validate a date. The regular expression is only useful to validate the format of the date as entered by a user. For the actual date validity, you should rely on another language. The following expressions will validate the number of days in a month but will NOT handle leap year validation; hence february can have 29 days every year, but not more. ISO date format (yyyy-mm-dd): ^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$ ISO date format (yyyy-mm-dd) with separators '-' or '/' or '.' or ' '. Forces usage of same separator accross date. ^[0-9]{4}([- /.])(((0[13578]|(10|12))\1(0[1-9]|[1-2][0-9]|3[0-1]))|(02\1(0[1-9]|[1-2][0-9]))|((0[469]|11)\1(0[1-9]|[1-2][0-9]|30)))$ United States date format (mm/dd/yyyy) ^(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02/(0[1-9]|[1-2][0-9]))|((0[469]|11)/(0[1-9]|[1-2][0-9]|30)))/[0-9]{4}$ Hours and minutes, ...

How can I emulate DOTALL in JavaScript?

  DOTALL is a flag in most recent regex libraries that makes the . metacharacter match anything INCLUDING line breaks. JavaScript by default does not support this since the . metacharacter matches anything BUT line breaks. To emulate this behavior, simply replaces all . metacharacters by [\S\s]. This means match anything that is a single white space character OR anything that is not a white space character! [\S\s]

Regular Expression - Documentation

  Metacharacters Character What does it do? $ Matches the  end of the input . If in multiline mode, it also matches  before a line break character , hence every end of line. (?:x) Matches 'x' but  does NOT remember the match . Also known as NON-capturing parenthesis. (x) Matches 'x' and  remembers the match . Also known as capturing parenthesis. * Matches the preceding character  0 or more times . + Matches the preceding character  1 or more times . . Matches  any single character except the newline character . ? Matches the preceding character  0 or 1 time . When used after the quantifiers *, +, ? or {},  makes the quantifier non-greedy ; it will match the minimum number of times as opposed to matching the maximum number of times. [\b] Matches a  backspace . [^abc] Matches  anything NOT enclosed by the brackets . Also known as a negative character set. [abc] Matches  any of the enclosed characters . Also known as a charac...