RobotFramework-Selenium2Library各个关键字的作用(1)

__init__.py

定义Selenium2Library对象,继承自所有的keywords对象,init函数中调用所有父对象的init函数,设置_BrowserManagementKeywords的timeout和implicit_wait,_RunOnFailureKeywords的keywords属性。Selenium2Library即为RF的库名,__init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot') 表示可以在申明库的同时传递三个参数,具体参数的含义在使用这些参数的关键字里介绍。ROBOT_LIBRARY_SCOPE = 'GLOBAL' 表明这个库是全局范围的,即整个测试过程中只有一个Selenium2Library对象的实例。


keywordgroup.py

定义所有keywords类的父类KeywordGroup,该类的metaclass会对keywords类中的所有非_开头的方法(也就是RF的关键字)加上_run_on_failure_decorator()的修饰,在原有方法出错时执行keywords类的_run_on_failure()方法


_browsermanagement.py

  1. Open Browser:open_browser(self, url, browser='firefox', alias=None, remote_url=False, desired_capabilities=None, ff_profile_dir=None):用{browser}指定的浏览器打开{url}地址,可用的浏览器名称如下-
        
            |       名称       |              浏览器            |    
            | firefox          | FireFox                        |
            | ff               | FireFox                        |
            | internetexplorer | Internet Explorer              |
            | ie               | Internet Explorer              |
            | googlechrome     | Google Chrome                  |
            | gc               | Google Chrome                  |
            | chrome           | Google Chrome                  |
            | opera            | Opera                          |
            | phantomjs        | PhantomJS                      |
            | htmlunit         | HTMLUnit                       |
            | htmlunitwithjs   | HTMLUnit with Javascipt support|
      {alias}指定这个浏览器实例的别名以便以后切换浏览器时使用。{remote_url}可以让selenium使用一个远程的浏览器,用{desired_capabilities}可以设置远程浏览器的配置属性,具体浏览器支持哪些配置可以在https://code.google.com/p/selenium/wiki/DesiredCapabilities查找。{ff_profile_dir}指定firefox浏览器使用的配置文件目录,当然只有当{browser}指定的浏览器是ff时才会起作用,默认其实会使用[Selenium2Library\resources\firefoxprofile]目录下的配置文件。所有新建的浏览器都会放入RF提供的connection cache中。
  2. Close Browser:close_browser(self):调用当前浏览器的quit命令,将该浏览器实例放入cache的closed集合中。
  3. Close All Browser:close_all_browsers(self):调用当前所有打开的浏览器的quit命令,清空整个cache。
  4. Switch Browser:switch_browser(self, index_or_alias):切换当前浏览器,可以用Open Browser返回的值(index,从1开始)或者Open Browser时指定的{alias}参数来指明切换到哪个浏览器。
  5. Close Window:close_window(self):关掉当前弹出窗口,其实弹出窗口相当于一个缓存在cache中的browser,关闭时调用该browser的close方法。如果没有当前broswer,会Raise RuntimeError('No browser is open')异常。
  6. 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')异常。
  7. 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'
  8. 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')异常。
  9. Maximize Browser Window:maximize_browser_window(self):最大化当前浏览器窗口。
  10. 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两种方式。
  11. 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。
  12. Unselect Frame:unselect_frame(self):调用当前浏览器的SWITCH_TO_FRAME命令,命令参数名为id,参数值为None。

猜你喜欢

转载自ratlsun.iteye.com/blog/1869379