您的位置:首页 > 其它

Selenium常用的命令(6)

2013-11-19 10:47 295 查看
下面是我觉得比较常用的命令:

1.open(url)

命令作用:打开网页,可以有相对和绝对路径两种形式

参数说明:url是要打开的网页的网址

例:要打开bing的首页,BaseURL为http://cn.bing.com/

commandtargetvalue

open/
2.type(inputLocator,value)

命令作用:在一个文本框中输入值

参数说明:inputLocator为UI元素的定位,value是要输入的值

例:在bing的首页中输入“selenium”

commandtargetvalue
typesb_form_qselenium
typeAndWaitsb_form_qselenium
3.click(elementLocator)

命令作用:点击连接,按钮,复选和单选框

参数说明:elementLocator为UI元素的定位

例:点击bing的首页的“搜索“,BaseURL为http://cn.bing.com/

commandtargetvalue

clicksb_form_go
4.goBack()
命令作用:模拟网页的后退功能
参数说明:
例:

commandtargetvalue

goBack()
5.close()
命令作用:模拟浏览器的关闭功能
参数说明:
例:

commandtargetvalue

close()
6.select(dropDownLocator, optionSpecifier)
命令作用:选择一个下拉菜单选项
参数说明:dropDownLocator为UI元素的定位,optionSpecifier是选择项
例:打开开心网,选择“年份“为2013年,BaseURL 为http://www.kaixin001.com/

commandtargetvalue
selectyearindex=1
selectyearvalue=2013
selectyearlabel=2013
7.check(locator)
命令作用:勾选或者是取消checkbox或者是radio
参数说明:locator为UI元素的定位
例:打开开心网,选择“我现在“为”其他”,BaseURL 为http://www.kaixin001.com/

commandtargetvalue
checkother
8.store(expression, variableName)

命令作用:保存一个值到一个变量里
参数说明:expression 为要保存的值,variableName为变量
例:

Command

Target

Value

store

123

id

在其他命令的value中访问这个变量的值,方法是${id},例如

Command

Target

Value

type

regemailinput

${id}

注意:在网上复制的或者是用中文输入$.{title},得到的可能不是变量的值,而是变量本身的字符串。如图:



所以一定要用英文输入${id}。 该值还可以由自其他变量组合而成或通过JavaScript表达式赋值给变量

Command

Target

Value

store

123

id

store

456

id2

store

${id}${id2}

id3

store

javascript{Math.round(Math.PI*100)/100}

id4

id3为“123456”,id4为3.14

9. storeValue(locator, variableName)
命令作用:locator为表单元素,也就是input元素的值,也就是属性value的值,其中checkbox/radio元素为“off/on“,select为空
参数说明:locatorUI元素定位,variableName为变量
例:text
<input style="font-family: Arial; color:rgb(204, 204, 204); font-weight: normal;" name="loginemail"value="" type="text" size="18"class="it1" onfocus="this.className='it2';" onblur="this.className='it1';"data-result="">

Command

Target

Value

storeValue

loginemail

email

echo

${email}

执行结果:
[info] echo: 邮箱/手机号/其他用户名
其他例子:



执行结果:
l [info] Executing:|open | / | |
l [info] Executing:|storeValue | loginemail | email |
l [info] Executing:|echo | ${email} | |
l [info] echo: 邮箱/手机号/其他用户名
l [info] Executing:|storeValue | month | value_select |
l [info] Executing:|echo | ${value_select} | |
l [info] echo:
l [info] Executing:|storeValue | man | man |
l [info] Executing:|echo | ${man} | |
l [info] echo: off
l [info] Executing:|storeValue | women | women |
l [info] Executing:|echo | ${women} | |
l [info] echo: on
l [info] Executing:|storeValue | status | status |
l [info] Executing:|echo | ${status} | |
l [info] echo: off
l [info] Executing:|storeValue | rcode2 | hidden |
l [info] Executing:|echo | ${hidden} | |
l [info] echo:
l [info] Executing:|storeValue | password | password |
l [info] Executing:|echo | ${password} | |
l [info] echo:
l [info] Executing:|storeValue | btn_dl | submit |
l [info] Executing:|echo | ${submit} | |
l [info] echo: 登 录
l [info] Executing:|storeValue | //a[@title='注册新账号'] | a |
l [error] Thiselement has no value; is it really a form field?

10. storeText(locator,variableName)
命令作用:保存页面中元素的text值
参数说明:locator元素的定位,variableName为变量
例:在开心网上,BaseURL 为http://www.kaixin001.com/, 要输出a的text,如下
<ahref="http://reg.kaixin001.com" title="注册新账号" class="newFormBtn_blue">注册新账号</a>

Command

Target

Value

storeText

//a[@title='注册新账号']

a_text

echo

${ a_text }

执行结果:
[info] echo: 注册新账号
<input type="submit" id="btn_dl" value="登 录" title="登录"class="newFormBtn_red2" onclick="">

Command

Target

Value

storeText

btn_dl

button_text

echo

${ button_text }

执行结果:
[info] echo:

11. waitForValue(inputLocator,value)
命令作用:等待某input(如hiddeninput)被赋予某值
参数说明:inputLocator元素的定位,value为预期的值

12. waitForCondition(inputLocator,value)
还有很多的命令,边学习边总结吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: