├── .gitignore ├── .gitpod.yml ├── README.md ├── cloudbuild.yaml ├── pom.xml ├── single.xml └── src └── test └── java └── com └── lambdatest └── CapturePerformanceMetrics.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /bin/ 3 | .project 4 | .settings 5 | .theia 6 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | # List the ports you want to expose and what to do when they are served. See https://www.gitpod.io/docs/config-ports/ 2 | ports: 3 | - port: 3000 4 | onOpen: open-preview 5 | 6 | # List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/ 7 | tasks: 8 | - init: mvn install -Dsuite=single.xml -Dtestng.dtd.http=true # runs during prebuild 9 | command: echo 'Please check your test on https://automation.lambdatest.com/' 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to capture performance logs in Java-TestNG using CDP on LambdaTest 2 | 3 | ### Environment Setup 4 | 5 | 1. Global Dependencies 6 | * Install [Maven](https://maven.apache.org/install.html) 7 | * Or Install Maven with [Homebrew](http://brew.sh/) (Easier) 8 | ``` 9 | $ install maven 10 | ``` 11 | 2. Project Dependencies 12 | * checkout the repository 13 | * Check that packages are available 14 | ``` 15 | $ cd Java-TestNG-Selenium 16 | ``` 17 | * 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. 18 | ``` 19 | $ mvn versions:display-dependency-updates 20 | ``` 21 | 22 | **LambdaTest Authentication Credentials:** Make sure you have your LambdaTest credentials with you to run test automation scripts with Jest on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/) or through [LambdaTest Profile](https://accounts.lambdatest.com/detail/profile). 23 | 24 | Set LambdaTest Username and Access Key in environment variables. 25 | 26 | * For Linux/macOS: 27 | ``` 28 | $ export LT_USERNAME="YOUR_USERNAME" 29 | $ export LT_ACCESS_KEY="YOUR ACCESS KEY" 30 | ``` 31 | 32 | * For Windows: 33 | ``` 34 | $ set LT_USERNAME="YOUR_USERNAME" 35 | $ set LT_ACCESS_KEY="YOUR ACCESS KEY" 36 | ``` 37 | ### View performance metrics 38 | To view performance metrics, the following code can be used: 39 | ```java 40 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 41 | devTools.createSession(); 42 | 43 | devTools.send(Performance.enable(Optional.empty())); 44 | List metricList = devTools.send(Performance.getMetrics()); 45 | 46 | 47 | for (Metric m : metricList) { 48 | System.out.println(m.getName() + " = " + m.getValue()); 49 | success = true; 50 | } 51 | ``` 52 | ### Running Tests 53 | 54 | ``` 55 | To run single test 56 | $ mvn test -D suite=single.xml -Dtestng.dtd.http=true 57 | 58 | ``` 59 | ## About LambdaTest 60 | 61 | [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. 62 | 63 | -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | steps: 2 | - name: gcr.io/cloud-builders/docker 3 | args: ['run' , '-d', '--name=lt', '--network=cloudbuild', 'lambdatest/tunnel', '--user', '${_LT_USERNAME}', '--key', '${_LT_ACCESS_KEY}', '--tunnelName', 'GCloud', '--infoAPIPort', '15000','--load-balanced'] 4 | - name: curlimages/curl 5 | args: ['-s', '--retry-connrefused', '--connect-timeout', '5', '--max-time', '5', '--retry', '30', '--retry-delay', '2', '--retry-max-time', '60', 'http://lt:15000/api/v1.0/info'] 6 | - name: 'ubuntu' 7 | args: ['sleep', '20'] 8 | - name: 'bash' 9 | args: ['ls'] 10 | - name: 'maven' 11 | entrypoint: 'mvn' 12 | args: ['test', '-P', 'local'] 13 | env: 14 | - 'LT_USERNAME=${_LT_USERNAME}' 15 | - 'LT_ACCESS_KEY=${_LT_ACCESS_KEY}' 16 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | Java-TestNG-Selenium 4 | Java-TestNG-Selenium 5 | 0.0.1-SNAPSHOT 6 | 7 | Java-TestNG-Selenium/src 8 | 9 | 10 | Java-TestNG-Selenium/src 11 | 12 | **/*.java 13 | 14 | 15 | 16 | 17 | 18 | maven-compiler-plugin 19 | 3.7.0 20 | 21 | 10 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-surefire-plugin 28 | 3.0.0-M5 29 | 30 | 31 | 32 | test 33 | 34 | 35 | 36 | 37 | 38 | 39 | ${suite} 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.seleniumhq.selenium 49 | selenium-java 50 | 4.1.1 51 | 52 | 53 | 54 | org.testng 55 | testng 56 | 7.3.0 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /single.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/CapturePerformanceMetrics.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Optional; 9 | 10 | import org.openqa.selenium.JavascriptExecutor; 11 | import org.openqa.selenium.WebDriver; 12 | import org.openqa.selenium.devtools.DevTools; 13 | import org.openqa.selenium.devtools.HasDevTools; 14 | import org.openqa.selenium.devtools.v94.performance.Performance; 15 | import org.openqa.selenium.devtools.v94.performance.model.Metric; 16 | import org.openqa.selenium.remote.Augmenter; 17 | import org.openqa.selenium.remote.DesiredCapabilities; 18 | import org.openqa.selenium.remote.RemoteWebDriver; 19 | import org.testng.ITestContext; 20 | import org.testng.annotations.AfterMethod; 21 | import org.testng.annotations.BeforeMethod; 22 | import org.testng.annotations.Test; 23 | 24 | public class CapturePerformanceMetrics { 25 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 26 | 27 | static Boolean success = false; 28 | private WebDriver driver; 29 | 30 | 31 | @BeforeMethod 32 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 33 | DesiredCapabilities capabilities = new DesiredCapabilities(); 34 | capabilities.setCapability("browserName", "Chrome"); 35 | capabilities.setCapability("browserVersion", "latest"); 36 | HashMap ltOptions = new HashMap(); 37 | ltOptions.put("user", System.getenv("LT_USERNAME")); 38 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 39 | ltOptions.put("build", "Selenium 4"); 40 | ltOptions.put("name", this.getClass().getName()); 41 | ltOptions.put("platformName", "Windows 10"); 42 | ltOptions.put("seCdp", true); 43 | ltOptions.put("selenium_version", "4.0.0"); 44 | capabilities.setCapability("LT:Options", ltOptions); 45 | 46 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 47 | System.out.println(driver); 48 | } 49 | 50 | @Test 51 | public void capturePerformanceMetrics() { 52 | Augmenter augmenter = new Augmenter(); 53 | driver = augmenter.augment(driver); 54 | 55 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 56 | devTools.createSession(); 57 | 58 | devTools.send(Performance.enable(Optional.empty())); 59 | List metricList = devTools.send(Performance.getMetrics()); 60 | 61 | driver.get("https://lambdatest.com"); 62 | 63 | for (Metric m : metricList) { 64 | System.out.println(m.getName() + " = " + m.getValue()); 65 | success = true; 66 | } 67 | if (success) { 68 | markStatus("passed", "Performance metrics successfully fetched", driver); 69 | } else { 70 | markStatus("failed", "Unable to fetch Performance metrics", driver); 71 | } 72 | } 73 | 74 | @AfterMethod 75 | public void tearDown() { 76 | try { 77 | driver.quit(); 78 | 79 | } catch ( 80 | 81 | Exception e) { 82 | markStatus("failed", "Got exception!", driver); 83 | e.printStackTrace(); 84 | driver.quit(); 85 | } 86 | } 87 | 88 | public static void markStatus(String status, String reason, WebDriver driver) { 89 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 90 | jsExecute.executeScript("lambda-status=" + status); 91 | System.out.println(reason); 92 | } 93 | 94 | } --------------------------------------------------------------------------------