Collections:
Load WebDriver for Different Browsers in Java
How to Load WebDriver for Different Browsers Dynamically in Java?
✍: FYIcenter.com
You can write a Java program with Selenium Client Java API to
Load WebDriver for Different Browsers Dynamically.
1. Make sure you have browser drivers installed locally for different browsers:
C:\fyicenter> dir \fyicenter\selenium <DIR> chromedriver <DIR> edgedriver <DIR> geckodriver
2. Enter the following program, WebDriverLoader.java, that offers static method for other Java programs to load WebDriver by browser name:
C:\fyicenter> type WebDriverLoader.java
// WebDriverLoader.java
// Copyright (c) FYIcenter.com
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.edge.*;
public class WebDriverLoader {
public static void main(String[] args) {
WebDriver driver = load(args[0]);
driver.get("http://www.google.com");
driver.quit();
}
public static WebDriver load(String name) {
WebDriver driver = null;
if (name.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver",
"\\fyicenter\\selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
} else if (name.equalsIgnoreCase("Edge")) {
System.setProperty("webdriver.edge.driver",
"\\fyicenter\\selenium\\edgedriver\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
} else {
System.out.println("ERROR: Invalid browser name: "+name);
}
return driver;
}
}
3. Compile and run it with the Selenium Client JAR file:
C:\fyicenter> javac -classpath \
\fyicenter\selenium\java\client-combined-3.141.59.jar \
WebDriverLoader.java
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
WebDriverLoader Chrome
Starting ChromeDriver 75.0.3770.8 (...-refs/branch-heads/3770@{#40}) on port 44341
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks
to prevent access by malicious code.
5:59:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
WebDriverLoader Chrome
[17:59:39.816] - Listening on http://localhost:28824/
5:59:42 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[17:59:45.003] - Stopping server.
As you can see, the WebDriverLoader.java works nicely.
⇒ Retrieve Web Page Title with WebDriver in Java
⇐ Using Selenium WebDriver Client Java API
2020-01-21, 1928🔥, 0💬
Popular Posts:
How to generate passwords? To help you to obtain some good passwords for testing purpose, FYIcenter....
How to validate domain name format? In order to help your programming or testing tasks, FYIcenter.co...
In what order thread groups are executed in JMeter? Thread groups, including regular "Thread Groups"...
Where to find online test tools? FYIcenter.com has prepared a number of online test tools: Data Look...
How to generate date and time test values? Timestamp values are frequently needed in testing almost ...