How to close multiple windows working with Test NG framework

63 views Asked by At

Suppose i have 5 test case and 2nd and 3rd one fails so after statement Assert.assertEquals("http://www.demo.guru99.com/V4/manager/Managerhomepage.php",driver.getCurrentUrl());

driver.close will not work. How to close all the windows failed once too

Trying with window handle too but not getting satisfactory results.

public static WebDriver driver;

@Test(dataProvider="inputdata")
public void login(String val1,String val2){
    System.setProperty("webdriver.chrome.driver","C:/Users/rakesh/Desktop/chromedriver.exe");
    driver =new ChromeDriver();
    driver.get("http://www.demo.guru99.com/V4/");
    driver.findElement(By.name("uid")).sendKeys(val1);
    driver.findElement(By.name("password")).sendKeys(val2);
    driver.findElement(By.name("btnLogin")).click();
    Assert.assertEquals("http://www.demo.guru99.com/V4/manager/Managerhomepage.php",driver.getCurrentUrl());
    driver.close();


}
@DataProvider(name="inputdata")
public Object[][] readExcel() throws BiffException, IOException {
    File f= new File("C:/Users/rakesh/Desktop/selenium/input data/loginTable.xls");
    Workbook w =Workbook.getWorkbook(f);
    Sheet s= w.getSheet("Sheet1");
    int rows=s.getRows();
    int columns=s.getColumns();
    //System.out.println(rows);
    //System.out.println(columns);

    String inputData [][] = new String[rows][columns];
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            Cell c=s.getCell(j,i);
            inputData[i][j]=c.getContents();
            //System.out.println(inputData[i][j]);
        }
    }
    return inputData;


}
1

There are 1 answers

0
Naman On

One way is to use current junit version 4.12 and declare @After method which would execute after every @Test method -

Method that is marked with @After gets executed after execution of every test.

@After
public void closeConnection(){
    driver.close();
}

But then this would also close the connection even if the test passes. So to implement actions based on test results you can try using the TestWatcher for Junit accordingly.