In the robot framework, use the selenium Library

Keywords: Firefox Selenium Session Python

At present, we use Python 2.7 (please do not despise, company environment, can't use Python 3) + robot framework to automate the function test. Today, we need to use selenium for web page operations, as follows


Installing the selenium 2 library Library

Using pip installation, the process is simple: pip install robot framework selenium 2 Library


Headless mode

Other ways of using are not mentioned here, but how to implement headless mode of browser

The common Open Browser keyword cannot implement headless mode locally (if there is an error, please point out), because the function it finally calls is

    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)
            browser = webdriver.Firefox(firefox_profile=profile)
        return browser

We can see that for the local browser, when creating the Firefox instance, only the profile parameter is used, and the headless parameter cannot be passed in, because the headless parameter needs to be passed in in the options or firefox_options parameter. You can see the following Firefox object's init function:

    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,
        """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

To implement headless mode, we need to pass in options or Firefox options, which is not recommended.


How to realize headless mode??

You can use another keyword, Create Webdriver

Let's first look at the implementation of this keyword:

    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()
            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)

It can be seen that it directly calls the webdriver interface (for example, Firefox function is called here). With the implementation of Firefox, we can know how to use it:

    ${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}


Posted by flash99 on Tue, 03 Dec 2019 03:01:57 -0800