├── .gitignore ├── .gitpod.yml ├── README.md ├── cloudbuild.yaml ├── pom.xml ├── single.xml └── src └── test └── java └── com └── lambdatest └── OverrideDeviceMode.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 set device mode using CDP in Java-TestNG automation test 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 | ### Override device mode 38 | To override device mode, the following code can be used 39 | ```java 40 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 41 | devTools.createSession(); 42 | 43 | devTools.send(Emulation.setDeviceMetricsOverride(414, 736, 50, true, Optional.empty(), Optional.empty(), 44 | Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 45 | Optional.empty(), Optional.empty())); 46 | ``` 47 | ### Running Tests 48 | 49 | ``` 50 | To run single test 51 | $ mvn test -D suite=single.xml -Dtestng.dtd.http=true 52 | ``` 53 | ## About LambdaTest 54 | 55 | [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. 56 | 57 | -------------------------------------------------------------------------------- /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/OverrideDeviceMode.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | import java.util.HashMap; 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.devtools.DevTools; 13 | import org.openqa.selenium.devtools.HasDevTools; 14 | import org.openqa.selenium.devtools.v94.emulation.Emulation; 15 | import org.openqa.selenium.remote.Augmenter; 16 | import org.openqa.selenium.remote.DesiredCapabilities; 17 | import org.openqa.selenium.remote.RemoteWebDriver; 18 | import org.testng.ITestContext; 19 | import org.testng.annotations.AfterMethod; 20 | import org.testng.annotations.BeforeMethod; 21 | import org.testng.annotations.Test; 22 | import java.lang.reflect.Method; 23 | 24 | 25 | public class OverrideDeviceMode { 26 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 27 | 28 | private WebDriver driver; 29 | 30 | 31 | 32 | @BeforeMethod 33 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 34 | DesiredCapabilities capabilities = new DesiredCapabilities(); 35 | capabilities.setCapability("browserName", "Chrome"); 36 | capabilities.setCapability("browserVersion", "latest"); 37 | HashMap ltOptions = new HashMap(); 38 | ltOptions.put("user", System.getenv("LT_USERNAME")); 39 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 40 | ltOptions.put("build", "Selenium 4"); 41 | ltOptions.put("name",this.getClass().getName()); 42 | ltOptions.put("platformName", "Windows 10"); 43 | ltOptions.put("seCdp", true); 44 | ltOptions.put("selenium_version", "4.0.0"); 45 | capabilities.setCapability("LT:Options", ltOptions); 46 | 47 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 48 | System.out.println(driver); 49 | } 50 | 51 | @Test 52 | public void overrideDevicemode() { 53 | Augmenter augmenter = new Augmenter(); 54 | driver = augmenter.augment(driver); 55 | 56 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 57 | devTools.createSession(); 58 | 59 | devTools.send(Emulation.setDeviceMetricsOverride(414, 736, 50, true, Optional.empty(), Optional.empty(), 60 | Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 61 | Optional.empty(), Optional.empty())); 62 | driver.get("http://whatismyscreenresolution.net/"); 63 | String resolution = driver.findElement(By.id("resolution")).getText(); 64 | 65 | System.out.println(resolution); 66 | if (resolution.equals("414x736")) { 67 | markStatus("passed", "Found screen resolution: 414x736", driver); 68 | } else { 69 | markStatus("failed", "Not found screen resolution: 414x736", driver); 70 | } 71 | } 72 | 73 | @AfterMethod 74 | public void tearDown() { 75 | try { 76 | driver.quit(); 77 | 78 | } catch ( 79 | 80 | Exception e) { 81 | markStatus("failed", "Got exception!", driver); 82 | e.printStackTrace(); 83 | driver.quit(); 84 | } 85 | } 86 | 87 | public static void markStatus(String status, String reason, WebDriver driver) { 88 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 89 | jsExecute.executeScript("lambda-status=" + status); 90 | System.out.println(reason); 91 | } 92 | 93 | } --------------------------------------------------------------------------------