How to close a window after running each test case in Selenium IntelliJ? -
i have set of 3 test cases, , want avoid keeping open multiple browser windows since automated process in selenium. there way close browser after each test case finished without giving error?
using close() , quit() both give me error codes of 1.
we use below approach handle these issue.
1) create base class has beforesuite , beforetest , aftertest , aftersuite methods run always.
2) each test plan should extend class create driver , close driver.
basepage.java
package com.test.test3; import java.lang.reflect.method; import java.util.date; import org.openqa.selenium.webdriver; import org.testng.annotations.aftermethod; import org.testng.annotations.aftersuite; public class basepage { public webdriver driver = null; private date start; /* * below method initialize driver once test method started * execution */ public void initializedriver(webdriver driver) { this.driver = driver; } /* * below method kill driver */ public void teardown() { if (this.driver != null) { this.driver.quit(); } } @aftermethod(alwaysrun = true) public void aftertestmethod(method method) { // clean ups test level services teardown(); } @aftersuite(alwaysrun = true) public void aftertestsuite() { teardown(); } }
testplan.java
package com.test.test3; import org.openqa.selenium.firefox.firefoxdriver; import org.testng.annotations.test; public class testplan extends basepage{ @test(groups = { "test"}) public void test() { firefoxdriver driver = new firefoxdriver(); driver.get("http://www.google.com"); system.out.println("testautomation test"); } @test(groups = { "test"}) public void test1() { firefoxdriver driver = new firefoxdriver(); driver.get("http://www.google.com"); system.out.println("testautomation test"); } @test(groups = { "test"}) public void test2() { firefoxdriver driver = new firefoxdriver(); driver.get("http://www.google.com"); system.out.println("testautomation test"); } }
testng.xml
<suite name="api test cases"> <test name="api test" parallel="methods"> <groups> <run> <include name="test" /> </run> </groups> <classes> <class name="com.test.test3.testplan" /> </classes> </test>
Comments
Post a Comment