Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created October 25, 2015 05:34
Show Gist options
  • Save krmahadevan/4ad83bc5675489a397d0 to your computer and use it in GitHub Desktop.
Save krmahadevan/4ad83bc5675489a397d0 to your computer and use it in GitHub Desktop.
This example shows how to work with web driver instances that were created at suite level
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
@Listeners (SuiteLevelWebdriverSample.CustomSuiteListener.class)
public class SuiteLevelWebdriverSample {
@Test
public void testMethod() {
String currentSuiteName = Reporter.getCurrentTestResult().getTestContext().getSuite().getName();
WebDriver driver = CustomSuiteListener.getDriver(currentSuiteName);
if (driver == null) {
throw new IllegalStateException("Webdriver instantiation failed.");
}
driver.get("http://www.facebook.com");
}
public static class CustomSuiteListener implements ISuiteListener {
public static Map<String, WebDriver> webdriverMap = new HashMap<>();
public static WebDriver getDriver(String suiteName) {
return webdriverMap.get(suiteName);
}
@Override
public void onStart(ISuite suite) {
String suiteName = suite.getName();
webdriverMap.put(suiteName, new FirefoxDriver());
}
@Override
public void onFinish(ISuite suite) {
String suiteName = suite.getName();
WebDriver driver = getDriver(suiteName);
if (driver != null) {
driver.quit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment