Hello all I have a TestNG test suite which executes tests sequentially. But before it starts running tests. It opens the required number of browsers and then run test one after other . I want to change this behaviour to open one browser, run test/s and close the browser. Following from that open another browser, run test/s and close and so on. Is this possible?
I use TestNG, JAVA on IntelliJ. Sample test suite:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" parallel="false" preserve-order="true" thread-count="1"> <test name="something1" group-by-instances="true"> <classes> <class name="com.this.is.First"/> </classes> </test> <test name="something2" group-by-instances="true"> <classes> <class name="com.this.is.Second"/> </classes> </test> <test name="something3" group-by-instances="true"> <classes> <class name="com.this.is.Third"/> </classes> </suite> All the test inherit Main class which has @BeforeClass and @AfterClass methods where I instantiate the browser.
Each Class can have multiple tests or sometimes only one.
Main class as requested:
public class MainBeforeAfter { protected WebDriver driver; protected String testUrl = "someurl"; public MainBeforeAfter() { if (System.getProperty("webdriver.chrome.driver") == null) { System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe"); } if (System.getProperty("test.url") != null) { testUrl = System.getProperty("test.url"); } System.out.println(System.getProperty("webdriver.chrome.driver")); ChromeOptions options = new ChromeOptions(); options.addArguments("headless"); options.addArguments("window-size=1200x600"); driver = new ChromeDriver(options); //driver = new ChromeDriver(); } @BeforeClass public void beforeClass() throws InterruptedException { driver.get(testUrl); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); enterLogin(); } protected void enterLogin() { driver.findElement(By.name("username")).sendKeys("user"); driver.findElement(By.name("password")).sendKeys("password"); driver.findElement(By.name("password")).sendKeys(Keys.ENTER); } @AfterClass public void afterClass() throws InterruptedException { driver.close(); try { driver.quit(); } catch (Exception e) { System.out.println("Unable to close browser after login header exception caught: " + e); } } }