Saturday, 18 February 2017

Selenium Webdriver 3 - Closing the browser/browsers using close()

1. Closing Single browser using driver.close().

package shirageri.blog;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HelloBrowser3 {

public static void main(String[] args) throws InterruptedException {

//Step 1 - Open empty browser
WebDriver driver  = new FirefoxDriver();

//Step 2 -Wait for 5 sec before clossing opened browser
Thread.sleep(1000);

//Step 3 - Close the opened browser
driver.close();


}
}
Note: 
1. Thread.sleep(milisec) - this method makes the running thread to sleep for mentioned timeout. The reason behind using this here is for understandability. 


2. Closing Multiple browsers using driver.close().

package shirageri.blog;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HelloBrowser4 {

public static void main(String[] args) throws InterruptedException {

//Step 1 - Open 1st empty browser
WebDriver driver1  = new FirefoxDriver();
//Step 2 - Open 2nd empty browser
WebDriver driver2  = new FirefoxDriver();

//Step 3 -Wait for 5 sec before closing 1st browser
Thread.sleep(1000);
//Step 4 - Close the opened browser
driver1.close();
//Step 5 -Wait for 5 sec before closing 2nd browser browser
Thread.sleep(1000);
//Step 5 - Close 2nd browser
driver2.close();
}
}

Note: If you created driver object as an array you can use any looping technique to close it.


No comments:

Post a Comment