Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1156ba0

Browse files
js javascript exception capture selenium 4 java console error log
0 parents commit 1156ba0

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

‎.gitignore

Lines changed: 25 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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.BasicAuthentication" -Dexec.classpathScope=test -e

‎README.md

Lines changed: 48 additions & 0 deletions
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.JavaScriptExceptions" -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

Lines changed: 40 additions & 0 deletions
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>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.lambdatest;
2+
3+
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
6+
import java.util.HashMap;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.function.Consumer;
11+
12+
import org.openqa.selenium.By;
13+
import org.openqa.selenium.JavascriptException;
14+
import org.openqa.selenium.JavascriptExecutor;
15+
import org.openqa.selenium.WebDriver;
16+
import org.openqa.selenium.WebElement;
17+
import org.openqa.selenium.devtools.DevTools;
18+
import org.openqa.selenium.devtools.HasDevTools;
19+
import org.openqa.selenium.remote.Augmenter;
20+
import org.openqa.selenium.remote.DesiredCapabilities;
21+
import org.openqa.selenium.remote.RemoteWebDriver;
22+
23+
24+
public class JavaScriptExceptions {
25+
public static String hubURL = "https://hub.lambdatest.com/wd/hub";
26+
static Boolean success = false;
27+
28+
private WebDriver driver;
29+
30+
public void setup() throws MalformedURLException {
31+
DesiredCapabilities capabilities = new DesiredCapabilities();
32+
capabilities.setCapability("browserName", "Chrome");
33+
capabilities.setCapability("browserVersion", "latest");
34+
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
35+
ltOptions.put("user", System.getenv("LT_USERNAME"));
36+
ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY"));
37+
ltOptions.put("build", "Selenium 4");
38+
ltOptions.put("name",this.getClass().getName());
39+
ltOptions.put("platformName", "Windows 10");
40+
ltOptions.put("seCdp", true);
41+
ltOptions.put("selenium_version", "4.0.0");
42+
capabilities.setCapability("LT:Options", ltOptions);
43+
44+
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
45+
System.out.println(driver);
46+
}
47+
48+
49+
public void javaScriptExceptions() throws InterruptedException {
50+
Augmenter augmenter = new Augmenter();
51+
driver = augmenter.augment(driver);
52+
53+
DevTools devTools = ((HasDevTools) driver).getDevTools();
54+
devTools.createSession();
55+
56+
List<JavascriptException> jsExceptionsList = new ArrayList<>();
57+
Consumer<JavascriptException> addEntry = jsExceptionsList::add;
58+
devTools.getDomains().events().addJavascriptExceptionListener(addEntry);
59+
60+
driver.get("https://facebook.com");
61+
62+
WebElement link2click = driver.findElement(By.name("login"));
63+
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);",
64+
link2click, "onclick", "throw new Error('My JS Error');");
65+
link2click.click();
66+
67+
Thread.sleep(1000);
68+
for (JavascriptException jsException : jsExceptionsList) {
69+
System.out.println("My JS exception message: " + jsException.getMessage());
70+
System.out.println("My JS exception system: " + jsException.getSystemInformation());
71+
jsException.printStackTrace();
72+
success = true;
73+
}
74+
Thread.sleep(1000);
75+
if (success) {
76+
markStatus("passed", "Got JS exception", driver);
77+
} else {
78+
markStatus("failed", "Didn't got JS exception", driver);
79+
}
80+
81+
}
82+
83+
public void tearDown() {
84+
try {
85+
driver.quit();
86+
87+
} catch (
88+
89+
Exception e) {
90+
markStatus("failed", "Got exception!", driver);
91+
e.printStackTrace();
92+
driver.quit();
93+
}
94+
}
95+
96+
public static void markStatus(String status, String reason, WebDriver driver) {
97+
JavascriptExecutor jsExecute = (JavascriptExecutor) driver;
98+
jsExecute.executeScript("lambda-status=" + status);
99+
System.out.println(reason);
100+
}
101+
102+
public static void main(String[] args) throws MalformedURLException, InterruptedException {
103+
JavaScriptExceptions test = new JavaScriptExceptions();
104+
test.setup();
105+
test.javaScriptExceptions();
106+
test.tearDown();
107+
}
108+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /