How to Close three different Browser Instances using ‘driver.quit()’ ?

 Below code will open three browsers (IE,Firefox and Chrome) and will open URL and finally it closes all the three opened browsers by ‘driver.quit()’ function.

 import java.util.ArrayList;  
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Test
{
private List drivers;
public Test( )
{
this.drivers = new ArrayList();
}
public void CloseAll()
{
for(WebDriver d : drivers)
{
d.quit();
}
}
public static void main(String[] args)
{
Test1 sc = new Test1();
sc.IE("http://google.com");
sc.Firefox("http://google.com");
sc.Chrome("http://google.com");
sc.CloseAll();
}
//Internet Explorer driver
public void IE(String URL)
{
//Set you IEDriver path as per your System Path
System.setProperty("webdriver.ie.driver","S:\\IE Driver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get(URL);
this.drivers.add(driver);
}
//Firefox driver
public void Firefox(String URL)
{
WebDriver driver = new FirefoxDriver();
driver.get(URL);
this.drivers.add(driver);
}
//Chrome driver
public void Chrome(String URL)
{
//Set you ChromeDriver path as per your System Path
System.setProperty("webdriver.chrome.driver","S://Chrome Driver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
this.drivers.add(driver);
}
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s