zoukankan      html  css  js  c++  java
  • Robot Framework常用关键字

    Selenium2Library常用关键字

    Open Browser 打开浏览器

    Open Browser http://www.baidu.com gc

    打开浏览器对应的关键字

    firefox FireFox
    ff FireFox
    internetexplorer Internet Explorer
    ie Internet Explorer
    googlechrome Google Chrome
    gc Google Chrome
    chrome Google Chrome

    Open Browser打开本地html页面

    Open Browser file:///D:/robot/data/select_demo.html gc
    备注:
    要想通过不同的浏览打开URL 地址,一定要安装浏览器相对应的驱动。
    chrome 的驱动为: chromedriver.exe 。
    IE 的驱动为: IEDriverServer.exe
    浏览器默认为空时启动 FireFox
    

    关闭浏览器

    Close All Browser 关键所有打开的浏览器和缓存重置
    Close Browser 关闭当前的浏览器

    浏览器窗口设置

    set windows size :用于设置打开浏览器的宽度和高度。以像素为单位,第一个参数800表示宽度,第二个参数900表示高度。
    get windows size 用于获取当前浏览器的宽度和高度。

    Open Browser http://www.baidu.com gc
    Maximize Browser Window # 全屏查看浏览器
    Set Windows Size 800 900
    ${width} ${height} Get Windows Size
    log ${width}
    log ${height}

    文本输入

    input text 表示关键字用于向文本框内输入内容。
    id=kw 表示元素定位,定位文本输入框
    robot framework 表示输入框输入的内容

    Open Browser http://www.baidu.com gc
    input text id=kw robot framework
    Sleep 3
    Close All Browsers

    点击元素

    Click Element 用于点击页面上的元素,可以点击按钮、文字/图片连接、复选框、 单选框、甚至是下拉等。
    id=su 表示元素定位

    Open Browser http://www.baidu.com gc
    input text id=kw robot framework
    Click Element id=su
    Sleep 3
    Close All Browsers

    点击按钮

    Click Element 关键字用于点击页面上的按钮。
    id=su 表示元素定位

    Open Browser http://www.baidu.com gc
    input text id=kw robot framework
    Click Button id=su #只能用于点击按钮
    Sleep 3
    Close All Browsers

    等待元素

    等待页面包含元素

    Wait Until Page Contains Element 关键字用于等待页面上的元素显示出来。
    id=kww 表示用于定位元素
    5 表示最长等待时间
    error 表示错误提示,自定义错误提示,如:“元素不能正常显示”

    Open Browser http://www.baidu.com gc
    Input text id=kw robot framework
    Wait Until Page Contains Element xpath=/html/body/div[1]/div[1]
    /div/div[1]/div/form/span[2]/input
    5 error
    Click Button xpath=/html/body/div[1]/div[1]
    /div/div[1]/div/form/span[2]/input
    Close All Browsers

    等待元素包含文本

    css=a.toindex:百度首页的定位

    百度首页:元素包含的文本

    10:表示最长等待时间

    元素未出现:自定义报错内容

    Open Browser http://www.baidu.com gc
    Input text id=kw robot framework
    Wait Until Element Contains css=a.toindex 百度首页 10 元素未出现
    Click Button id=su
    Close All Browsers

    获取 title

    get title 关键字用于获得当前浏览器窗口的 title 信息。我们通常会将获取的title 传递给一个变量,然后与预期结果进行比较,从而判断当前脚本执行成功。

    Open Browser http://www.baidu.com gc
    Maximize Browser Window
    ${title} Get Title
    Page Should Contain ${title}

    获取 text

    get text 关键字用于获取元素的文本信息。
    name=tj_trnews 定位文本信息的元素。

    Open Browser http://www.baidu.com gc
    Maximize Browser Window
    ${element} Get Text name=tj_trnews
    log ${element}

    获取元素的其他属性值

    id=kw 表示定位的元素
    name 表示获取这个元素的 name 属性值。

    Open Browser http://www.baidu.com gc
    ${attr} Get Element Attribute id=kw name
    log ${attr}
    Close All Browsers

    title断言

    Get Title 获得浏览器窗口的titile ,并赋值给变量${title}
    Should Contain 比较${title}是否等于“robot_百度搜索”。

    Open Browser http://www.baidu.com gc
    Input text id=kw robot
    Click Element id=su
    ${title} Get Title
    Should Contain ${title} robot_百度搜索
    Close All Browsers

    text断言

    Get Text:获取页面新闻链接的文本值,并赋值给${text}变量
    Should Contain:${text}中应该包含新闻

    Open Browser http://www.baidu.com gc
    ${text} Get Text name=tj_trnews
    Should Contain ${text} 新闻
    Close All Browsers

    Frame标签

    有时候和页面中会出现表单嵌套,这个时候需要进入到表单才能操作相关元素。

    Select Frame:选择需要进入的框架,可以通过id或者name定位,如果没有id或者name可以通过xpath等

    Unselect Frame:退出框架

    Open Browser file:///D:/frame/frame.html gc
    Select Frame id=f1
    Select Frame id=f2
    Input Text id=kw python
    Click Button id=su
    Unselect Frame
    Close Browser

    下拉框操作

    <!DOCTYPE html>
    <html>
    <head> 
    	<meta charset="utf-8"> 
    	<title>select下拉框</title> 
    </head>
    <body>
    民族:<select name='s'>
    		<option value="a">请选择</option>
    		<option value="b">汉族</option>
    		<option value="c">壮族</option>
    		<option value="d">朝鲜族</option>
    		<option value="e">满族</option>
    	 </select>
    </body>
    </html>
    

    下拉框通过Select类库进行定位

    name=s:下拉框的元素定位

    Select From List By Index:通过下拉框中的索引进行定位

    Select From List By Label:通过下拉框中的文本值进行定位

    Select From List By Value:通过下拉框中的value属性值进行定位

    Open Browser file:///D:/data/select.html gc
    Select From List By Index name=s 1
    Select From List By Label name=s 满族
    Select From List By Value name=s d
    Close All Browsers

    下拉框通过层级定位

    name=s:先定位下拉框

    xpath=//option[@value='c']:再通过xpath进行定位下拉框中的元素

    Open Browser file:///D:/data/select.html gc
    Click Element name=s
    Click Element xpath=//option[@value='c']
    Close All Browsers

    多窗口的切换

    Select Window new 切换到新的窗口

    Select Window title=python官网_百度搜索 根据窗口title切换至其他窗口

    Open Browser http://www.baidu.com gc
    Input Text id=kw python官网
    Click Element id=su
    Click Link python官网
    Select Window new
    Wait Until Page Contains Element id=id-search-field 20 网站未打开
    Input Text id=id-search-field wxpython
    Select Window title=python官网_百度搜索
    Click Element xpath=//*[@id="3"]/h3/a
    Close All Browsers

    执行JavaScript

    通过 js 定位

    Document: 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。

    getElementById() : 返回对拥有指定 id 的第一个对象的引用。

    .value:表示将id=kw的value值设置为test

    Open Browser http://www.baidu.com gc
    Execute Javascript document.getElementById('kw').value='test'
    Execute Javascript $('#su').click()

    通过JQuery定位

    $:表示的是jQuery的语句

    #kw:#表示id的属性值为kw

    .val():表示将id=kw的value值设置为robot

    Open Browser http://www.baidu.com gc
    Execute Javascript $('#kw').val('robot')
    Execute Javascript $('#su').click()
    Close All Browsers

    js操作隐藏元素

    <!DOCTYPE html>
    <html>
    <body>
    
    <form action="/demo/demo_form.asp">
    First name:<br>
    	<input type="text" name="firstname" id='ft' style="display: none;">
    <br>
    Last name:<br>
    <input type="text" name="lastname" >
    <br><br>
    <input type="submit" value="Submit">
    </form> 
    
    </body>
    </html>
    

    页面中如果有style="display: none;"是无法直接定位的,需要通过js更改页面的属性值,从而去定位

    document.getElementById('ft').style.display='block'; 就是将style的显示方式更改为block

    Open Browser file:///D:/data/is_dispaly.html gc
    Execute Javascript document.getElementById('ft').style.display='block';
    Input Text id=ft hello

    js操作进度条

    UI自动化测试中,有时候定位页面元素需要滚动进度条才能够定位

    Open Browser https://www.sina.com.cn/ gc
    Execute Javascript window.scrollBy(0, document.body.scrollHeight) #操作滚动条至底部
    Execute Javascript window.scrollTo(0, document.body.scrollHeight) #操作滚动条至底部
    Execute Javascript window.scrollTo(0,100) #通过调试确定滚动到页面100的位置

    告警框操作

    Handle Alert: 方法有两个参数,[ action=action | timeout=timeout ]

    action:默认值为Accept,表示点击确定

    DISMISS:表示点击取消

    Open Browser file:///D:/data/alert.html gc
    Click Element css=body > div > button:nth-child(2) > h3
    Handle Alert
    Handle Alert DISMISS

    常用关键字项目实战

    QQ自动发送邮件

    Open Browser https://mail.qq.com/cgi-bin/loginpage gc
    Maximize Browser Window
    #等待页面元素出现
    Wait Until Page Contains Element id=login_frame
    #进入框架
    Select Frame id=login_frame
    Wait Until Page Contains Element id=img_out_60XXXXX75
    #点击QQ图标,进行登录
    Click Element id=img_out_60XXXXX75
    Click Element id=composebtn
    #进入框架
    Select Frame id=mainFrame
    #点击收件人输入框
    Input Text xpath=//*[@id="toAreaCtrl"]/div[2]/input 771xxx297@qq.com
    #点击邮件主题
    Input Text id=subject 测试邮件
    #进入正文内容的框架
    Select Frame xpath=//div[@id="QMEditorArea"]/
    table/tbody/tr[2]/td/iframe
    #输入正文的内容
    Input Text xpath=/html/body 测试邮件
    #退出框架
    Unselect Frame
    #进入框架
    Select Frame id=mainFrame
    #点击发送邮件按钮
    Click Element name=sendbtn
    Page Should Contain 您的邮件已发送
    Close All Browsers
  • 相关阅读:
    Python学习之路—2018/6/27
    Python学习之路—2018/6/26
    python面试315问
    day4(css优先级)
    date3(form表单,今天html结束,css)
    date2(html)
    day1
    mysql数据库(7day)
    mysql数据库(day6)索引,ORM框架
    mysql数据库(day5)-视图,触发器,存储过程
  • 原文地址:https://www.cnblogs.com/pan-louis/p/12066596.html
Copyright © 2011-2022 走看看