All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.verisoft.fw.async.AsyncTask Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package co.verisoft.fw.async;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;


/**
 * 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.
*
*

* 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 * *

 * {@code 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 * *

 * {@code 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:
*
* *
 * {@code
 * Observer o = new AsyncTask(driver, new SeleniumTask() {
 *             @Override
 *             public boolean doTask() {
 *                 boolean result = false;
 *
 *                 List 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);
 *         }
 * 
* *
*
* * @author Nir Gallner @ www.VeriSoft.co * @since 2.0.1 */ @Slf4j @ToString public class AsyncTask implements Observer { private static final boolean isDebug = true; private final WebDriver driver; private boolean dispose; private final SeleniumTask task; /** * Default c-tor. It is recommended that SeleniumTask object will be implemented * anonymously. see class description for detailed informatio and examples. * * @param driver webDriver instance * @param task async task to perform */ public AsyncTask(WebDriver driver, SeleniumTask task) { dispose = false; this.driver = driver; this.task = task; } @Override public void update() { log.debug("Observer " + this + " has been notified by the Subject"); dispose = task.doTask(); if (!dispose) { log.debug("Observer " + this + "has requested to stop being notified"); } } @Override public boolean isDisposed() { return dispose; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy