Skip to content

Commit 897c791

Browse files
committed
emulate geolocation selenium 4 java cdp
0 parents  commit 897c791

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Compiled class file
2+
*.class
3+
/target/
4+
.vscode/
5+
6+
# Log file
7+
*.log
8+
9+
# BlueJ files
10+
*.ctxt
11+
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.nar
19+
*.ear
20+
*.zip
21+
*.tar.gz
22+
*.rar
23+
24+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
25+
hs_err_pid*

.gitpod.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
2+
tasks:
3+
- init: echo 'init script' # runs during prebuild
4+
command: mvn clean install exec:java -Dexec.mainClass="com.lambdatest.EmulateGeolocation" -Dexec.classpathScope=test -e

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Java-Selenium-Sample
2+
![Java](https://www.lambdatest.com/support/assets/images/og-images/Java-with-Selenium-1-1.jpg)
3+
4+
### Prerequisites
5+
1. Install and set environment variable for java.
6+
* Windows - https://www.oracle.com/java/technologies/downloads/
7+
* Linux - ``` sudo apt-get install openjdk-8-jre ```
8+
* MacOS - Java should already be present on Mac OS X by default.
9+
2 Install and set environment varibale for Maven.
10+
* Windows - https://maven.apache.org/install.html
11+
* Linux/ MacOS - [Homebrew](http://brew.sh/) (Easier)
12+
```
13+
install maven
14+
```
15+
16+
### Run your First Test
17+
1. Clone the Java-Selenium-Sample repository.
18+
```
19+
git clone https://github.com/LambdaTest/java-selenium-sample.git
20+
```
21+
2. Next get into Java-Selenium-Sample folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard.
22+
<p align="center">
23+
<b>For Linux/macOS:</b>:
24+
25+
```
26+
export LT_USERNAME="YOUR_USERNAME"
27+
export LT_ACCESS_KEY="YOUR ACCESS KEY"
28+
```
29+
<p align="center">
30+
<b>For Windows:</b>
31+
32+
```
33+
set LT_USERNAME="YOUR_USERNAME"
34+
set LT_ACCESS_KEY="YOUR ACCESS KEY"
35+
```
36+
Step 3. You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
37+
```
38+
mvn versions:display-dependency-updates
39+
```
40+
Step 4. Run single test.
41+
```
42+
mvn clean install exec:java -Dexec.mainClass="com.lambdatest.EmulateGeolocation" -Dexec.classpathScope=test -e
43+
```
44+
45+
## About LambdaTest
46+
47+
[LambdaTest](https://www.lambdatest.com/) is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your [selenium automation testing](https://www.lambdatest.com/selenium-automation) to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.
48+

pom.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>java-selenium-sample</groupId>
4+
<artifactId>java-selenium-sample</artifactId>
5+
<version>0.0.1</version>
6+
<build>
7+
<sourceDirectory>src/test</sourceDirectory>
8+
<resources>
9+
<resource>
10+
<directory>src/test</directory>
11+
<excludes>
12+
<exclude>**/*.java</exclude>
13+
</excludes>
14+
</resource>
15+
</resources>
16+
<plugins>
17+
<plugin>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.7.0</version>
20+
<configuration>
21+
<release>10</release>
22+
</configuration>
23+
</plugin>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-surefire-plugin</artifactId>
27+
<version>3.0.0-M5</version>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.seleniumhq.selenium</groupId>
34+
<artifactId>selenium-java</artifactId>
35+
<version>4.1.0</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.lambdatest;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Optional;
8+
9+
import org.openqa.selenium.By;
10+
import org.openqa.selenium.JavascriptExecutor;
11+
import org.openqa.selenium.WebDriver;
12+
import org.openqa.selenium.chrome.ChromeOptions;
13+
import org.openqa.selenium.devtools.DevTools;
14+
import org.openqa.selenium.devtools.HasDevTools;
15+
import org.openqa.selenium.devtools.v96.emulation.Emulation;
16+
import org.openqa.selenium.remote.Augmenter;
17+
import org.openqa.selenium.remote.DesiredCapabilities;
18+
import org.openqa.selenium.remote.RemoteWebDriver;
19+
20+
public class EmulateGeolocation {
21+
22+
public static String hubURL = "https://hub.lambdatest.com/wd/hub";
23+
private WebDriver driver;
24+
25+
public void setup() throws MalformedURLException {
26+
DesiredCapabilities capabilities = new DesiredCapabilities();
27+
capabilities.setCapability("browserName", "Chrome");
28+
capabilities.setCapability("browserVersion", "latest");
29+
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
30+
ltOptions.put("user", System.getenv("LT_USERNAME"));
31+
ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY"));
32+
ltOptions.put("build", "Selenium 4");
33+
ltOptions.put("name", this.getClass().getName());
34+
ltOptions.put("platformName", "Windows 10");
35+
ltOptions.put("seCdp", true);
36+
ltOptions.put("selenium_version", "4.0.0");
37+
capabilities.setCapability("LT:Options", ltOptions);
38+
39+
ChromeOptions options = new ChromeOptions();
40+
41+
Map<String, Object> prefs = new HashMap<String, Object>();
42+
prefs.put("googlegeolocationaccess.enabled", true);
43+
prefs.put("profile.default_content_setting_values.geolocation", 1); // 1:allow 2:block
44+
prefs.put("profile.default_content_setting_values.notifications", 1);
45+
prefs.put("profile.managed_default_content_settings", 1);
46+
options.setExperimentalOption("prefs", prefs);
47+
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
48+
49+
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
50+
51+
System.out.println(driver);
52+
}
53+
54+
public void emulateGeolocation() {
55+
Augmenter augmenter = new Augmenter();
56+
driver = augmenter.augment(driver);
57+
58+
DevTools devTools = ((HasDevTools) driver).getDevTools();
59+
devTools.createSession();
60+
61+
// setGeolocationOverride() takes input lattitude, longitude and accuracy as
62+
// parameters.
63+
devTools.send(Emulation.setGeolocationOverride(Optional.of(28.622409),
64+
Optional.of(77.364925),
65+
Optional.of(1)));
66+
driver.get("https://my-location.org");
67+
68+
String address = driver.findElement(By.id("address")).getText();
69+
System.out.println(address);
70+
if (address.contains("Noida")) {
71+
markStatus("passed", "I am in Noida", driver);
72+
} else {
73+
markStatus("failed", "I am not in Noida", driver);
74+
}
75+
76+
}
77+
78+
public void tearDown() {
79+
try {
80+
driver.quit();
81+
82+
} catch (
83+
84+
Exception e) {
85+
markStatus("failed", "Got exception!", driver);
86+
e.printStackTrace();
87+
driver.quit();
88+
}
89+
}
90+
91+
public static void markStatus(String status, String reason, WebDriver driver) {
92+
JavascriptExecutor jsExecute = (JavascriptExecutor) driver;
93+
jsExecute.executeScript("lambda-status=" + status);
94+
System.out.println(reason);
95+
}
96+
97+
public static void main(String[] args) throws MalformedURLException, InterruptedException {
98+
EmulateGeolocation test = new EmulateGeolocation();
99+
test.setup();
100+
test.emulateGeolocation();
101+
test.tearDown();
102+
}
103+
}

0 commit comments

Comments
 (0)