If I have 10 classes in testng.xml, it will open 10 browser sessions. How can I initialize the driver once and only open one browser for one test case then close and open a window again for the second test case and so on?
I have my driver setup code in a constructor, which is maybe a bad way to approach it? How can I initialize it in TNGDriver class and then use it in Testcase1 class?
I tried using @BeforeClass and have a setUp method but that did not work.
TNGDriver class
public abstract class TNGDriver {
public static WebDriver driver;
private static String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe";
@SuppressWarnings("deprecation")
public TNGDriver() {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
driver = new ChromeDriver(capabilities);
driver.manage().window().maximize();
}
public static WebDriver getDriver() {
return driver;
}
public static void setDriver(WebDriver driver) {
TNGDriver.driver = driver;
}
Testcase1 class
public class Testcase1 extends Registration {
@Test(priority = 1)
public void step1_checkSomething() {
//do something
}
@Test(priority = 2)
public void step2_clickOnSomething() {
//click on something
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="AutomationFramework" parallel="false" preserve-order="true">
<test name="Registration">
<classes>
<class name="regression.Testcase01" />
<class name="regression.Testcase02" />
</classes>
</test>
</suite>
Chrome Driver opens every time private window by default, You don't need this really.
//options.addArguments("--incognito");
You can make structure like below :
public class TNGDriver {
public static WebDriver driver;
private static String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe";
public void DriverConfiguration() {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
driver = new ChromeDriver(capabilities);
driver.manage().window().maximize();
}
public void QuitDriver(){
driver.quit();
}
}
Unit Test cases:
public class Testcase1 extends Registration {
TNGDriver objTND = new TNGDriver();
@BeforeTest
public void initializeDriver(){
objTND.DriverConfiguration();
}
@Test(priority = 1)
public void step1_checkSomething() {
//do something
}
@Test(priority = 2)
public void step2_clickOnSomething() {
//click on something
}
@AfterTest
public void initializeDriver(){
objTND.QuitDriver();
}
}
If you want to use browser(to open) before each @Test, You can use this same method with @BeforeMethod annotation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.