com.nordstrom.automation.selenium.junit.DriverWatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-foundation Show documentation
Show all versions of selenium-foundation Show documentation
Selenium Foundation is an automation framework designed to extend and enhance the capabilities provided by Selenium (WebDriver).
package com.nordstrom.automation.selenium.junit;
import org.junit.internal.runners.model.ReflectiveCallable;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import com.nordstrom.automation.junit.LifecycleHooks;
import com.nordstrom.automation.junit.MethodWatcher;
import com.nordstrom.automation.selenium.annotations.PageUrl;
import com.nordstrom.automation.selenium.core.DriverManager;
import com.nordstrom.automation.selenium.core.GridUtility;
import com.nordstrom.automation.selenium.core.TestBase;
import com.nordstrom.common.base.UncheckedThrow;
/**
* This JUnit watcher performs several basic functions related to driver session management:
*
* - Manage Selenium driver lifetime.
* - For local execution, manage a local instance of Selenium Grid.
* - Store and dispense the driver instance created for the test.
* - Manage configured driver timeout intervals.
* - If an initial page class is specified:
*
* - Open the initial page based on its {@link PageUrl} annotation.
* - Store the page object for subsequent dispensing to the test.
*
*
*
*
* @see GridUtility
*/
public class DriverWatcher implements MethodWatcher {
/**
* {@inheritDoc}
*/
@Override
public void beforeInvocation(final Object runner, final FrameworkMethod method, final ReflectiveCallable callable) {
try {
Object obj = LifecycleHooks.getFieldValue(callable, "val$target");
DriverManager.beforeInvocation(obj, method.getMethod());
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
UncheckedThrow.throwUnchecked(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void afterInvocation(final Object runner, final FrameworkMethod method, final ReflectiveCallable callable, final Throwable thrown) {
try {
Object obj = LifecycleHooks.getFieldValue(callable, "val$target");
DriverManager.afterInvocation(obj, method.getMethod());
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
UncheckedThrow.throwUnchecked(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public Class supportedType() {
return FrameworkMethod.class;
}
/**
* Get test watcher to manage driver instances.
*
* @param obj test class instance extending {@link TestBase}
* @return test watcher object
*/
public static TestWatcher getTestWatcher(final TestBase obj) {
return new TestWatcher() {
@Override
protected void finished(final Description description) {
DriverManager.closeDriver(obj);
}
};
}
}