Lecture 11 Selenium using Regex
Lecture 10
Copyright By PowCoder代写 加微信 powcoder
Selenium using Regex
https://regexr.com/
What is Regex?
Matching Text Patterns
Regular Expressions Patterns
Using Regular expressions through web driver in VS2019
Matching Text Patterns
Text Patterns are a type of parameter frequently required by Selenese commands. Patterns allow you to describe, via the use of special characters, what text is expected rather than having to specify that text exactly.
Link locators utilize patterns.
Other commands which require text patterns are : verifyTextPresent, verifyTitle,verifyAlert, assertConfirmation, verifyText, and verifyPrompt.
There are three types of patterns: globbing, regular expressions, and exact.
These expressions are not entirely working with the current versions. Most likely, the newer updates will include them in future.
Updated-May 2018
Regular Expression Patterns
Regular expression patterns are the most powerful of the patterns that most of the programming languages now support.
Selenese regular expression patterns offer the same wide array of special characters that exist in JavaScript.
Regular expressions are also supported by most high-level programming languages, many text editors, and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.
Example: regexp: [0-9]+ is a simple pattern that will match a decimal number of any length.
Regular Expression Patterns
PATTERN MATCH
. any single character
[ ] character class: any single character that appears inside the brackets
* quantifier: 0 or more of the preceding character (or group)
+ quantifier: 1 or more of the preceding character (or group)
? quantifier: 0 or 1 of the preceding character (or group)
{1,5} quantifier: 1 through 5 of the preceding character (or group)
| alternation: the character/group on the left or the character/group on the right
( ) grouping: often used with alternation and/or quantifier
Meta Meaning
Regular expressions are a powerful tool for searching, finding, and replacing specific text. Writing advanced regex is no easy task, and requires logic.
A meta-meaning is the meaning of a term or expression on a higher level of abstraction. Some metacharacters defined in regular expression patterns are: \ , + ( ) { } [ ] $ ^
Metacharacters in RegEx
the backslash \. 1. Escape, 2. Search non printable characters (\n, \t)
the caret ^. Not character
the dollar sign $. Matches “end of line”
the period or dot . Matches any character
the vertical bar or pipe symbol | Or operator
the question mark?. Matches 0 or 1 times
the asterisk or star *. Matches 0 or more times
the plus sign +. Match 1 or more times
the opening & closing parenthesis ( and ). Grouping Operators
the opening square bracket [ . Matches any character between [ and ]
the opening curly brace { . Enclose repetition operators
the comma ,. Separate repetition operators
http://faculty.nps.edu/sebuttre/home/Computing/regexp.html
https://sc1.checkpoint.com/documents/R77/CP_R77_IPS_WebAdminGuide/12885.htm
Are Regular expressions fast?
In general longer regex is better.
Good regular expressions are often longer because they make use of specific characters/character classes and have more structure.
And therefore, good regular expressions run faster as they predict their input more accurately.
Using Regular expressions for testing in VS2019
public class Tests
IWebDriver driver;
public void Setup()
driver = new ChromeDriver();
//driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(“https://www.massey.ac.nz/massey/learning/programme-course/course.cfm?course_code=158244&course_offering_id=1264588″);
public void Test1()
string pattern = @”^\d{3}\.\d{3}\s\w.*\(15 credits\)$”;
IWebElement input = driver.FindElement(By.TagName(“h1”));
Match m = Regex.Match(input.Text, pattern);
Assert.IsTrue(m.Success);
driver.Close();
driver.Quit();
//Assert.Pass();
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions?view=netframework-4.8
System.Text.RegularExpressions Namespace
Class Exercise
https://regex101.com/
https://regexone.com/
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com