Tuesday, October 1, 2024

Run two browsers simultaneously in Selenium

 package TEST;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;


public class Selenium_Latest {


public static void main(String[] args) {

// Create and start two threads, one for each browser.

Thread chromeThread = new Thread(new BrowserRunnable("chrome"));

Thread firefoxThread = new Thread(new BrowserRunnable("firefox"));


chromeThread.start();

firefoxThread.start();

}


}


// Runnable class for launching a browser

class BrowserRunnable implements Runnable {

private String browserType;


public BrowserRunnable(String browserType) {

this.browserType = browserType;

}


@Override

public void run() {

WebDriver driver = null;


if (browserType.equals("chrome")) {

driver = new ChromeDriver();

} else if (browserType.equals("firefox")) {

driver = new FirefoxDriver();

}


if (driver != null) {

driver.get("https://suriyaparithy.blogspot.com/");


// Optionally, get the title and print it for each browser

String title = driver.getTitle();

System.out.println(browserType + " Browser - Page Title: " + title);


// Closing the browser after operation

driver.quit();

}

}

}

No comments:

Post a Comment