---
title: "RobotFramework-Selenium2Library各个关键字的作用(1)"
author: "ratlsun"
date: "2013-05-14T17:34:00Z"
tags: []
categories: ["开源软件"]
slug: "robotframework-selenium2library-1"
draft: false
lastmod: "2013-08-22T17:22:43Z"
id: 1869379
url: "https://ratlsun.iteye.com/blog/1869379"
---

# \_\_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

01. **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](https://code.google.com/p/selenium/wiki/DesiredCapabilities)查找。{ff\_profile\_dir}指定firefox浏览器使用的配置文件目录，当然只有当{browser}指定的浏览器是ff时才会起作用，默认其实会使用\[Selenium2Library\\resources\\firefoxprofile]目录下的配置文件。所有新建的浏览器都会放入RF提供的connection cache中。
02. **Close Browser：**close\_browser(self)：调用当前浏览器的quit命令，将该浏览器实例放入cache的closed集合中。
03. **Close All Browser：**close\_all\_browsers(self)：调用当前所有打开的浏览器的quit命令，清空整个cache。
04. **Switch Browser：**switch\_browser(self, index\_or\_alias)：切换当前浏览器，可以用**Open Browser**返回的值（index，从1开始）或者**Open Browser**时指定的{alias}参数来指明切换到哪个浏览器。
05. **Close Window：**close\_window(self)：关掉当前弹出窗口，其实弹出窗口相当于一个缓存在cache中的browser，关闭时调用该browser的close方法。如果没有当前broswer，会Raise RuntimeError('No browser is open')异常。
06. **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')异常。
07. **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'*
08. **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')异常。
09. **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。