Package co.verisoft.fw.async
Class AsyncTask
java.lang.Object
co.verisoft.fw.async.AsyncTask
- All Implemented Interfaces:
Observer
Defines an async set of operations to be executed during the test, in an
async manner.
Important: tasks are executed in the same thread as the WebDriver main thread. It means that:
1. You should avoid long async operations, or use Wait Until in your async operation. It will block the main thread and prevents execution.
2. The async code will not be performing context switching - the code will run from start to finish once it is invoked. and,
3. The async code uses the same driver as the test. You should consider it when wrapping up the async code - bring the driver back to the position where it can continue the test from the point it was paused.
Example #2
The following code waits for a popup to appear on the screen, and after it appears, click on dismiss and then asks to be unregistered from further executions. It uses the default dispatch interval, which is 1 second:
Important: tasks are executed in the same thread as the WebDriver main thread. It means that:
1. You should avoid long async operations, or use Wait Until in your async operation. It will block the main thread and prevents execution.
2. The async code will not be performing context switching - the code will run from start to finish once it is invoked. and,
3. The async code uses the same driver as the test. You should consider it when wrapping up the async code - bring the driver back to the position where it can continue the test from the point it was paused.
Further practices to consider:
1. By default, the script is set to run each time a driver.findElement() is
invoked. You should change it to a different dispatch time. For example, to
change the dispatch time to 2 seconds use
driver.async().setDispatchInterval(2, ChronoUnit.SECONDS);
2. doTask() returns a boolean value. True, means the async task is done and
it will not be invoked again. False means the async task is not done (e.g
failed to locate an elements) and should be invoked next time as well.
3. Don't forget to register the observer with
driver.async().register(o);
otherwise it will not be invoked at all.
Example #1
The following code example creates an async script to get and print the page
title to the STDIO, and then asks to be unregistered from further
executions.
{ @code Observer o = new AsyncTask(driver, new SeleniumTask() { @Override public boolean doTask() { String pageTitle = driver.getCurrentUrl(); System.out.println("Time is " + LocalTime.now() + ", page url is " + pageTitle); return false; } }); driver.async().setDispatchInterval(2, ChronoUnit.SECONDS); driver.async().register(o); }
Example #2
The following code waits for a popup to appear on the screen, and after it appears, click on dismiss and then asks to be unregistered from further executions. It uses the default dispatch interval, which is 1 second:
Observer o = new AsyncTask(driver, new SeleniumTask() {
@Override
public boolean doTask() {
boolean result = false;
List<WebElement> elements = driver.findElements(By.id("popup-button-dismiss-locator"));
if (elements.isEmpty())
return false;
try{
elements.get(0).click();
result = true;
}
catch(Throwable t)
result = false;
return result;
}
});
driver.async().register(o);
- Since:
- 2.0.1
- Author:
- Nir Gallner @ www.VeriSoft.co
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncTask
(org.openqa.selenium.WebDriver driver, SeleniumTask task) Default c-tor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Inform the subject whether the observer wish to remain on the notification list or would like to be removed from it.void
update()
An action to be performed once the subject has triggered the notify method.
This update should hold the action to be performed, typically the sequence of actions needed to be executed asynchroniously.
-
Constructor Details
-
AsyncTask
Default c-tor. It is recommended that SeleniumTask object will be implemented anonymously. see class description for detailed informatio and examples.- Parameters:
driver
- webDriver instancetask
- async task to perform
-
-
Method Details
-
update
public void update()Description copied from interface:Observer
An action to be performed once the subject has triggered the notify method.
This update should hold the action to be performed, typically the sequence of actions needed to be executed asynchroniously. Each invocation of the subject's notify method fires up this update.
Tips:
1. Since Selenium WebDriver does not support multithread operations, the async operation runs on the main thread. So if you block it with long waits, the main thread will get blocked. Make sure the async script is as small and thin as possible to avoid blocking the main thread.
2. Since there is no real context switching between the main activity and the async script, make sure you save the necessary information of the driver states (e.g - current url) to leave things to be continuted once you are done with your async actions. -
isDisposed
public boolean isDisposed()Description copied from interface:Observer
Inform the subject whether the observer wish to remain on the notification list or would like to be removed from it. Typically, the observer should hold a private field which initially is false, and changes to true once once the observer has finished his task. Since an observer cannot register itself with the subject, and cannot signal a subject that it wished to be included in the notification list, once an observer is removed from the notification list, it cannot be re-register. So default this method to false at first, and change it to true only when the observer has finished his role. For example - an observer for dismissing an automatic pop up of a page. Once the pop up has been dismissed, the observer has filled his purpose and is no longer necessary- Specified by:
isDisposed
in interfaceObserver
- Returns:
- true if the subject should remove the observer from the notification list, false otherwise
-