TESTEVERYTHING

Wednesday 5 May 2021

XPath Axis Family Tree Analogy

 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 context node, but does not go any deeper, like descendant:: does.
  • following:: will select all of the nodes that come after the context node and their descendant's, but that does not include the context node's descendants.
  • descendant:: will select all of the nodes along the child:: axis, as well as their children, and their children's children, etc..

Sometimes, a picture is worth a thousand words:




Thursday 4 March 2021

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]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\($?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$
Percentage from 0 to 100 with optional 2 decimals and optional % sign at the end (0, 0.00, 100.00, 100%, 99.99%):
^-?[0-9]{0,2}(\.[0-9]{1,2})?%?$|^-?(100)(\.[0]{1,2})?%?$

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, 24 hours format (HH:MM):
^(20|21|22|23|[01]\d|\d)((:[0-5]\d){1,2})$

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

CharacterWhat 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 character set. You can create range of characters using the hyphen character such as A-Z (A to Z). Note that in character sets, special characters (., *, +) do not have any special meaning.
\
  • Used to indicate that the next character should NOT be interpreted literally. For example, the character 'w' by itself will be interpreted as 'match the character w', but using '\w' signifies 'match an alpha-numeric character including underscore'.
  • Used to indicate that a metacharacter is to be interpreted literally. For example, the '.' metacharacter means 'match any single character but a new line', but if we would rather match a dot character instead, we would use '\.'.
\0Matches a NULL character.
\bMatches a word boundary. Boundaries are determined when a word character is NOT followed or NOT preceeded with another word character.
\BMatches a NON-word boundary. Boundaries are determined when two adjacent characters are word characters OR non-word characters.
\cXMatches a control character. X must be between A to Z inclusive.
\dMatches a digit character. Same as [0-9] or [0123456789].
\DMatches a NON-digit character. Same as [^0-9] or [^0123456789].
\fMatches a form feed.
\nMatches a line feed.
\rMatches a carriage return.
\sMatches a single white space character. This includes space, tab, form feed and line feed.
\SMatches anything OTHER than a single white space character. Anything other than space, tab, form feed and line feed.
\tMatches a tab.
\uhhhhMatches a character with the 4-digits hexadecimal code.
\vMatches a vertical tab.
\wMatches any alphanumeric character incuding underscore. Equivalent to [A-Za-z0-9_].
\WMatches anything OTHER than an alphanumeric character incuding underscore. Equivalent to [^A-Za-z0-9_].
\xA back reference to the substring matched by the x parenthetical expression. x is a positive integer.
\xhhMatches a character with the 2-digits hexadecimal code.
^
  • Matches the beginning of the input. If in multiline mode, it also matches after a line break character, hence every new line.
  • When used in a set pattern ([^abc]), it negates the set; match anything not enclosed in the brackets
x(?!y)Matches 'x' only if 'x' is NOT followed by 'y'. Also known as a negative lookahead.
x(?=y)Matches 'x' only if 'x' is followed by 'y'. Also known as a lookahead.
x|yMatches 'x' OR 'y'.
{n,m}Matches the preceding character at least n times and at most m times. n and m can be omitted if zero..
{n}Matches the preceding character exactly n times.

Monday 26 November 2018

Selenium 4 Release / Webdriver latest release

Selenium 4 is all set to release this Christmas. Simon Stewart (a founding member of Selenium) officially announced it at the recently held Selenium conference in Bangalore. Some major changes in the upcoming Selenium version 4 have been revealed. It’s time to get ahead of the curve and figure out what is going to be changed, added, and deprecated. In this article, we will take a look at a few important features and give some insight on updates you can expect for your automation framework.

W3C WebDriver Standardization

The Selenium 4 WebDriver will be completely W3C Standardized. The WebDriver API has grown to be relevant outside of Selenium. It is used in multiple tools for automation. For example, it’s used heavily in mobile testing through tools such as Appium and iOS Driver. The W3C standard will encourage compatibility across different software implementations of the WebDriver API.
Let’s take an example of how Selenium Grid communicates with Driver executables as of now –

Adopting New Protocol

Adopting-New-Protocol
A test in Selenium 3.x, on the local end (through JSON wire protocol) communicates with the browser at the End node. This requires encoding and decoding of the API. In Selenium 4, the test will directly communicate without any encoding and decoding of API requests (through the W3C Protocol), though Java bindings will be backward-compatible, but focusing more on the W3C Protocol. The JSON wire protocol will no longer be used.
There are multiple contributors in the WebDriver W3C specs. All of the work done is on GitHub.

Selenium 4 IDE TNG

Selenium-4-IDE-TNG
Selenium IDE support for Chrome is in the bucket. As we all know, Selenium IDE is a record and playback tool. It will now be available with much richer and more advanced features, such as
  • New plugin system – Any browser vendor will now be able to easily plug in to the new Selenium IDE. You can have your own locator strategy and plug in the Selenium IDE.
  • New CLI runner – It will be completely based on node.js, not the old HTML-based runner. It will have the following capabilities:
    • WebDriver Playback – The new Selenium IDE runner will be completely based on WebDriver.
    • Parallel execution — The new CLI runner will also support parallel test case execution and will provide useful information like time taken and number of test cases passed/failed.

Improved Selenium Grid

Those who have worked with Selenium Grid know how difficult it is to set up and configure. Selenium Grid supports test case execution on different browsers, operating systems, and machines, providing parallel execution capability.
There are two main elements in Selenium Grid: Hub and  Node.
Hub acts as a server, a central point to control all the test machines in a network. In Selenium Grid, there is only one hub, which allocates test execution to a particular node based on capability matches.
Node is a test machine where test cases actually run.
Selenium-Node-Container
For more details on Selenium Grid, follow the tutorial here.
The typical process to set up Selenium Grid has caused testers sometimes to face difficulty in connecting the node to the hub.
In Selenium 4, the Grid experience is going to be easy and smooth. There will not be any need to set up and start the hub and node separately. Once we start Selenium Server, the Grid will act as both hub and node.
Selenium 4 will come up with a more stable Selenium Grid in terms of removing all thread-safety bugs, and better support for Docker.

Better Selenium Grid UI

Selenium 4 will come with a more user-friendly UI for Grid, with relevant information about sessions running, capacity, etc.

Better Observability

“Passive observability is the ability to do descriptive tracing.”  – Simon Stewart
Observability, logging, and debugging are no longer confined to DevOps in recent times. As part of this release, request tracing and logging with hooks will be improved so as to provide automation engineers a hold on debugging.

Refreshed Documentation

Documentation plays a key role in any project’s success. The Selenium docs had not been updated since the Selenium 2.0 release. In the latest upgrade, Selenium's documentation is also going to be refreshed and detailed. You can access it here.
Here’s a video from Selenium Conference 2018, held at Bangalore recently:

Selenium 4 in a Nutshell

Upgrading to the latest version of Selenium should not require any code changes. Setting up nodes and hubs will become smooth and the entire grid experience is going to be streamlined. For automation engineers, the latest version should not be challenging and existing automation frameworks should work with minimal changes.

Which one is right ?

Translate







Tweet