美文网首页测试圈测试杂谈测试基础简介
SeleniumLibrary关键字介绍及使用

SeleniumLibrary关键字介绍及使用

作者: 测试猴 | 来源:发表于2017-12-18 10:44 被阅读62次

    大家对selenium这个自动化测试工具一定不陌生,关于selenium就不多说啦,最近项目要使用robotframework做自动化回归测试,小编每日和文档上的小蚂蚁斗智斗勇,下面给大家分享一下战斗的成果,介绍一下seleniumlibrary库的关键字,体会一下selenium和seleniumlibrary使用的差异:

    Close Browser:close_browser(self):调用当前浏览器的quit命令,将该浏览器实例放入cache的closed集合中。

    Close All Browser:close_all_browsers(self):调用当前所有打开的浏览器的quit命令,清空整个cache。

    Switch Browser:switch_browser(self, index_or_alias):切换当前浏览器,可以用Open Browser返回的值(index,从1开始)或者Open Browser时指定的{alias}参数来指明切换到哪个浏览器。

    Close Window:close_window(self):关掉当前弹出窗口,其实弹出窗口相当于一个缓存在cache中的browser,关闭时调用该browser的close方法。如果没有当前broswer,会Raise RuntimeError('No browser is open')异常。

    Get Window Identifiers:get_window_identifiers(self):返回当前browser关联的所有dom对象window的id属性数组,同时会调用_LoggingKeywords的_log_list(self, items, what='item')把ids数组作为info写入robot的日志。如果没有当前broswer,会Raise RuntimeError('No browser is open')异常。

    Get Window Names:get_window_names(self):返回当前browser关联的所有dom对象window的name属性数组,同样会调用_LoggingKeywords的_log_list(self, items, what='item')把names数组作为info写入robot的日志。如果没有当前broswer,会Raise RuntimeError('No browser is open')异常。window对象的name属性一般在js函数-window.open中指定,如果没有指定(undefined)并且返回的name数组长度为1,说明当前browser就打开了一个窗口,undefined会被替换成selenium_main_app_window

    Get Window titles:get_window_identifiers(self):返回当前browser关联的所有dom对象document的title属性数组,同时会调用_LoggingKeywords的_log_list(self, items, what='item')把titles数组作为info写入robot的日志。如果没有当前broswer,会Raise RuntimeError('No browser is open')异常。

    Maximize Browser Window:maximize_browser_window(self):最大化当前浏览器窗口。

    Select Frame:select_frame(self, locator):用传入的locator作为参数调用_ElementKeywords的_element_find(self, locator, first_only, required, tag=None)方法得到符合locator条件的第一个dom元素,然后调用当前浏览器的SWITCH_TO_FRAME命令,命令参数名为id,参数值即为locator得到的dom元素。frame的locator支持id和name两种方式。

    Select Window:select_window(self, locator=None):用传入的locator去匹配当前浏览器的所有dom对象window里面的属性,如果匹配到了,就调用当前浏览器的SWITCH_TO_WINDOW命令,命令参数名为name,参数值即为locator得到的window handler对象。这里的locator不是selenium的locator,而是rf自己是实现的WindowManager,支持通过title,name,url的方式查询window。

    Unselect Frame:unselect_frame(self):调用当前浏览器的SWITCH_TO_FRAME命令,命令参数名为id,参数值为None。

    Click button[locator]:点击按钮,locator是按钮位置

    Click element[]:点击元素,locator是元素位置

    Click element at coordinates[]:点击元素,该元素以x/y坐标为准

    Click image[]:点击图片

    Click link[]:点击链接

    Delete all cookies[]:删除所有cookie

    Delete cookie[name]:删除名字为name的cookie,如果没有匹配到,不会执行任何操作

    Double click element[]:双击元素

    Element should be disabled[locator]:验证元素是否可用

    Element should be visible[]:验证元素是否可见


    好啦,直接上代码:

    RF写法

    *** Settings ***

    LibrarySeleniumLibrary

    *** Test Cases ***

    测试百度

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

    input textid=kw    robot framework

    click button   id=su

    close Browser


    Python写RF测试代码

    # 测试用例:启动浏览器

    def open_browsers(self):

    test_01=self.suite.tests.create("启动浏览器")

    test_01.keywords.create("Open Browser",args=["https://www.baidu.com"

    ,"chrome"])

    test_01.keywords.create("Title Should Be",args=["百度一下,你就知道"])

    以上是关键字的使用代码示例,不同的写法

    根据文档翻译的,暂时先翻译到这,后续会给大家继续更新,感兴趣的小伙伴关注一下,谢谢大家的支持,你们的支持就是小编的动力

    sunny

    相关文章

      网友评论

        本文标题:SeleniumLibrary关键字介绍及使用

        本文链接:https://www.haomeiwen.com/subject/ekbpwxtx.html