26
Jun

Multithreaded initializing of browsers with Selenium web driver

Internet Explorer Driver

InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol. This driver has been tested with IE 7, 8, 9, 10, and 11 on appropriate combinations of Vista, Windows 7, Windows 8, and Windows 8.1.

(Note: IE 6 is no longer supported)

Installing

You do not need to run an installer before using the InternetExplorerDriver, though some configuration is required. The standalone server executable must be downloaded from the Downloads page and placed in your PATH.

download location: http://selenium-release.storage.googleapis.com/index.html

public class GoogleSearchMultiThread extends Thread {

private WebDriver driver;
private String baseUrl;
private String browsertype;

public GoogleSearchMultiThread(String name, String browsertype) {
super(name);
this.browsertype = browsertype;
}

@Override
public void run() {
System.out.println(“Thread- Started” + Thread.currentThread().getName());

try {
Thread.sleep(1000);
setUp(this.browsertype);
testGoogleSearch();
} catch (InterruptedException e) {
System.out.println(“Interrupted Exception details {} ” + e.getMessage());
} catch (Exception e) {
System.out.println(“Exception details {} ” + e.getMessage());
} finally {

}
System.out.println(“Thread- END ” + Thread.currentThread().getName());
//tearDown();
}

// main method to create thread and run multiple thread
public static void main(String[] args) {
Thread t1 = new GoogleSearchMultiThread(“Thread Firefox”, “Firefox”);
Thread t2 = new GoogleSearchMultiThread(“Thread IE”, “IE”);
System.out.println(“Starting MyThreads”);
t1.start();
t2.start();

System.out.println(“Thread has been started”);
}

// set up method to initialize driver object
public void setUp(String browsertype) throws Exception {
if (browsertype.contains(“IE”)) {
File IEDriver = new File(“C:\\softwares\\Selenium jars\\IEDriverServer.exe”);
System.setProperty(“webdriver.ie.driver”, IEDriver.getAbsolutePath());
driver = new InternetExplorerDriver();
} else if (browsertype.contains(“Firefox”)) {
File FFDriver = new File(“C:\\softwares\\Selenium jars\\geckodriver.exe”);
System.setProperty(“webdriver.firefox.marionette”,FFDriver.getAbsolutePath());
driver = new FirefoxDriver();
}

baseUrl = “https://www.google.co.in/”;
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

// test case
public void testGoogleSearch() throws Exception {

driver.get(baseUrl);
driver.findElement(By.id(“lst-ib”)).clear();
driver.findElement(By.id(“lst-ib”)).sendKeys(“Testing”);
driver.findElement(By.name(“btnK”)).click();
}

// tear down function to close browser
public void tearDown() {
driver.quit();
}

}