---
title: "Selenium Commands For Automation Testing"
author: "wnzz95391511"
date: "2009-10-18T15:08:42Z"
tags: ["ASP", "Flash", "Flex", "JavaScript", "脚本"]
slug: "selenium-commands-for-automation-testing"
draft: false
lastmod: "2009-10-18T15:09:36Z"
id: 493557
url: "https://perfectlife.iteye.com/blog/493557"
---

**Selenium Commands For Automation Testing**

 

**一、Element Locators(定位器)**

- **id=id**

**id locator (Select the element with the specified @id attribute in HTML).**

- **name=name**

**name locator(Select the first element of the same @name attribute elements in HTML).**

- **identifier=id**

**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=javascriptExpression

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=xpathExpression

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=textPattern

link locator (Select the link (anchor) element which contains text matching the specified pattern).

 

**Example:**

        link=The link text

- **Default**

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**

 

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**

 

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**

 

type(inputLocator, value)

\- Imitate the process of user typing, type value to inputLocator.

\- also is fit for radio or checkbox.

 

- **contextMenu**

 

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**

 

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

 

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. 

 

 

- Don't using id locator unless the id of this element is stable or you can't find other ways.

 

**Example:** 

selenium.click("xpath=//div\[@id='test']");

(If the id is not stable, your code is not safe.)

 

- Common useful Path Expression

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.

 

 

- **Useful function of location**

 

--**text()**: Return text value of the element. Like &lt;span&gt;ddd&lt;/span&gt;, 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.)

 

- **Some XPath Instance**

 

--**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](http://www.w3school.com.cn/xpath/xpath_syntax.asp_)**

**--SpringSide:                     [http://wiki.springside.org.cn/display/springside/Selenium+Reference](http://wiki.springside.org.cn/display/springside/Selenium+Reference_)**

**--Selenium Official site:   [http://seleniumhq.org/projects/remote-control/](http://seleniumhq.org/projects/remote-control/_)**