在robotframework中,使用selenium库,无头模式

目前使用python2.7(请勿鄙视,公司环境,无法使用python3)+ robot framework,做功能测试的自动化。 今天需要使用selenium进行web页面的操作,记录如下

 

安装selenium2Library库

使用pip安装,过程是简单: pip install robotframework-selenium2Library

 

无头模式

其他的使用方式这里就不说了,只说怎么实现浏览器的无头模式

常用的Open Browser关键字,无法在本地实现无头模式(如果有错误,请指出),因为其最终调用的函数是

    def _make_ff(self , remote , desired_capabilites , profile_dir):

        if not profile_dir: profile_dir = FIREFOX_PROFILE_DIR
        profile = webdriver.FirefoxProfile(profile_dir)
        if remote:
            browser = self._create_remote_web_driver(webdriver.DesiredCapabilities.FIREFOX  ,
                        remote , desired_capabilites , profile)
        else:
            browser = webdriver.Firefox(firefox_profile=profile)
        return browser

我们可以看到,对于本地浏览器来说,创建Firefox实例时,仅用了profile参数,无法将无头参数传入,因为无头参数需要在options或者firefox_options参数中传入。可以看下面Firefox对象的__init__函数:

    def __init__(self, firefox_profile=None, firefox_binary=None,
                 timeout=30, capabilities=None, proxy=None,
                 executable_path="geckodriver", options=None,
                 log_path="geckodriver.log", firefox_options=None,
                 service_args=None):
        """Starts a new local session of Firefox.

        """
        if firefox_options:
            warnings.warn('use options instead of firefox_options', DeprecationWarning)
            options = firefox_options
        self.binary = None
        self.profile = None
        self.service = None
                .
                .
                .
                .

要实现无头模式,我们需要通过options或者firefox_options参数传入,而firefox_options已经不建议使用了。

 

那怎么实现无头模式呢??

可以使用另一个关键字 Create Webdriver

我们先看看这个关键字的实现:

    def create_webdriver(self, driver_name, alias=None, kwargs={}, **init_kwargs):
        """Creates an instance of a WebDriver.

        """
        if not isinstance(kwargs, dict):
            raise RuntimeError("kwargs must be a dictionary.")
        for arg_name in kwargs:
            if arg_name in init_kwargs:
                raise RuntimeError("Got multiple values for argument '%s'." % arg_name)
            init_kwargs[arg_name] = kwargs[arg_name]
        driver_name = driver_name.strip()
        try:
            creation_func = getattr(webdriver, driver_name)
        except AttributeError:
            raise RuntimeError("'%s' is not a valid WebDriver name" % driver_name)
        self._info("Creating an instance of the %s WebDriver" % driver_name)
        driver = creation_func(**init_kwargs)
        self._debug("Created %s WebDriver instance with session id %s" % (driver_name, driver.session_id))
        return self._cache.register(driver, alias)

可以看到,它直接调用的webdriver的接口(以firefox举例,这里调用的就是Firefox函数),结合Firefox的实现,我们可以知道怎么用了:

    ${firefox_options} =     Evaluate    sys.modules['selenium.webdriver'].FirefoxOptions()    sys, selenium.webdriver
    Call Method    ${firefox_options}   add_argument    -headless

    create webdriver    Firefox    options=${firefox_options}
    go to    http://${I_NODE_IP}:${SWHLS_PORT}/v1/web?action=login&session=${sessionId}

 

你可能感兴趣的:(Robot,framework)