Back to QMetry All Products Help Page
Test Listeners
- Mrunalini Potnis
Test Listeners allow you to hook your custom code for performing various actions. The listener can perform some actions or track details before/after command execution as well as on failure.
It provides flexibility to expand the scope of testing. You can define your own criteria to create Test Listeners and execute them on fulfilling mentioned conditions.
Automation Listeners
You can create listener by implementing appropriate listener interface of by extending appropriate adapter class. To register one or more listener you need to set appropriate property.
Listeners API Table
List Interface | Adapter Class | Property | Listener Methods |
---|---|---|---|
QAFDataProviderIntercepter | QAFListenerAdapter | qaf.listeners | beforeFech(TestNGScenario, ITestContext) |
List intercept(TestNGScenario, ITestContext, List<java.lang.Object[]>) | |||
QAFTestStepListener | QAFTestStepAdapter | teststep.listeners or qaf.listeners | onFailure(StepExecutionTracker) |
beforExecute(StepExecutionTracker) | |||
afterExecute(StepExecutionTracker) | |||
QAFWebDriverCommandListener | QAFWebDriverCommandAdapter | wd.command.listeners or qaf.listeners | beforeInitialize(Capabilities) |
onInitialize(QAFExtendedWebDriver) | |||
beforeCommand(QAFExtendedWebDriver, CommandTracker) | |||
afterCommand(QAFExtendedWebDriver, CommandTracker) | |||
onFailure(QAFExtendedWebDriver, CommandTracker) | |||
onInitializationFailure(Capabilities desiredCapabilities, Throwable t) | |||
QAFWebElementCommandListener | QAFWebElementCommandAdapter | we.command.listeners or qaf.listeners | beforeCommand(QAFExtendedWebElement, CommandTracker) |
afterCommand(QAFExtendedWebElement, CommandTracker) | |||
onFailure(QAFExtendedWebElement, CommandTracker) |
There is additional Adapter Class QAFListenerAdapter
that implements all of the qaf listeners. You can extend it, override required methods and register using qaf.listeners
property.
TestStep Listener
In order to create teststep listener, you need to implement listener interface com.qmetry.qaf.automation.step.QAFTestStepListener
or extend adapter class QAFTestStepAdapter
.
QAFTestStepListener
defines following methods:
void beforExecute(StepExecutionTracker stepExecutionTracker); void afterExecute(StepExecutionTracker stepExecutionTracker); void onFailure(StepExecutionTracker stepExecutionTracker);
Creating Listener
public class ThresholdListener extends QAFTestStepAdapter { @Override public void afterExecute(StepExecutionTracker stepExecutionTracker) { long duration = (stepExecutionTracker.getEndTime() - stepExecutionTracker.getStartTime()) / 1000; if (stepExecutionTracker.getStep().getThreshold() > duration) stepExecutionTracker.setVerificationError("Threshold value is exceeded"); } }
Registering listener
To register listener set property teststep.listeners.
For example to register above created listener you need to set property as below:
teststep.listeners=com.qmetry.qaf.listeners.ThresholdListener
WebDriver Listener
In order to create webdriver command listener, you need to implement listener interfaceQAFWebDriverCommandListener
or extend adapter classQAFWebDriverCommandAdapter
.
QAFWebDriverCommandListener
defines following methods:
void beforeInitialize(Capabilities) void beforeCommand(QAFExtendedWebDriver paramQAFExtendedWebDriver, CommandTracker paramCommandTracker); void afterCommand(QAFExtendedWebDriver paramQAFExtendedWebDriver, CommandTracker paramCommandTracker); void onFailure(QAFExtendedWebDriver paramQAFExtendedWebDriver, CommandTracker paramCommandTracker); void onInitialize(QAFExtendedWebDriver driver);
Creating listener
public class WDListener extends QAFWebDriverCommandAdapter { Log logger = LogFactory.getLog(getClass()); @Override public void beforeCommand(QAFExtendedWebDriver driver, CommandTracker commandTracker) { super.beforeCommand(driver, commandTracker); String command = commandTracker.getCommand(); Map<String, Object> params = commandTracker.getParameters(); // support selenium 1 api for navigating from frame to main window // with selenium.selectFrame(""); if (command.equalsIgnoreCase(DriverCommand.SWITCH_TO_FRAME) && StringUtil.isBlank((String) params.get("id"))) { String mainWindow = driver.getWindowHandle(); params.put("id", mainWindow); driver.switchTo().window(mainWindow); // skip original command execution commandTracker.setResponce(new Response()); } if (command.equalsIgnoreCase(DriverCommand.SWITCH_TO_WINDOW)) { String targetWindow = String.valueOf(commandTracker.getParameters().get("name")); String parentHandle = driver.getWindowHandle(); if (!targetWindow.equalsIgnoreCase(parentHandle)) { // store the parent window handle ConfigurationManager.getBundle().setProperty("parentWindowHandel", parentHandle); } } } @Override public void onFailure(QAFExtendedWebDriver driver, CommandTracker commandTracker) { super.onFailure(driver, commandTracker); if (commandTracker.getCommand().equalsIgnoreCase(DriverCommand.SWITCH_TO_WINDOW) && commandTracker.hasException()) { Set<String> windows = driver.getWindowHandles(); String parentWindowHandel = ConfigurationManager.getBundle().getString("parentWindowHandel"); for (String window : windows) { if (!parentWindowHandel.equalsIgnoreCase(window)) { driver.switchTo().window(window); // now we can remove the exception, it is handled! commandTracker.setException(null); break; } } } } }
Registering listener
To register listener set property wd.command.listeners. For example to register above created listener you need to set property as below:
wd.command.listeners= com.ispl.automation.sample.webdriver.WDListener
WebElement Listener
A simple but very useful example of listener is “listener for sendkeys”. As you know in webdriver send keys will append the text box, so each time we need to first call clear command and then use send keys command.
Code Without listener:
firstName.clear(); firstName.sendKeys("fname"); lastName.clear(); firstName.sendKeys("lname");
Code With use of listener:
firstName.sendKeys("fname"); firstName.sendKeys("lname");
Creating listener:
In order to create webelement command listener, you need to implement listener interface QAFWebElementCommandListener
or extend adapter class QAFWebElementCommandAdapter
.
QAFWebElementCommandListener
defines following methods:
void beforeCommand(QAFExtendedWebElement paramQAFExtendedWebElement, CommandTracker paramCommandTracker); void afterCommand(QAFExtendedWebElement paramQAFExtendedWebElement, CommandTracker paramCommandTracker); void onFailure(QAFExtendedWebElement paramQAFExtendedWebElement, CommandTracker paramCommandTracker);
Below example illustrates how we can write a listener that clears element text before sending text.
public class SendKeysListener extends QAFWebElementCommandAdapter { @Override public void beforeCommand(QAFExtendedWebElement element, CommandTracker commandTracker) { if (commandTracker.getCommand().equalsIgnoreCase(DriverCommand.SEND_KEYS_TO_ELEMENT)) { element.clear(); // you access/modify any parameter! String value = String.valueOf(commandTracker.getParameters().get("value")); if (StringUtil.isBlank(value)) { // No need to send key // ignore actual command commandTracker.setResponce(new Response()); } } } }
In this example we had extended adapter class for webelement command listener and provided implementation for beforeCommand
. Same way you can provide implementation for afterCommand
and onFailure
method if required. You can see that one can access parameters in listener and also can modify its value if required.
Registering listener
To register listener set property we.command.listeners. For example to register above created listener you need to set property as below:
we.command.listeners= com.ispl.automation.sample.webdriver.SendKeysListener
TestNG Listeners
One can create and use different kind of TestNG listeners. Please refer TestNG documentation for more information and examples.
In Behave, listeners are often referred as hooks.
Automation Listeners
In resources/application.properties file you can mention your test listener python classes.
e.g.
wd.command.listeners=model.listener.Listener
we.command.listeners=model.listener.ElementListener
There are two types of Automation Listeners
WebDriver Event Listener
WebDriver Event Listener - specify using `wd.command.listeners` key, separated by ';'
Listener class should extend/implement below methods.
def before_command(self, driver, command_tracker): # your implmentation to be performed before command goes here pass def after_command(self, driver, command_tracker): # your implmentation to be performed after command goes here pass def on_exception(self, driver, command_tracker): # your implmentation to be performed when some exception occurs goes here pass
Example of such listener is attached here: wd_listener.py
WebElement Event Listener
WebElement Event Listener - specify using `we.command.listeners` key, separated by ';'
Listener class should extend/implement below methods
def before_command(self, driver, command_tracker): # your implmentation to be performed before command goes here pass def after_command(self, driver, command_tracker): # your implmentation to be performed after command goes here pass def on_exception(self, driver, command_tracker): # your implmentation to be performed when some exception occurs goes here pass
Example of such listener is attached here: we_listener.py
Behave Listeners
One can create and use different kind of Behave listeners. Refer to Behave documentation for more information and examples.
One such listener/hook is environment.py file, which controls the driver management and execution.
Back to QMetry All Products Help Page