Selenium Commands For Automation Testing
一、Element Locators(定位器)
id locator (Select the element with the specified @id attribute in HTML).
name locator(Select the first element of the same @name attribute elements in HTML).
identifier locator (First,select the element with the specified @id attribute in HTML.IF it is not extant,then Select the first element of the same @name attribute elements in HTML).
dom locator (Using javascriptExpression locate the element of HTML. Note:must start by 'document').
Example:
dom=document.forms['myForm'].myDropdown
dom=document.images[56]
xpath locator (Locate the element in HTML by XPath. Note:must start by '//')
Example:
xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[4]/td[2]
link locator (Select the link (anchor) element which contains text matching the specified pattern).
Example:
link=The link text
Without an explicit locator prefix, Selenium uses the following default strategies:
dom, for locators starting with "document."
xpath, for locators starting with "//"
identifier, otherwise
二、Actions(模仿用户操作)
Imitate user's actions: Action have two format: 'action' and 'actionAndWait', action will execute at once,but 'actionAndWait' will wait for a setting time to get the response of this action. 'open' can deal latency time itself.
click(elementLocator)
- Click link,button,radio or checkbox button.
- If needing latency time for new page response ,please use "waitForPageToLoad"
Example:
selenium.click("xpath=//div[@id='dd']");
open(url)
- Open page using URL in Borwer, the action could receive two formats of relative path and absolute path.
Example:
selenium.open("http://www.google.cn");
selenium.open("/login.jsp");
type(inputLocator, value)
- Imitate the process of user typing, type value to inputLocator.
- also is fit for radio or checkbox.
contextMenu(locator)
- Right-click link, button, radio or checkbox button.
三、Execute JavaScript(执行JS脚本)
Launch page, then using selenium Server execute JavaScripte code for some useful function.
getEval(String script)
- script: some JavaScript code string.
- getEval: execute script and get the return value.
Example:
selenium.getEval("return document.getElementById('test').attribute('value');");
四、Execute Flex Script(执行Flex脚本)
Launch page, then using selenium Server execute JavaScripte code to call Flex function APIs.
getEval(String script)
--Precondition: The flash object is in current executing page and this flash has function APIs.
--First: Using javascript DOM find the locator of flash object id.
- Second: Exectue the function of Flex function APIs of this object locator.
- Third: GetEval function execute script and get the return value.
Example:
String text = "selenium.browserbot.getCurrentWindow().document.getElementById('testFlash').addNode('nodename','parentNodeName','true');";
selenium.getEval(text);
五、Using XPath(如何使用XPath定位元素)
Show some useful method to locator element using XPath.
Example:
selenium.click("xpath=//div[@id='test']");
(If the id is not stable, your code is not safe.)
nodename |
Locate all children nodes of this node |
/ |
Locate all node for root node |
// |
Locate node from current select node, don't care about the location of selected node. |
. |
Choice current selected node |
.. |
Choice parent node of current selected node |
@ |
Locate node using attribute |
Example:
booknode |
Locate all children nodes of the node named 'booknode' |
/booknode |
Locate root node named 'booknode' |
//book |
Locate all node named 'book', don't care about the location of selected node. |
//book[@value='1'] |
Locate node named 'book' and attribute of value equal 1. |
--text(): Return text value of the element. Like <span>ddd</span>, return ddd.
--contains(String A, String B): If A contains B return true, else return false.
--last(): Return the last node of choice nodes.
Example:
selenium.click("xpath=//div[contains(text(),'Released Projects')]");
(Click the div which text contains 'Released Projects' string.)
--First: xpath=//span[contains(text(),'Released Projects')]/../..//img[@class='dijitTreeExpando dijitTreeExpandoClosed']
(Locate an image tag which is a sub node of the parent node of the parent node of span node which text contains 'Released Projects'.)
--Second: xpath=//label[@title='level1FolderName']/../../../..//label[@title='level2FolderName']/../../../img
--Third: xpath=//span[@class='dijitDialogTitle' and text()='Users/Groups Filter']
六、Reference(参考)
--W3School: http://www.w3school.com.cn/xpath/xpath_syntax.asp
--SpringSide: http://wiki.springside.org.cn/display/springside/Selenium+Reference
--Selenium Official site: http://seleniumhq.org/projects/remote-control/