In the previous article, a simple simulation of a Baidu search and the process, in the search process, we need to locate the search box, search button. This article mainly introduces the specific positioning method.
Our script is as follows:
Settings * * * Library selenium2library * * * Test Cases * * * firefox compatibility Open Browser https://www.baidu.com/ ff Input Text id=kw leettest Click button id= su Sleep 2 Capture Page screenshot ff.png Close Browser
Here we use the Selenium2library for browser control. Next, look for Selenium2library's official documentation to see how the page element is positioned.
Search for Selenium2library on GitHub with the project address
Https://github.com/rtomac/robotframework-selenium2library
View its wiki to find its official documentation
Http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html
One of the locating or specifying elements chapters describes several methods for locating elements:
With a script
Click Element my_element
For example, the positioning method can be used in the following ways:
Strategy |
Example |
Description |
Describe |
Identifier |
Click Element | Identifier=my_element |
Matches by @id or @name attribute |
Match ID or Name property |
Id |
Click Element | Id=my_element |
Matches by @id attribute |
Match ID Property |
Name |
Click Element | Name=my_element |
Matches by @name attribute |
Match the Name property |
Xpath |
Click Element | xpath=//div[@id = ' my_element ') |
Matches with arbitrary XPath expression |
Match an Xpath expression |
Dom |
Click Element | DOM=DOCUMENT.IMAGES[56] |
Matches with arbitrary DOM Express |
Match DOM Expression |
Link |
Click Element | Link=my Link |
Matches anchor elements by their link text |
Link text to match anchor points |
Partial link |
Click Element | Partial link=y Lin |
Matches anchor elements by their partial link text |
Partially linked text that matches an anchor point |
Css |
Click Element | Css=div.my_class |
Matches by CSS Selector |
Matching CSS |
Jquery |
Click Element | Jquery=div.my_class |
Matches by Jquery/sizzle Selector |
Match Jquery/sizzle Selector |
Sizzle |
Click Element | Sizzle=div.my_class |
Matches by Jquery/sizzle Selector |
Match Jquery/sizzle Selector |
Tag |
Click Element | Tag=div |
Matches by HTML tag name |
The HTML tag name of the matching element |
default* |
Click Link | Default=page?a=b |
Matches key attributes with value after first ' = ' |
Match the key attribute after the first = |
Specific to our test script
Inputtext id=kw leettest
Here we use the ID to locate, found the ID of kw text box, and entered the keyword leettest.
Next, try using other methods for element positioning. First, open the homepage of Baidu from the browser, use the debugging tool to see the text box element of the input search keyword, its HTML code is as follows:
<class= "S_ipt" name= "WD" id= "kw" maxlength= "AutoComplete" = " Off " type=" text ">
Depending on the positioning methods given in the document, we can change our test script as follows
1.identifier:identifier will match the ID and name, regardless of the ID or name, and will be matched to the selected element as long as it meets the keyword requirements.
Here we can use its ID as the keyword
Input Text identifier=kw leettest
or name as the keyword
Input Text identifier=wd leettest
2.name,name only matches the Name attribute of the element
Input Text name=wd leettest
3.Xpath
XPath is the XML Path language, which is a language used to determine the location of a part of an XML document. XPath is an XML-based tree structure that provides the ability to find nodes in a data structure tree. XPath provides two ways to locate absolute and relative paths.
The absolute path is used first for positioning. The absolute path starts from the first part of the HTML document of the page, and is positioned inward by layer.
Inputtext Xpath =/html/body/div[3]/div[1]/div[1]/div[1]/div[1]/form/span[1]/input leettest
However, in actual use, Baidu's home page will be due to different browser or environment changes in style, the corresponding XPath absolute address will change, this time you need to use the XPath relative path. Relative path of XPath
1) can be anchored to the target location by Id/name and other element attributes
Inputtext Xpath =/input[@id = ' kw '] leettest
2) You can also first determine a relatively fixed element (here we take form as an example), and then start with the element, looking for the relative position of the element.
Inputtext XPath =/form[@id = ' form ']/span[1]/input leettest
4.Css
Using a CSS matching strategy, you can navigate to the element by CSS matching policy when the page element is rendered by the browser. Supported in CSS syntax. # > and other keywords.
Inputtext css=. S_ipt leettestinputtext css=#kw leettestinputtext css=[ NAME=WD] leettest
A detailed description of how XPath and CSS are positioned is then described in detail, and here are just a few of the simplest examples.
5.Jquery and Sizzle
Sizzle is the new Dom selector engine written by jquery author John Resig, the fastest-known in the industry. jquery starts with 1.3 and uses the new selector –sizzle. More efficient than previous versions of jquery and other selectors you can see today.
You can refer to the following articles:
Http://www.cnblogs.com/xesam/archive/2012/02/15/2352466.html
In addition to the above several methods, in the official documents in the DOM, link, partial link, tag, default and several other positioning schemes, compared to the above introduction of the 5 kinds of programs, not commonly used, here do not do too much description.
The above is the robot framework in the actual use of the elements used in the location method, based on these methods, we can achieve the automatic positioning of a variety of format elements, so that we further deploy automated testing.
After we have implemented the positioning of the elements, what we need to do is to create variable tables for variables such as search keywords in the script, to automate the testing in batches, and in the next article we will describe how to use resource to organize test data.
Robot Framework Tutorial (2)-page element positioning