├── .gitignore
├── .gitpod.yml
├── README.md
├── pom.xml
└── src
└── test
└── com
└── lambdatest
└── CapturePerformanceMetrics.java
/.gitignore:
--------------------------------------------------------------------------------
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*
26 |
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
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.CapturePerformanceMetrics" -Dexec.classpathScope=test -e
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java-Selenium-Sample
2 | 
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 |
23 | For Linux/macOS::
24 |
25 | ```
26 | export LT_USERNAME="YOUR_USERNAME"
27 | export LT_ACCESS_KEY="YOUR ACCESS KEY"
28 | ```
29 |
30 | For Windows:
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.CapturePerformanceMetrics" -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 |
49 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | java-selenium-sample
4 | java-selenium-sample
5 | 0.0.1
6 |
7 | src/test
8 |
9 |
10 | src/test
11 |
12 | **/*.java
13 |
14 |
15 |
16 |
17 |
18 | maven-compiler-plugin
19 | 3.7.0
20 |
21 | 10
22 |
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-surefire-plugin
27 | 3.0.0-M5
28 |
29 |
30 |
31 |
32 |
33 | org.seleniumhq.selenium
34 | selenium-java
35 | 4.1.0
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/test/com/lambdatest/CapturePerformanceMetrics.java:
--------------------------------------------------------------------------------
1 | package com.lambdatest;
2 |
3 | import java.net.MalformedURLException;
4 | import java.net.URL;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Optional;
8 |
9 | import org.openqa.selenium.JavascriptExecutor;
10 | import org.openqa.selenium.WebDriver;
11 | import org.openqa.selenium.devtools.DevTools;
12 | import org.openqa.selenium.devtools.HasDevTools;
13 | import org.openqa.selenium.devtools.v94.performance.Performance;
14 | import org.openqa.selenium.devtools.v94.performance.model.Metric;
15 | import org.openqa.selenium.remote.Augmenter;
16 | import org.openqa.selenium.remote.DesiredCapabilities;
17 | import org.openqa.selenium.remote.RemoteWebDriver;
18 |
19 | public class CapturePerformanceMetrics {
20 | public static String hubURL = "https://hub.lambdatest.com/wd/hub";
21 |
22 | static Boolean success = false;
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 ltOptions = new HashMap();
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 | driver = new RemoteWebDriver(new URL(hubURL), capabilities);
40 | System.out.println(driver);
41 | }
42 |
43 | public void capturePerformanceMetrics() {
44 | Augmenter augmenter = new Augmenter();
45 | driver = augmenter.augment(driver);
46 |
47 | DevTools devTools = ((HasDevTools) driver).getDevTools();
48 | devTools.createSession();
49 |
50 | devTools.send(Performance.enable(Optional.empty()));
51 | List metricList = devTools.send(Performance.getMetrics());
52 |
53 | driver.get("https://lambdatest.com");
54 |
55 | for (Metric m : metricList) {
56 | System.out.println(m.getName() + " = " + m.getValue());
57 | success = true;
58 | }
59 | if (success) {
60 | markStatus("passed", "Performance metrics successfully fetched", driver);
61 | } else {
62 | markStatus("failed", "Unable to fetch Performance metrics", driver);
63 | }
64 | }
65 |
66 | public void tearDown() {
67 | try {
68 | driver.quit();
69 |
70 | } catch (
71 |
72 | Exception e) {
73 | markStatus("failed", "Got exception!", driver);
74 | e.printStackTrace();
75 | driver.quit();
76 | }
77 | }
78 |
79 | public static void markStatus(String status, String reason, WebDriver driver) {
80 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver;
81 | jsExecute.executeScript("lambda-status=" + status);
82 | System.out.println(reason);
83 | }
84 |
85 | public static void main(String[] args) throws MalformedURLException, InterruptedException {
86 | CapturePerformanceMetrics test = new CapturePerformanceMetrics();
87 | test.setup();
88 | test.capturePerformanceMetrics();
89 | test.tearDown();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------