SeleniumLibrary

repository·master·Indexed 23 days ago

https://github.com/robotframework/seleniumlibrary

A web testing library for Robot Framework that leverages Selenium to automate browser interactions. It provides keywords for interacting with web elements and managing browser sessions, and offers a Public API, Plugin API, and EventFiringWebDriver support for extending its functionality.

Tokens
26.3K
Snippets
36
Records
274
Agent score
81%

What's inside SeleniumLibrary

  1. Understand SeleniumLibrary Timeouts

    master

    SeleniumLibrary uses timeouts for various keywords.

    • Global Timeout: The default timeout can be set globally using the Set Selenium Timeout keyword or by passing a timeout argument when importing the library.
    • Default Value: If no global timeout is set, the default is 5 seconds.
    • Handling None: If None is specified for a timeout argument in a keyword, the library will fall back to using the currently set global default timeout.
  2. Using desired_capabilities with Open Browser

    master

    When using the Open Browser keyword, be aware of how desired_capabilities are handled:

    1. Dictionary Support: You can define desired_capabilities as a dictionary.
    2. Capability Merging: In version 3.3.1, if user-defined desired_capabilities are provided, they are used as is and are not joined with the default Selenium browser-specific capabilities.
    3. Requirement: Because they are no longer automatically joined, you must define all capabilities required to launch the browser within your desired_capabilities argument if you choose to provide it.
  3. Use Get DOM Attribute and Get Property for element data

    master

    Selenium 4 introduced specific methods to distinguish between HTML attributes and DOM properties. The older Get Element Attribute keyword uses Selenium's getAttribute() method, which may return an inferred value rather than the exact attribute or property requested.

    It is recommended to transition to these newer keywords for precision:

    • Get DOM Attribute: Fetches the specific HTML attribute.
    • Get Property: Fetches the specific DOM property.
  4. Understand the 'run on failure' change in SeleniumLibrary 6.0.0

    master

    In SeleniumLibrary 6.0.0, the run on failure functionality was updated to ensure reliability. Specifically, the Capture Page Screenshot action is now executed as a method rather than a Robot Framework keyword.

    This change prevents conflicts in test suites where another library might also define a keyword named Capture Page Screenshot. Previously, Robot Framework's library search order could cause the wrong library's keyword to be selected. This is a backwards-incompatible change, though most users will only notice a difference in how the log is displayed.

  5. Use LibraryComponent to build SeleniumLibrary plugins

    master

    When creating plugins for SeleniumLibrary, your plugin classes must inherit from LibraryComponent. This class provides an IDE-friendly interface and convenient shortcuts to the SeleniumLibrary's internal state and public API.

    While the ctx attribute provides access to the SeleniumLibrary Public API, LibraryComponent exposes the active browser directly via self.driver (instead of self.ctx.driver) and includes several helper methods for element interaction and logging.

  6. Handle Open Browser argument changes in Selenium 4.10.0+

    master

    Due to changes in Selenium v4.10.0+, certain arguments in the Open Browser keyword are handled differently. If you use advanced configurations, review the following changes:

    service_log_path and executable_path

    These arguments are now passed differently to the webdriver creation. Most users will not need to change their code, but verify that driver initialization still works as expected.

    Firefox profile directory (ff_profile_dir)

    The ff_profile_dir argument is now attached to the options structure before being passed to the webdriver creation. Warning: Do not set the profile in two places at once. If you are already setting the profile via the options argument (e.g., Open Browser | None | Firefox | options=profile=/path/to/profile/dir) AND using ff_profile_dir, you may encounter unexpected behavior.

    desired_capabilities

    The desired_capabilities argument is deprecated and has been completely removed by Selenium. SeleniumLibrary now ignores this argument. You should migrate to using options for browser configuration as recommended by Selenium or your browser vendor (e.g., SauceLabs, BrowserStack).

  7. Use the SeleniumLibrary Plugin API

    master

    SeleniumLibrary 4.0 introduced a plugin API that allows you to add or modify keywords directly within the library and provides wider access to internal methods.

    When creating a plugin, ensure it is the last thing loaded in the SeleniumLibrary.__init__ to ensure correct discovery of plugins and event-firing WebDrivers.

    To easily add an event-firing WebDriver to your plugin, you can use the event_firing_webdriver attribute.

    class MyPlugin(LibraryComponent):
        def __init__(self, ctx):
            LibraryComponent.__init__(self, ctx)
            self.event_firing_webdriver = WhatEverYouWant
  8. Coding conventions and style for SeleniumLibrary

    master

    When contributing code, follow these guidelines:

    • Python Style: Follow PEP-8 and aim for idiomatic/Pythonic code using SOLID principles. Code should be clear enough that comments are generally unnecessary.
    • Docstrings: Use PEP-257 for docstrings. They are required for public keywords but generally not needed for internal code.
    • Linting/Formatting: Use Ruff for formatting and linting.
    • Naming: Prefer the term message over error when indicating custom error messages in keywords.
    • Git Workflow: Use dedicated branches for pull requests. Maintain a linear history of commits; avoid merge commits or noise. Use git pull --rebase instead of git pull --merge when pulling changes from upstream.