Skip to content

Commit 657865b

Browse files
committed
[Java] Simple demo of Selenium Manager
1 parent a8bf008 commit 657865b

File tree

8 files changed

+288
-2
lines changed

8 files changed

+288
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 LambdaTest
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# selenium-manager-demo
2-
This simple repo shows how to use Selenium Manager (in Selenium 4.7) with Java
1+
This simple example demonstrates how to use Selenium Manager in Selenium 4.6 (and above). For the demonstration, I have made use of Selenium 4.7.
2+
3+
## What is Selenium Manager
4+
5+
Selenium Manager is a new tool that helps to get a working environment to run Selenium out of the box.
6+
7+
Available with Selenium v4.6 (and above), it automatically configures the browser drivers for Chrome, Firefox, Internet Explorer, and Edge.
8+
9+
More information about Selenium Manager is available in the following locations:
10+
11+
* [Introducing Selenium Manager](https://www.selenium.dev/blog/2022/introducing-selenium-manager/)
12+
* [Selenium Manager GitHub Repo](https://github.com/SeleniumHQ/selenium/tree/trunk/common/manager)
13+
* [Selenium Manger in Java](https://github.com/SeleniumHQ/selenium/commit/eecdacae2d83b611cb9c089f7d3cb0220218b78e)
14+
15+
## How to use Selenium Manager in Java
16+
17+
* Add [Selenium Manager dependency](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-manager/4.7.0) in pom.xml
18+
19+
<img width="459" alt="Selenium-Manager-POM" src="https://user-images.githubusercontent.com/1688653/207520606-b6d87531-3341-4aab-a31f-b979deeeb1a5.png">
20+
21+
* Add [Selenium Java 4.7.0](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/4.7.0) dependency in pom.xml
22+
23+
<img width="459" alt="Selenium-Java-POM" src="https://user-images.githubusercontent.com/1688653/207520702-f7140f2f-1791-481d-a538-2a6eecc9966d.png">
24+
25+
## Execution
26+
27+
Here is all what is needed to instantiate the browsers:
28+
29+
* Chrome ------> WebDriver driver = new ChromeDriver();
30+
* Firefox ------> WebDriver driver = new FirefoxDriver();
31+
* Edge --------> WebDriver driver = new EdgeDriver();
32+
33+
For demonstration, I used the *getInstance* method in *SeleniumManager* to print the location where the respective browser drivers are downloaded:
34+
35+
<img width="1420" alt="VS_IDE_Selenium_Manager_2" src="https://user-images.githubusercontent.com/1688653/207524076-dfa57992-93fb-460b-9ee5-1d4d85cd6fcc.png">
36+
37+
On execution, the browser drivers are downloaded in */Users/applemacbook_pro/.cache/selenium* folder. These would be re-downloaded in case there is any update in the browser version.
38+
39+
<img width="728" alt="CMD_Selenium_Manager" src="https://user-images.githubusercontent.com/1688653/207521761-bcebdaa1-e30a-4743-8b62-8d350d98ab3f.png">

pom.xml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.example</groupId>
7+
<artifactId>TestNG_Demo</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.testng</groupId>
18+
<artifactId>testng</artifactId>
19+
<version>7.6.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.slf4j</groupId>
24+
<artifactId>slf4j-nop</artifactId>
25+
<version>1.7.28</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.seleniumhq.selenium</groupId>
30+
<artifactId>selenium-java</artifactId>
31+
<version>4.7.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.seleniumhq.selenium</groupId>
35+
<artifactId>selenium-chrome-driver</artifactId>
36+
<version>4.7.0</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>4.12</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.httpcomponents</groupId>
46+
<artifactId>httpclient</artifactId>
47+
<version>4.5.13</version>
48+
</dependency>
49+
<!-- https://mvnrepository.com/artifact/com.github.sdrss/reportng -->
50+
<dependency>
51+
<groupId>org.uncommons</groupId>
52+
<artifactId>reportng</artifactId>
53+
<version>1.1.4</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.inject</groupId>
57+
<artifactId>guice</artifactId>
58+
<version>4.2.2</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>3.0.0-M5</version>
64+
<type>maven-plugin</type>
65+
</dependency>
66+
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
67+
<dependency>
68+
<groupId>io.github.bonigarcia</groupId>
69+
<artifactId>webdrivermanager</artifactId>
70+
<version>5.3.0</version>
71+
</dependency>
72+
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-manager -->
73+
<dependency>
74+
<groupId>org.seleniumhq.selenium</groupId>
75+
<artifactId>selenium-manager</artifactId>
76+
<version>4.7.0</version>
77+
</dependency>
78+
79+
</dependencies>
80+
81+
<build>
82+
<defaultGoal>install</defaultGoal>
83+
<plugins>
84+
<plugin>
85+
<artifactId>maven-compiler-plugin</artifactId>
86+
<version>3.7.0</version>
87+
<configuration>
88+
<source>1.8</source>
89+
<target>1.8</target>
90+
</configuration>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-surefire-plugin</artifactId>
95+
<version>3.0.0-M5</version>
96+
<configuration>
97+
<properties> <property>
98+
<name>usedefaultlisteners</name>
99+
<value>false</value>
100+
</property>
101+
<property>
102+
<name>listener</name>
103+
<value>org.uncommons.reportng.HTMLReporter,
104+
org.uncommons.reportng.JUnitXMLReporter
105+
</value>
106+
</property> </properties>
107+
<suiteXmlFiles>
108+
<!-- TestNG suite XML files -->
109+
<suiteXmlFile>testng.xml</suiteXmlFile>
110+
</suiteXmlFiles>
111+
</configuration>
112+
</plugin>
113+
<plugin>
114+
<groupId>org.apache.maven.plugins</groupId>
115+
<artifactId>maven-surefire-report-plugin</artifactId>
116+
<version>3.0.0-M5</version>
117+
</plugin>
118+
</plugins>
119+
</build>
120+
</project>

src/.DS_Store

6 KB
Binary file not shown.

src/test/.DS_Store

6 KB
Binary file not shown.

src/test/java/.DS_Store

6 KB
Binary file not shown.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import org.openqa.selenium.WebDriver;
2+
import org.openqa.selenium.chrome.ChromeDriver;
3+
import org.openqa.selenium.edge.EdgeDriver;
4+
import org.openqa.selenium.firefox.FirefoxDriver;
5+
import org.testng.annotations.Test;
6+
import org.openqa.selenium.manager.SeleniumManager;
7+
8+
public class SeleniumManagerDemo
9+
{
10+
protected static ThreadLocal<WebDriver> ThreadLocal
11+
= new ThreadLocal<WebDriver>();
12+
13+
String testURL = "https://www.lambdatest.com";
14+
15+
@Test(description="Demonstration of Toggle Buttons on LambdaTest Selenium Grid")
16+
public void testEdge() throws InterruptedException {
17+
/* Selenium Manager - MS Edge */
18+
/* [Start] Optional - Printing path of browser driver */
19+
/*
20+
SeleniumManager manager = SeleniumManager.getInstance();
21+
String path = manager.getDriverPath("msedgedriver");
22+
System.out.println(path);
23+
*/
24+
/* [End] Optional - Printing path of browser driver */
25+
26+
/* Voila, only this line is sufficient */
27+
28+
WebDriver driver = new EdgeDriver();
29+
30+
ThreadLocal.set(driver);
31+
32+
driver.get(testURL);
33+
34+
Thread.sleep(3000);
35+
36+
driver.quit();
37+
ThreadLocal.remove();
38+
39+
Thread.sleep(5000);
40+
}
41+
42+
@Test(description="Demonstration of Toggle Buttons on LambdaTest Selenium Grid")
43+
public void testFirefox() throws InterruptedException {
44+
/* Selenium Manager - Firefox */
45+
/* [Start] Optional - Printing path of browser driver */
46+
/*
47+
SeleniumManager manager = SeleniumManager.getInstance();
48+
String path = manager.getDriverPath("geckodriver");
49+
System.out.println(path);
50+
*/
51+
/* [End] Optional - Printing path of browser driver */
52+
53+
/* Voila, only this line is sufficient */
54+
55+
WebDriver driver = new FirefoxDriver();
56+
ThreadLocal.set(driver);
57+
58+
driver.get(testURL);
59+
60+
Thread.sleep(3000);
61+
62+
driver.quit();
63+
ThreadLocal.remove();
64+
65+
Thread.sleep(5000);
66+
}
67+
68+
@Test(description="Demonstration of Toggle Buttons on LambdaTest Selenium Grid")
69+
public void testChrome() throws InterruptedException {
70+
/* Selenium Manager - Chrome */
71+
/* [Start] Optional - Printing path of browser driver */
72+
/*
73+
SeleniumManager manager = SeleniumManager.getInstance();
74+
String path = manager.getDriverPath("chromedriver");
75+
System.out.println(path);
76+
*/
77+
/* [End] Optional - Printing path of browser driver */
78+
79+
/* Voila, only this line is sufficient */
80+
81+
WebDriver driver = new ChromeDriver();
82+
83+
ThreadLocal.set(driver);
84+
85+
driver.get(testURL);
86+
87+
Thread.sleep(3000);
88+
89+
driver.quit();
90+
ThreadLocal.remove();
91+
92+
Thread.sleep(5000);
93+
}
94+
}

testng.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="[Demo] Selenium Manager" parallel="methods" thread-count="3">
4+
<listeners>
5+
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
6+
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
7+
</listeners>
8+
9+
<test name="[Demo] Selenium Manager">
10+
<classes>
11+
<class name="SeleniumManagerDemo"> </class>
12+
</classes>
13+
</test>
14+
</suite>

0 commit comments

Comments
 (0)