Menu

Handlers

Bartek Wilczek

Introduction

Each test in bubik is represented by an independent object (instance of Scenario class). It's lifetime is limited only to time of 'run' method execution. None of the object created inside a test can be reused in other tests. Therefore the concept of handlers has been introduced.

Handler is an instance of a class that extends Bubik::Handler and is located in module Bubik::Handlers. Built-in handlers like 'webdriver' are located in BUBIK_DIR/handlers. Project specific handler should be located in PROJECT_DIR/handlers. Both directories are scanned for proper classes when handler object is requested from the test code for the first time.

Accessing handlers from test code

# In test code:
webdriver_handler = handler( :webdriver )
browser = webdriver_handler.driver

# In other handlers:
webdriver_handler = @runner.get_handler( :webdriver )
browser = webdriver_handler.driver

The code above will set webdriver_handler variable to reference to instance of Webdriver hander object that is created once per whole test execution process. If there's a need to have multiple instances of the same handler class (for example to have multiple browser sessions for one test) additional parameter instance_id has to be introduced to 'handler' function call:

# In test code:
webdriver_handler = handler( :webdriver, 2 )
browser = webdriver_handler.driver

# In other handlers:
webdriver_handler = @test.handler( :webdriver, 2 )
browser = webdriver_handler.driver

Life cycle

An instance of handler class is created when handler() function is called for the first time in any place of the code. Handler class implements Listener interface, so it supports methods start_exec, start_test, start_variant, end_variant, end_test, end_exec. When first instantiated execution control flow is inside 'variant' so the following 3 methods are executed: start_exec, start_test, start_variant. Initialization of objects needed throughout the whole execution should be set in start_exec and cleaned up in end_exec. Other start/end methods can be used to manipulate these internal objects: for example Webdriver handler uses start_variant method to restart browser window on each variant.


Related

Wiki: Home
Wiki: Test creation

MongoDB Logo MongoDB
Gen AI apps are built with MongoDB Atlas
Atlas offers built-in vector search and global availability across 125+ regions. Start building AI apps faster, all in one place.
Try Free →