1 of 12

Extending SeleniumLibrary

Tatu Aalto, SeleniumLibrary lead developer

2 of 12

New architecture vision

  • Make Python 3 support easier to achieve.
    • Removing unnecessary complexity helps with the Python 3 support.
    • Make code easier to read, understand and maintain.
  • Good and clean public API for probramating use.
    • Clear separation of keywords, public and private methods.
    • Separation to own name spaces.

3 of 12

New architecture: Public methods

  • Public API methods, which are not keywords:

find_element

Finds first element matching "locator".

find_elements

Find all elements matching "locator.

register_driver

Add a Selenium "driver" to the library WebDriverCache.

run_keyword

Responsible for executing keywords.

failure_occured

Executed when a SeleniumLibrary keyword fails.

4 of 12

New architecture: Public attributes

Public API attributes:

driver

Current active Selenium driver in the library.

timeout

Default value for "timeouts" used with "Wait ..."" keywords.

implicit_wait

Default value for Selenium "implicit wait" used when locating elements.

run_on_failure_keyword

Stores the keyword for the "run-on-failure functionality".

screenshot_root_directory

Location where possible screenshots are created.

5 of 12

New architecture: Context object

  • Context object allows access to public methods for the keyword classes.
    • Context object the SeleniumLibrary instance.
    • Provides access to keywords from other classes.
  • Almost all the classes uses context.
    • From high level keyword classes to the low level ElementFinder class.

6 of 12

New architecture: base object

  • LibraryComponent and ContextAware classes
    • Wrappers for context object methods and attributes.
    • Declares generally useful objects, like logging or log_dir, to be used easily in the library.
  • LibraryComponent is intended for classes implementing library keywords.
  • ContextAware is intended for classes not implementing library keywords.
    • Example for ElementFinder.
  • PythonLibCore eases the usage of the dynamic library API.
    • @keyword decorator is used to identify the keywords.

7 of 12

The end result

  • SeleniumLibrary has a new heart.
    • With keeping the backwards compatibility on keywords level.
    • Backwards compatibility is not usually kept on code level.
  • Using dynamic library API really makes a difference in library development.
    • Although some IDE do not support dynamic library API.
  • Context object, LibraryComponent and ContextAware makes the code readable and development more easier.
  • Testing in the unit level is also easy.
    • Less methods to mock.
    • Usually context is the only thing that is mocked.

8 of 12

Extending the library

  • Robot Framework user guide provides good start how extend libraries.
  • Read PythonLibCore documentation and examples
    • The PythonLibCore project documentation should be improved.
  • Three main ways to extend:
    • Creating new library by getting active library instance with BuiltIn.
    • Creating new library by inheriting the SeleniumLibrary.
    • Extending SeleniumLibrary directly.

9 of 12

How to extend: Getting active instance

10 of 12

How to extend: Using multiple classes

11 of 12

How to extend: Extend SeleniumLibrary

12 of 12

Questions?