使用 SeleniumLibrary 库做准备
- 安装 Selenium 库:pip install selenium
- 安装 SeleniumLibrary 库 :pip install --upgrade robotframework - seleniumLibrary
- 设置浏览器驱动
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
ccase01
Open Browser http://www.baidu.com chrome
Input Text id=kw hello
Sleep 3
Close Browser
SeleniumLibrary库 - 定位元素
- SeleniumLibrary中需要与网页上的元素进行交互的所有关键字,都带有一个通常称为 locator 的参数,该参数指定如何查找该元素
- 使用以下描述的定位符语法,将定位符作为字符串给出,也可使用WebElements
Locator 语法
- SeleniumLibrary 支持根据不同策略查找元素,例如元素id,Xpath表达式或CSS选择器
- 可使用前缀明确指定策略,也可隐式指定策略
默认定位器策略
- 默认情况下,定位器被认为使用关键字特定的默认定位器策略
- 所有关键字支持基于 id 和 name 属性查找元素,某些关键字支持在上下文中有意义的其他属性或其他值。例如,单击链接支持 href 属性,链接文本以及常规 id 和 name 的添加
例如
操作 | 例子 | 描述 |
---|---|---|
Click Element | example | #匹配 id 或者 name |
Click Link | example | #匹配链接文本和 href |
Click Button | example | #匹配 id , name 或者 value |
- 如果定位器意外地以识别为显式定位器策略或隐式 Xpath 策略前缀开头,可使用显式 default 前缀来启用默认策略
例如
操作 | 编码页 | 描述 |
---|---|---|
Click Element | name:foo | #查找名称为foo的元素 |
Click Element | default:name:foo | #使用默认策略使用值名称 name:foo |
Click Element | //foo | #使用查找元素Xpath //foo |
Click Element | default://foo | #将默认策略与value结合使用 //foo |
明确定位器策略
- 使用语法strategy : value 或 strategy = value 使用前缀指定显式定位器策略。首选前一种语法,因为后者与robot framework命名参数语法相同,可能导致问题。分隔符周围空格将被忽略,因此 id : foo, id : foo 是等效的
例如
策略 | 基于什么匹配 | 实例 |
---|---|---|
id | Element id | id : example |
name | name attribute | name : example |
identifier | Either id or name | identifier : example |
class | Element class | class : example |
tag | Tag name | tag : div |
xpath | Xpath expression | xpath://div[@id=“example”] |
css | CSS selector | css : div#example |
dom | DOM expression | dom:document.images[5] |
link | Exact text a link has | link : the example |
partial link | Partial link text | partial link:he ex |
sizzle | Sizzle selector deprecated | sizzle : div . example |
jquery | jQuery expression | jquery : div.example |
default | Keyword specific default | default : example |
- 默认策略如何工作更多信息,请参见默认定位器策略部分。仅当定位器值本身偶然与某些显式策略匹配时,才需要使用显式默认前缀
- 不同定位器策略有不同优缺点,建议尽可能使用 id (显式地像 id:foo 一样)或使用默认定位器策略 (像foo一样),语法简单,对于浏览器来说,按 id 定位元素很快
- 元素没有 ID 或 ID不稳定,需要使用其他解决方案
- 元素具有唯一标签名称或类,使用标签,类或css策略(例如 tag:h1, class:example 或 css:h1. example)是一个简单的解决方案
- 复杂情况下,使用Xpath表达式是最好的方法,优点是非常强大,缺点是可能变得复杂
实例
操作 | 元素 | 描述 |
---|---|---|
Click Element | id:foo | #具有 id ‘foo’ 元素 |
Click Element | css:div#foo h1 | #具有id‘foo’的div下的h1元素 |
Click Element | xpath://div[@id=“foo”]//h1 | #与上面使用的XPath相同,而不是使用CSS |
Click Element | xpath://*[contains(text(),“example”)] | #包含文本“示例”的元素。 |
备注:
- SeleniumLibrary 3.0和更高版本仅支持strategy:value语法
- 使用 sizzle 策略或其别名 jquery 要求被测系统包含jQuery库
- 在SeleniumLibrary 3.0之前,与表相关关键字仅支持xpath, css和sizzle / jquery 策略
*** Setting ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://www.baidu.com chrome
#Input Text id=kw hello
#Input Text kw hello
#Input Text //*[@id="kw"] hello
#Input Text name:wd hello
Input Text css:#kw 京东
Click Button id:su
Sleep 3
Click Element partial link:京东
Sleep 6
Close Browser
SeleniumLibrary 浏览器和窗口
SeleniumLibrary 浏览器和窗口相关关键字
- Open Browser
- Close Browser
- Maximize Browser Window
- Switch Browser
- Go To
- Go Back
- Close All Browsers
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
${res} Open Browser http://www.baidu.com chrome chrome
Maximize Browser Window
Go To http://www.jd.com
Sleep 3
Go Back
Sleep 3
Close Browser
Sleep 6
${res} Open Browser http://www.jd.com ff firefox
Sleep 6
Switch Browser 1
Page Should Contain 百度
Sleep 3
Close All Browsers
- Switch Window
- Get Window Handles
- Get Window Identifiers
- Get Window Names
- Get Window Position
- Get Window Size
- Get Window Titles
- Set Window Position
- Set Window Size
- Close Window
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://www.baidu.com chrome
${title} Get Window Titles
sleep 5
${id} Get Window Identifiers
Set Window Position 0 0
${x} ${y} Get Window Position
#js window.open()
${names} Get Window Names
${h} Get Window Handles
Execute Javascript Window.open()
#Switch Window NEW
Go To http://www.jd.com
#Switch Window MAIN
Sleep 3
Close Browser
SeleniumLibrary 操作表单元素
测试网站
http://sahitest.com
操作表单元素关键字
- Select Checkbox
- Unselect Checkbox
- Checkbox Should Be Selected
- Checkbox Should Not Be Selected
- Input Password
- Input Text
- Select Radio Button
- Radio Button Should Be Set To
- Radio Button Should Not Be Selected
- Submit Form
- Textfield Should Contain
- Textfield Value Should Be
- Textarea Value Should Be
- Textarea Should Contain
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/formTest.htm chrome
Handle Alert ACCEPT
Select Checkbox name:c1
Checkbox Should Be Selected name:c1
Sleep 3
Unselect Checkbox name:c1
Checkbox Should Not Be Selected name:c1
Sleep 3
Input Password name:p1 123
Sleep 3
Input Text name:t1 hello,world!
Sleep 3
Select Radio Button r1 rv1
Sleep 3
Radio Button Should Be Set To r1 rv1
Sleep 3
Radio Button Should Not Be Selected r1
Sleep 3
Submit Form name:f1
Handle Alert ACCEPT
Sleep 3
Textfield Should Contain name:t1 hello
Textfield Value Should Be name:t1: hello,world!
Input Text name:ta1 haha
Textarea Value Should Be name:ta1 haha
Textarea Should Contain name:ta1 ha
Submit Form name:f1
Handle Alert ACCEPT
Close Browser
SeleniumLibrary 操作下拉列表
下拉列表
-
Get List Ltems
-
Select From List By Index
-
Select From List By Label
-
Select From List By Value
-
Select All From List
-
Get Selected List Label
-
Get Selected List Labels
-
Get Selected List Value
-
Get Selected List Values
-
Unselect All From List
-
Unselect From List By Index
-
Unselect From List By Label
-
Unselect From List By Value
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/formTest.htm chrome
Handle Alert
${items} Get List Items id:s1Id
${item} Select From List By Index id:s1Id 1
Select From List By Label id:s1Id 03
Select From List By Value id:s1Id 02
Select From List By Index xpath:/html/body/form/select[4] 0
Handle Alert
Select From List By Index xpath:/html/body/form/select[4] 1
Handle Alert
Select From List By Index xpath:/html/body/form/select[4] 2
Handle Alert
Select From List By Index id:s1Id 1
Handle Alert
Select From List By Index xpath:/html/body/form/select[4] 1
Handle Alert
Sleep 3
Unselect From List By Index xpath:/html/body/form/select[4]
Handle Alert
${label} Get Selected List Label id:s1Id
${value} Get Selected List Value id:s1Id
Sleep 3
Close Browser
SeleniumLibrary 鼠标键盘事件
- Click Button
- Click Element
- Double Click Element
- Click Element At Coordinates
- Click lmage
- Click Link
- Drag And Drop
- Drag And Drop By Offset
- Mouse Down
- Mouse Down On lmage
- Mouse Down On Link
- Mouse Out
- Mouse Over
- Mouse Up
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/click.htm chrome
Click Button xpath:/html/body/form/input[3]
Click Element xpath:/html/body/form/input[3]
Double Click Element xpath:/html/body/form/input[2]
Click Element At Coordinates xpath:/html/body/form/input[3] 5 5
Click Image xpath:/html/body/img
Open Browser http://www.baidu.com chrome
Click Link name:tj_trnews
Open Browser http://sahitest.com/demo/clicks.htm chrome
Drag And Drop id:dragger xpath:/html/body/div[2]
Open Browser http://sahitest.com/demo/mouseover.htm chrome
Mouse Over xpath:/html/body/a[1]
Mouse Down xpath:/html/body/a[2]
Mouse Down On Link xpath:/html/body/a[2]
Sleep 8
Close Browser
SeleniumLibrary 元素相关操作
- Get WebElement
- Get WebElements
- Element Attribute Value Should Be
- Element Should Be Disabled
- Element Should Be Enabled
- Element Should Be Focused
- Element Should Be Visible
- Element Should Be Contain
- Element Should Not Be Visible
- Element Should Not Contain
- Element Text Should Be
- Element Text Should Not Be
- Get Element Attribute
- Get Element Count
- Get Element Size
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/formTest.htm chrome
Handle Alert
${e} Get WebElement name:t1
${es} Get WebElements name:c1
Element Attribute Value Should Be name:t1 type text
Element Should Be Disabled textdisabled
Element Should Be Enabled name:t1
Element Should Be Focused name:t1
Element Should Be Visible name:t1
Input Text name:t1 hello
Sleep 3
Element Should Contain name:t1 ${empty}
${attr} Get Element Attribute name:t1 type
${count} Get Element Count name:t1
${size} Get Element Size name:t1
Input Text Into Alert name:t1 hello
Element Should Contain name:t1 type
Close Browser
SeleniumLibrary 页面相关操作
-
Page Should Contain
-
Page Should Contain Button
-
Page Should Contain Checkbox
-
Page Should Contain Element
-
Page Should Contain Image
-
Page Should Contain Link
-
Page Should Contain List
-
Page Should Contain Radio Button
-
Page Should Contain Textfield
-
Page Should Not Contain
-
Page Should Not Contain Button
-
Page Should Not Contain Checkbox
-
Page Should Not Contain Element
-
Page Should Not Contain Image
-
Page Should Not Contain Link
-
Page Should Not Contain List
-
Page Should Not Contain Radio Button
-
Page Should Not Contain Textfield
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/formTest.htm chrome
Handle Alert
Page Should Contain Form Test
Page Should Contain Button xpath://*[@id="submitBtnId"]
Page Should Contain Checkbox xpath:/html/body/form/input[1]
Page Should Contain Element xpath://*[@id="submitBtnId"]
Page Should Contain Image xpath:/html/body/form/img[3]
Page Should Contain Link xpath:/html/body/a
Page Should Contain List s1Id
Page Should Contain Radio Button xpath:/html/body/form/input[5]
Page Should Contain Textfield t1
Close Browser
SeleniumLibrary 操作弹框
- Handle Alert
- Alert Should Be Present
- Alert Should Not Be Present
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/alertTest.htm chrome
Click Button xpath:/html/body/form/input[2]
Sleep 3
Handle Alert
Close Browser
case02
Open Browser http://sahitest.com/demo/confirmTest.htm chrome
Click Button b1
Sleep 3
Handle Alert action=DISMISS
Sleep 3
Close Browser
case03
Open Browser http://sahitest.com/demo/promptTest.htm chrome
Click Button b1
Sleep 3
${res} Handle Alert
Close Browser
case04
Open Browser http://sahitest.com/demo/alertTest.htm chrome
Click Button xpath://html/body/form/input[2]
Sleep 3
Alert Should Be Present Alert Message
Close Browser
case05
Open Browser http://sahitest.com/demo/ chrome
Alert Should Not Be Present
Close Browser
SeleniumLibrary 表格操作
- Table Cell Should Contain
- Table Column Should Contain
- Table Footer Should Contain
- Table Header Should Contain
- Table Row Should Contain
- Table Should Contain
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
case01
log test
Open Browser http://sahitest.com/demo/tableTest.htm chrome
Table Cell Should Contain xpath:/html/body/table[1] 1 1 0-0
Table Column Should Contain xpath:/html/body/table[1] 1 0-0
Table Row Should Contain xpath:/html/body/table[1] 1 0-0
Table Header Should Contain xpath://*[@id="tableWithId"]/thead header 1
Table Should Contain xpath:/html/body/table[1] 0-0
Close Browser
SeleniumLibrary 执行js
- Execute Javascript
- Execute Async Javascript
index.js
alert('hello');
*** Comments ***
这里是注释
*** Settings ****
Library SeleniumLibrary
*** Test Cases ***
case01
Open Browser http://www.baidu.com chrome
Execute Javascript window.open('http://www.jd.com')
Sleep 3
Close Browser
case02
Open Browser http://www.baidu.com chrome
Execute Javascript index.js
Sleep 3
Close Browser
case03
Open Browser http://www.baidu.com chrome
Execute Async Javascript index.js
Sleep 6
Handle Alert
Close Browser
SeleniumLibrary 操作 frame
- Select Frame
- Frame Should Contain
- Current Frame Should Contain
- Current Frame Should Not Contain
*** Comments ***
这里是注释
*** Settings ***
Library SeleniumLibrary
Suite Teardown Close Browser
*** Test Cases ***
case01
Open Browser http://sahitest.com/demo/framesTest.htm chrome
Select Frame name:top
Click Link link:Link Test
Sleep 3
Close Browser
case02
Open Browser http://sahitest.com/demo/framesTest.htm chrome
Frame Should Contain name:top Link Test
Close Browser
case03
Open Browser http://sahitest.com/demo/framesTest.htm chrome
Select Frame name:top
Current Frame Should Contain Link Test
Close Browser
SeleniumLibrary 等待机制
- Set Selenium Implicit Wait
- Set Browser Implicit Wait
- Wait For Condition
- Wait Until Element Contains
- Wait Until Element Does Not Contain
- Wait Until Element Is Enabled
- Wait Until Element Is Not Visible
- Wait Until Element Is Visible
- Wait Until Location Contains
- Wait Until Location Is
- Wait Until Page Contains
- Wait Until Page Contains Element
- Wait Until Page Does Not Contain
- Wait Until Page Does Not Contain Element
*** Comments ***
这里是注释
*** Settings ***
Library Selenium2Library
*** Test Cases ***
case01
Open Browser http://www.baidu.com chrome
Set Selenium Implicit Wait 10 seconds
Wait For Condition return document.title == "百度一下,你就知道"
${title} Get Title
Close Browser
case02
Open Browser http://www.baidu.com chrome
Wait Until Element Contains link:新闻 新闻
Wait Until Element Is Enabled link:新闻
Wait Until Element Is Visible link:新闻
#Wait Until Location Contains http://www.baidu.com
#Wait Until Location Is https://www.baidu.com/
Wait Until Page Contains 新闻
Wait Until Page Contains Element link:新闻
Close Browser