场景:打开百度页面,搜索robot,验证搜索结果页面包含"机器人"关键字
- window下robot测试用例
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
test1
open browser https://www.baidu.com chrome
Wait Until Keyword Succeeds 10 2 input text id=kw robot #在输入框输入robot
Wait Until Keyword Succeeds 10 2 click element id=su #点击百度一下
sleep 3
Wait Until Keyword Succeeds 10 2 Page Should Contain 机器人
close browser
- linux下robot测试用例
因为linux无图像界面,运行用例时需要用到无头浏览器,最新版的chrome支持headless模式,则修改以上的用例,设置启动chrome时使用无头模式
创建一个chrometest.py的文件,设置chrome的options参数
#chrometest.py
from selenium.webdriver.chrome.options import Options
def get_chrome_options():
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless') # 无头参数
chrome_options.add_argument('--disable-gpu')
return chrome_options
在用例中添加Library chrometest.py,即可以不开启浏览器执行完测试用例
*** Settings ***
Library SeleniumLibrary
Library chrometest.py
*** Test Cases ***
test1
${options} get_chrome_options #获取chrome options
create webdriver Chrome options=${options}
go to https://www.baidu.com
Wait Until Keyword Succeeds 10 2 input text id=kw robot #在输入框输入robot
Wait Until Keyword Succeeds 10 2 click element id=su #点击百度一下
sleep 3
Wait Until Keyword Succeeds 10 2 Page Should Contain 机器人
close browser
无头+打开浏览器模式
from selenium import webdriver
#web模式
def create_headlesschrome_options():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1920,1080')
return chrome_options
#H5模式
def create_app_headlesschrome_options(deviceName='iPhone 5/SE'):
devname={'deviceName':deviceName}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#chrome_options.add_argument('--deviceName=iPhone 5/SE')
chrome_options.add_experimental_option('mobileEmulation',devname)
# chrome_options.add_argument('--window-size=1920,1080')
return chrome_options
添加library后
web无头模式
${options} create_headlesschrome_options #web模式
Create Webdriver Chrome chrome_options=${options}
H5无头模式
${options} create_app_headlesschrome_options #H5模式
Create Webdriver Chrome chrome_options=${options}