Web-Side Automation element location method
is the version iteration cycle getting shorter? More and more demand? Is the test work getting bigger? How to guarantee the quality? How to guarantee the online cycle?
With the rapid development of the Internet, more and more testers meet the above mentioned problems. How to achieve both improve testing efficiency and coverage, but also save testing costs, is the goal of every enterprise. In order to solve the above problems, more and more companies began to join the automated testing. Here are some commonly used positioning methods to introduce
First, the common method
1) Positioning by class attribute
Driver.findelement (By.classname ("spread")). SendKeys ("Hello");
2) location by id attribute
Driver.findelement (By.id ("username")). SendKeys ("Hello");
3) Location by Name property
Driver.findelement (By.name ("username")). SendKeys ("Hello");
4) Navigate through the Link property
Driver.findelement (By.linktext ("The Thief King"). Click ();
5) Positioning via Partiallink
Driver.findelement (By.partiallinktext ("Thief")). Click ();
6) positioning via label TabName
Driver.findelement (By.tagname ("a")). Click ();
7) positioning via CSS
Driver.findelement (By.cssselector ("input[type= ' button ')"). Click ();
8) positioning via xapth
Driver.findelement (By.xpath ("/html/body/div[1]/input[2]"). Click ();
Position by XPath Absolute path
Driver.findelement (By.xpath ("//input[@value = ' query ']"). Click ();
Position by relative path
Driver.findelement (By.xpath ("//a[text () = ' Baidu a bit ')"). Click ();
Relative path mode, element is clickable link text
The above list is the common elements in the same Web page location method. Then encountered some interface jump, there is a drop-down box, the interface has a number of frames, non-web popup window, the common method can not locate the situation how to do? Here is a description of how to solve these positioning methods.
Second, higher-order methods
1) page jump, the cursor remains in the original interface, the new interface element is not located.
Mode one: Do not close the original window
String old = Driver.getwindowhandle ();
Driver.getwindowhandle ()://Get old window handle
For (String i:driver.getwindowhandles ()) {
Driver.getwindowhandles ()://Get Handle of all windows
if (!i.equals (old)) {
Driver.switchto (). window (i);
break;} The handle is judged, and if it is a new window, toggle the cursor
}
Mode two: Close the original window: recommended use
String old = Driver.getwindowhandle ();
Driver.getwindowhandle ()://Get old window handle
For (String i:driver.getwindowhandles ()) {
Driver.getwindowhandles ()://Get Handle of all windows
if (i.equals (old)) {
Driver.close ();
} else {
Driver.switchto (). window (i);
Break
}
}
2) The contents of the dropdown box are not located
Select s = new Select (Driver.findelement (By.name ("fruit"));//Position drop-down box
S.selectbyindex (1);//Get the value from the drop-down list by index/subscript
S.selectbyvalue ("Lizhi");//Get the value from the drop-down list by value
S.selectbyvisibletext ("Kiwi"); Gets the value from the drop-down list by the visible text in the interface
3) same page, with multiple frame,frame elements not positioned
Distance from the interface with 3 parallel frame
Step 1: Switch frame, go to the left page, and then enter the content
Driver.switchto (). FRAME ("Leftframe");
Driver.findelement (By.id ("left")). SendKeys ("Fan Bingbing");
Step 2: Return to the default state
Driver.switchto (). Defaultcontent ();
Step 3: Switch frame into the middle page, then enter the content
Driver.switchto (). FRAME ("Middleframe");
Driver.findelement (By.id ("Middle")). SendKeys ("Liu Yifei");
4) Non-web popup cannot be located after clicking a button
Driver.findelement (By.id ("Btnconfirm")). Click ();//Position button and click action
Driver.switchto (). alert (). Accept ();//Click OK, accept: means accepting or agreeing
Driver.switchto (). alert (). dismiss ();//Click Cancel, dismiss means cancel
The above is to introduce the Web page in the use of Java language elements of the positioning method, the Python language is the same way, but the syntax is not the same.
Web-Side Automation element location method