selenium+c# Automation Script Development knowledge
Learning1:
SeleniumActions on the browser in
First, build a Web Object
Iwebdriver Driver = new Firefoxdriver ();
Opens the specified URL address
Driver. Navigate (). Gotourl (@ "http://12.99.102.196:9080/corporbank/logon_pro.html");
Close browser
Driver.quit ();
During the Internet banking browser compatibility test , there will be a dialog box after closing the browser, and this problem can be resolved as follows:
public void Logout ()
{
System.diagnostics.process[] myprocesses;
myprocesses = System.Diagnostics.Process.GetProcessesByName ("IEXPLORE");
foreach (System.Diagnostics.Process instance in myprocesses)
{
Instance. Kill ();
}
}
Execute JS script in 2:selenium
Need to cast driver to JS actuator type
((Ijavascriptexecutor) driver). Executescript ("JS filename");
Positioning page elements in 3:selenium
Driver. Findelement (By.id ("Cp1_btnmodify")). Click ();
By.classname (ClassName));
By.cssselector (selector);
By.id (ID);
By.linktext (LinkText);
By.name (Name);
By.partiallinktext (LinkText);
By.tagname (name);
By.xpath (XPathExpression);
3.1 Locating and manipulating based on element ID
Enter a string 500001 into the specified text box
Driver.findelement (By.id ("Amount")). SendKeys ("500001");
3.2 Positioning and operation according to element classname
Click the button classname for the specified value
Driver.findelement (By.classname ("Wotherday")). Click ();
3.3 Positioning and operation according to the linktext of the element
Driver.findelement (By.linktext ("Select Account"). Click ();
3.4 Positioning and manipulation based on the name of the element
Driver.findelement (By.name ("quality")). Perform ();
3.5 using Cssselector to locate and operate
String order = "#resultTable. Result_table tbody tr.bg1 td.center a";
Driver. Findelement (By.cssselector (Order)). Click ();
3.6 using XPath to position and manipulate elements
Locating elements Using multiple attributes
Driver.findelement (By.xpath ("//input[@id = ' Submit ' and @value = ' Next ']"). Click ();
Locating elements using absolute paths
String path = "/html/body/div[4]/div/div/div[2]/table/tbody/tr/td/a";
Driver.findelement (Path). Click (By.xpath);
Priority principles for each method use:
Priority use of Id,name,classname,link, second use of Cssselector (), and final use of XPath ();
Because the XPath () method has the least performance and efficiency.
Empty the default content in a text box in 4:selenium
Clear the text box clear ()
Driver.findelement (By.id ("Tranamttext")). Clear ();
Enter the specified string in the specified text box in the 5:selenium
Enter the specified string in the text box SendKeys ()
Driver.findelement (By.id ("Tranamttext")). SendKeys ("123456");
In 6:selenium
Movingcursor to the specified element
Moves the cursor over the specified element perform
Actions action=new actions (driver);
Action. Movetoelement (Find (By.xpath ("//input[@id = ' Submit ' and @value = ' OK '])). Perform ();
7:
SeleniumClick on the button/link
Click the button/link click ()
Driver.findelement (By.xpath ("//input[@id = ' Submit ' and @value = ' Next ']"). Click ();
8:selenium waiting for elements on the page to finish loading
Wait for page element to finish loading
Default wait of 100 seconds
webdriverwait wait = new webdriverwait (Driver, timespan.fromseconds (100));
Wait for an element with an ID property value of Submitbutton on the page to load complete
Wait. Until ((d) = {return Waitforobject (by.id ("Submitbutton");});
Simulating mouse shaking in 9:selenium
Simulate cursor shaking movebyoffset ()
Actions action = new actions (driver);
Action. Movebyoffset (2, 4);
Positioning of elements in an IFRAME in 10:selenium
5.1: Toggle focus to the ID fixed value on the IFRAME
After entering the page, the cursor default focus is in Defaultcontent, if you want to locate the IFRAME need to change focus
Driver. SwitchTo (). Defaultcontent ();
Toggle focus to Mainframe
Driver. SwitchTo (). Frame ("MainFrame");
It is important to note that if you want to switch focus to another IFRAME, you need to return to defaultcontent before switching focus to the specified IFRAME.
5.2 Switch focus to an IFRAME with an ID value of dynamic value
Sometimes the ID of the flyout on the page is a dynamic value, and it is time to get all of the records that match the IFRAME in the array, and then iterate over the array to switch focus to the target IFRAME.
The following methods:
Protected string Bizframeid = String. Empty;
Protected string Bizid = String. Empty;
Gets the ID value of the dynamic IFRAME
protected void Setiframeid ()
{
readonlycollection<iwebelement> els = driver. Findelements (By.tagname ("iframe"));
foreach (var e in driver. Findelements (By.tagname ("iframe")))
{
string S1 = E.getattribute ("id");
if (S1. IndexOf ("window") >= 0 && S1. IndexOf ("content") >= 0)
{
Bizframeid = E.getattribute ("id");
string[] ss = S1. Split (new char[] {' _ '});
Bizid = ss[1];
}
}
}
Close multiple child browser windows in 11:selenium
Get all the windowhandle, close all child windows
String oldwin = driver. Currentwindowhandle;
readonlycollection<string> windows = driver. Windowhandles;
foreach (Var win in Windows)
{
if (win! = Oldwin)
{
Driver. SwitchTo (). Window (Win). Close ();
}
}
Driver. SwitchTo (). Window (Oldwin);
Actions on the drop-down box in 12:selenium
Select the drop-down box
protected void Selectusage (String selectid, string text)
{
Iwebelement select = Find (By.id (Selectid));
ilist<iwebelement> alloptions = Select. Findelements (by.tagname ("option"));
foreach (iwebelement option in select. Findelements (by.tagname ("option")))
{
if (option. GetAttribute ("value"). Equals (text))
Option. Click ();
}
}
13:selenium confirm, alert, prompt operation
Only confirm and alert encountered in this browser compatibility test Project
The following example shows the code for confirm and alert, prompt similar
Operation of the Confirm
Ialert confirm = driver. SwitchTo (). Alert ();
Confirm. Accept ();
Operation of Alert
The same business in personal internet banking sometimes does not play against alert when it is necessary to determine if alert exists
To determine the alert prompt, wait 50 milliseconds by default
protected void alertaccept ()
{
Alertaccept (0.05);
}
Wait a few seconds, can be a decimal, the unit is seconds
protected void Alertaccept (double waitsesonds)
{
Double Nsleepmillon = waitsesonds * 1000;
int k=0;
int split=50;
Ialert alert = null;
Do
{
k++;
Thread.Sleep (split);
Alert = driver. SwitchTo (). Alert ();
} while (k * Split <= Nsleepmillon | | alert==null);
if (alert! = NULL)
{
Alert. Accept ();
}
}
Features of the 14:selenium Webdriver
Webdriver in-band function
Screenshot screenshotfile = ((itakesscreenshot) driver). Getscreenshot ();
Screenshotfile.saveasfile ("test", imageformat.jpeg);
This article is reproduced from http://www.51testing.com/html/66/15050766-865363.html
selenium+c# Automated Scripting Development Learning