├── .gitignore ├── .gitpod.yml ├── README.md ├── cloudbuild.yaml ├── mobile.xml ├── parallel.xml ├── pom.xml ├── single.xml └── src └── test └── java └── com └── lambdatest ├── BasicAuthentication.java ├── CDPTestConsole.java ├── CapturePerformanceMetrics.java ├── EmulateGeolocation.java ├── EmulateNetworkConditions.java ├── InterceptNetwork.java ├── JavaScriptExceptions.java ├── OverrideDeviceMode.java ├── TestNGRelativeLocator.java ├── TestNGTodoMobile.java ├── TestNGTodoTest.java └── toggleNetworkOffline.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=parallel.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 utilise Bidirectional protocol (BiDi) for automation test in Java-testng 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 | 38 | ### Running Tests 39 | 40 | ``` 41 | To run single test 42 | $ mvn test -D suite=single.xml -Dtestng.dtd.http=true 43 | 44 | To run parallel test 45 | $ mvn test -D suite=parallel.xml -Dtestng.dtd.http=true 46 | 47 | To run realdevice test 48 | $ mvn test -D suite=mobile.xml -Dtestng.dtd.http=true 49 | 50 | 51 | ``` 52 | ## About LambdaTest 53 | 54 | [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. 55 | 56 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /mobile.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /parallel.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 14 | 15 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /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/BasicAuthentication.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 | 8 | import org.openqa.selenium.By; 9 | import org.openqa.selenium.HasAuthentication; 10 | import org.openqa.selenium.JavascriptExecutor; 11 | import org.openqa.selenium.UsernameAndPassword; 12 | import org.openqa.selenium.WebDriver; 13 | import org.openqa.selenium.devtools.DevTools; 14 | import org.openqa.selenium.devtools.HasDevTools; 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 | 23 | public class BasicAuthentication { 24 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 25 | private WebDriver driver; 26 | 27 | @BeforeMethod 28 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 29 | 30 | DesiredCapabilities capabilities = new DesiredCapabilities(); 31 | capabilities.setCapability("browserName", "Chrome"); 32 | capabilities.setCapability("browserVersion", "latest"); 33 | HashMap ltOptions = new HashMap(); 34 | ltOptions.put("user", System.getenv("LT_USERNAME")); 35 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 36 | ltOptions.put("build", "Selenium 4"); 37 | ltOptions.put("name", this.getClass().getName()); 38 | ltOptions.put("platformName", "Windows 10"); 39 | ltOptions.put("seCdp", true); 40 | ltOptions.put("selenium_version", "4.0.0"); 41 | capabilities.setCapability("LT:Options", ltOptions); 42 | 43 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 44 | System.out.println(driver); 45 | } 46 | 47 | @Test 48 | public void authentication() { 49 | Augmenter augmenter = new Augmenter(); 50 | driver = augmenter.augment(driver); 51 | 52 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 53 | devTools.createSession(); 54 | 55 | driver = augmenter.addDriverAugmentation("chrome", HasAuthentication.class, 56 | (caps, exec) -> (whenThisMatches, useTheseCredentials) -> devTools.getDomains().network() 57 | .addAuthHandler(whenThisMatches, useTheseCredentials)) 58 | .augment(driver); 59 | 60 | ((HasAuthentication) driver).register(UsernameAndPassword.of("foo", "bar")); 61 | 62 | driver.get("http://httpbin.org/basic-auth/foo/bar"); 63 | 64 | String text = driver.findElement(By.tagName("body")).getText(); 65 | System.out.println(text); 66 | if (text.contains("authenticated")) { 67 | markStatus("passed", "Authentication Successful", driver); 68 | } else { 69 | markStatus("failed", "Authentication Failure", driver); 70 | } 71 | 72 | } 73 | 74 | @AfterMethod 75 | public void tearDown() { 76 | try { 77 | driver.quit(); 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 | 94 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/CDPTestConsole.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 | 8 | import org.openqa.selenium.JavascriptExecutor; 9 | import org.openqa.selenium.WebDriver; 10 | import org.openqa.selenium.devtools.DevTools; 11 | import org.openqa.selenium.devtools.HasDevTools; 12 | import org.openqa.selenium.devtools.v94.log.Log; 13 | import org.openqa.selenium.remote.Augmenter; 14 | import org.openqa.selenium.remote.DesiredCapabilities; 15 | import org.openqa.selenium.remote.RemoteWebDriver; 16 | import org.testng.ITestContext; 17 | import org.testng.annotations.AfterMethod; 18 | import org.testng.annotations.BeforeMethod; 19 | import org.testng.annotations.Test; 20 | 21 | public class CDPTestConsole { 22 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 23 | static Boolean status = false; 24 | 25 | private WebDriver driver; 26 | 27 | @BeforeMethod 28 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 29 | 30 | DesiredCapabilities capabilities = new DesiredCapabilities(); 31 | capabilities.setCapability("browserName", "Chrome"); 32 | capabilities.setCapability("browserVersion", "latest"); 33 | HashMap ltOptions = new HashMap(); 34 | ltOptions.put("user", System.getenv("LT_USERNAME")); 35 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 36 | ltOptions.put("build", "Selenium 4"); 37 | ltOptions.put("name", this.getClass().getName()); 38 | ltOptions.put("platformName", "Windows 10"); 39 | ltOptions.put("seCdp", true); 40 | ltOptions.put("selenium_version", "4.0.0"); 41 | capabilities.setCapability("LT:Options", ltOptions); 42 | 43 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 44 | System.out.println(driver); 45 | } 46 | 47 | @Test 48 | public void cdpTestConsole() throws InterruptedException { 49 | Augmenter augmenter = new Augmenter(); 50 | driver = augmenter.augment(driver); 51 | 52 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 53 | devTools.createSession(); 54 | 55 | devTools.send(Log.enable()); 56 | devTools.addListener(Log.entryAdded(), logEntry -> { 57 | System.out.println("text: " + logEntry.getText()); 58 | System.out.println("level: " + logEntry.getLevel()); 59 | status = true; 60 | }); 61 | driver.get("http://the-internet.herokuapp.com/broken_images"); 62 | Thread.sleep(1000 * 10); 63 | if (status) { 64 | markStatus("passed", "Console logs enteries", driver); 65 | } else { 66 | markStatus("failed", "Did not found Console logs enteries", driver); 67 | } 68 | } 69 | 70 | @AfterMethod 71 | public void tearDown() { 72 | try { 73 | driver.quit(); 74 | 75 | } catch ( 76 | 77 | Exception e) { 78 | markStatus("failed", "Got exception!", driver); 79 | e.printStackTrace(); 80 | driver.quit(); 81 | } 82 | } 83 | 84 | public static void markStatus(String status, String reason, WebDriver driver) { 85 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 86 | jsExecute.executeScript("lambda-status=" + status); 87 | System.out.println(reason); 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/EmulateGeolocation.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.Map; 8 | import java.util.Optional; 9 | 10 | import org.openqa.selenium.By; 11 | import org.openqa.selenium.JavascriptExecutor; 12 | import org.openqa.selenium.WebDriver; 13 | import org.openqa.selenium.chrome.ChromeOptions; 14 | import org.openqa.selenium.devtools.DevTools; 15 | import org.openqa.selenium.devtools.HasDevTools; 16 | import org.openqa.selenium.devtools.v95.emulation.Emulation; 17 | import org.openqa.selenium.remote.Augmenter; 18 | import org.openqa.selenium.remote.DesiredCapabilities; 19 | import org.openqa.selenium.remote.RemoteWebDriver; 20 | import org.testng.ITestContext; 21 | import org.testng.annotations.AfterMethod; 22 | import org.testng.annotations.BeforeMethod; 23 | import org.testng.annotations.Test; 24 | 25 | public class EmulateGeolocation { 26 | 27 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 28 | private WebDriver driver; 29 | 30 | @BeforeMethod 31 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 32 | DesiredCapabilities capabilities = new DesiredCapabilities(); 33 | capabilities.setCapability("browserName", "Chrome"); 34 | capabilities.setCapability("browserVersion", "latest"); 35 | HashMap ltOptions = new HashMap(); 36 | ltOptions.put("user", System.getenv("LT_USERNAME")); 37 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 38 | ltOptions.put("build", "Selenium 4"); 39 | ltOptions.put("name", this.getClass().getName()); 40 | ltOptions.put("platformName", "Windows 10"); 41 | ltOptions.put("seCdp", true); 42 | ltOptions.put("selenium_version", "4.0.0"); 43 | capabilities.setCapability("LT:Options", ltOptions); 44 | 45 | ChromeOptions options = new ChromeOptions(); 46 | 47 | Map prefs = new HashMap(); 48 | prefs.put("googlegeolocationaccess.enabled", true); 49 | prefs.put("profile.default_content_setting_values.geolocation", 1); // 1:allow 2:block 50 | prefs.put("profile.default_content_setting_values.notifications", 1); 51 | prefs.put("profile.managed_default_content_settings", 1); 52 | options.setExperimentalOption("prefs", prefs); 53 | capabilities.setCapability(ChromeOptions.CAPABILITY, options); 54 | 55 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 56 | 57 | System.out.println(driver); 58 | } 59 | 60 | @Test 61 | public void emulateGeolocation() { 62 | Augmenter augmenter = new Augmenter(); 63 | driver = augmenter.augment(driver); 64 | 65 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 66 | devTools.createSession(); 67 | 68 | // setGeolocationOverride() takes input lattitude, longitude and accuracy as 69 | // parameters. 70 | devTools.send(Emulation.setGeolocationOverride(Optional.of(28.622409), 71 | Optional.of(77.364925), 72 | Optional.of(1))); 73 | driver.get("https://my-location.org"); 74 | 75 | String address = driver.findElement(By.id("address")).getText(); 76 | System.out.println(address); 77 | if (address.contains("Noida")) { 78 | markStatus("passed", "I am in Noida", driver); 79 | } else { 80 | markStatus("failed", "I am not in Noida", driver); 81 | } 82 | 83 | } 84 | 85 | @AfterMethod 86 | public void tearDown() { 87 | try { 88 | driver.quit(); 89 | 90 | } catch ( 91 | 92 | Exception e) { 93 | markStatus("failed", "Got exception!", driver); 94 | e.printStackTrace(); 95 | driver.quit(); 96 | } 97 | } 98 | 99 | public static void markStatus(String status, String reason, WebDriver driver) { 100 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 101 | jsExecute.executeScript("lambda-status=" + status); 102 | System.out.println(reason); 103 | } 104 | 105 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/EmulateNetworkConditions.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.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.v95.network.Network; 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 | 23 | public class EmulateNetworkConditions { 24 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 25 | 26 | private WebDriver driver; 27 | 28 | @BeforeMethod 29 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 30 | DesiredCapabilities capabilities = new DesiredCapabilities(); 31 | capabilities.setCapability("browserName", "Chrome"); 32 | capabilities.setCapability("browserVersion", "latest"); 33 | HashMap ltOptions = new HashMap(); 34 | ltOptions.put("user", System.getenv("LT_USERNAME")); 35 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 36 | ltOptions.put("build", "Selenium 4"); 37 | ltOptions.put("name", this.getClass().getName()); 38 | ltOptions.put("platformName", "Windows 10"); 39 | ltOptions.put("seCdp", true); 40 | ltOptions.put("selenium_version", "4.0.0"); 41 | capabilities.setCapability("LT:Options", ltOptions); 42 | 43 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 44 | 45 | System.out.println(driver); 46 | } 47 | 48 | @Test 49 | public void emulateNetworkConditions() throws InterruptedException { 50 | Augmenter augmenter = new Augmenter(); 51 | driver = augmenter.augment(driver); 52 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 53 | devTools.createSession(); 54 | 55 | devTools.send(Network.emulateNetworkConditions(false, 0, 2 * 1024 * 1024, 2 * 1024 * 1024, Optional.empty())); 56 | driver.get("https://fast.com"); 57 | Thread.sleep(15000); 58 | String speed = driver.findElement(By.id("speed-value")).getText(); 59 | String units = driver.findElement(By.id("speed-units")).getText(); 60 | System.out.println(speed); 61 | System.out.println(units); 62 | if (Float.parseFloat(speed) < 2 && units.equals("Mbps")) { 63 | markStatus("passed", "Speed is in the limit of 2 Mbps", driver); 64 | } else { 65 | markStatus("failed", "Speed is not in the limit of 2 Mbp", driver); 66 | } 67 | 68 | } 69 | 70 | @AfterMethod 71 | public void tearDown() { 72 | try { 73 | driver.quit(); 74 | 75 | } catch ( 76 | 77 | Exception e) { 78 | markStatus("failed", "Got exception!", driver); 79 | e.printStackTrace(); 80 | driver.quit(); 81 | } 82 | } 83 | 84 | public static void markStatus(String status, String reason, WebDriver driver) { 85 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 86 | jsExecute.executeScript("lambda-status=" + status); 87 | System.out.println(reason); 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/InterceptNetwork.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.lang.reflect.Method; 6 | 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | import java.nio.charset.StandardCharsets; 10 | import java.util.HashMap; 11 | import java.util.function.Supplier; 12 | 13 | import org.openqa.selenium.JavascriptExecutor; 14 | import org.openqa.selenium.WebDriver; 15 | import org.openqa.selenium.devtools.DevTools; 16 | import org.openqa.selenium.devtools.HasDevTools; 17 | import org.openqa.selenium.devtools.NetworkInterceptor; 18 | import org.openqa.selenium.remote.Augmenter; 19 | import org.openqa.selenium.remote.DesiredCapabilities; 20 | import org.openqa.selenium.remote.RemoteWebDriver; 21 | import org.openqa.selenium.remote.http.HttpResponse; 22 | import org.openqa.selenium.remote.http.Route; 23 | 24 | import org.testng.ITestContext; 25 | import org.testng.annotations.AfterMethod; 26 | import org.testng.annotations.BeforeMethod; 27 | import org.testng.annotations.Test; 28 | 29 | 30 | public class InterceptNetwork { 31 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 32 | 33 | private WebDriver driver; 34 | 35 | @BeforeMethod 36 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 37 | DesiredCapabilities capabilities = new DesiredCapabilities(); 38 | capabilities.setCapability("browserName", "Chrome"); 39 | capabilities.setCapability("browserVersion", "latest"); 40 | HashMap ltOptions = new HashMap(); 41 | ltOptions.put("user", System.getenv("LT_USERNAME")); 42 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 43 | ltOptions.put("build", "Selenium 4"); 44 | ltOptions.put("name", this.getClass().getName()); 45 | ltOptions.put("platformName", "Windows 10"); 46 | ltOptions.put("seCdp", true); 47 | ltOptions.put("selenium_version", "4.0.0"); 48 | capabilities.setCapability("LT:Options", ltOptions); 49 | 50 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 51 | System.out.println(driver); 52 | } 53 | 54 | @Test 55 | public void interceptNetwork() { 56 | Augmenter augmenter = new Augmenter(); 57 | driver = augmenter.augment(driver); 58 | 59 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 60 | devTools.createSession(); 61 | 62 | Supplier message = () -> new ByteArrayInputStream( 63 | "Creamy, delicious cheese!".getBytes(StandardCharsets.UTF_8)); 64 | 65 | NetworkInterceptor interceptor = new NetworkInterceptor( 66 | driver, 67 | Route.matching(req -> true) 68 | .to(() -> req -> new HttpResponse() 69 | .setStatus(200) 70 | .addHeader("Content-Type", StandardCharsets.UTF_8.toString()) 71 | .setContent(message))); 72 | driver.get("https://example-sausages-site.com"); 73 | String source = driver.getPageSource(); 74 | System.out.println(source); 75 | 76 | if (source.contains("delicious cheese!")) { 77 | markStatus("passed", "Source contains the contents", driver); 78 | } else { 79 | markStatus("failed", "Content was not found in the source", driver); 80 | } 81 | interceptor.close(); 82 | 83 | } 84 | 85 | @AfterMethod 86 | public void tearDown() { 87 | try { 88 | driver.quit(); 89 | 90 | } catch ( 91 | 92 | Exception e) { 93 | markStatus("failed", "Got exception!", driver); 94 | e.printStackTrace(); 95 | driver.quit(); 96 | } 97 | } 98 | 99 | public static void markStatus(String status, String reason, WebDriver driver) { 100 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 101 | jsExecute.executeScript("lambda-status=" + status); 102 | System.out.println(reason); 103 | } 104 | 105 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/JavaScriptExceptions.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URL; 5 | import java.util.HashMap; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.function.Consumer; 10 | 11 | import org.openqa.selenium.By; 12 | import org.openqa.selenium.JavascriptException; 13 | import org.openqa.selenium.JavascriptExecutor; 14 | import org.openqa.selenium.WebDriver; 15 | import org.openqa.selenium.WebElement; 16 | import org.openqa.selenium.devtools.DevTools; 17 | import org.openqa.selenium.devtools.HasDevTools; 18 | import org.openqa.selenium.remote.Augmenter; 19 | import org.openqa.selenium.remote.DesiredCapabilities; 20 | import org.openqa.selenium.remote.RemoteWebDriver; 21 | import org.testng.ITestContext; 22 | import org.testng.annotations.AfterMethod; 23 | import org.testng.annotations.BeforeMethod; 24 | import org.testng.annotations.Test; 25 | import java.lang.reflect.Method; 26 | 27 | public class JavaScriptExceptions { 28 | public static String hubURL = "https://hub.lambdatest.com/wd/hub"; 29 | static Boolean success = false; 30 | 31 | private WebDriver driver; 32 | 33 | @BeforeMethod 34 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 35 | DesiredCapabilities capabilities = new DesiredCapabilities(); 36 | capabilities.setCapability("browserName", "Chrome"); 37 | capabilities.setCapability("browserVersion", "latest"); 38 | HashMap ltOptions = new HashMap(); 39 | ltOptions.put("user", System.getenv("LT_USERNAME")); 40 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 41 | ltOptions.put("build", "Selenium 4"); 42 | ltOptions.put("name", this.getClass().getName()); 43 | ltOptions.put("platformName", "Windows 10"); 44 | ltOptions.put("seCdp", true); 45 | ltOptions.put("selenium_version", "4.0.0"); 46 | capabilities.setCapability("LT:Options", ltOptions); 47 | 48 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 49 | System.out.println(driver); 50 | } 51 | 52 | @Test 53 | public void javaScriptExceptions() throws InterruptedException { 54 | Augmenter augmenter = new Augmenter(); 55 | driver = augmenter.augment(driver); 56 | 57 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 58 | devTools.createSession(); 59 | 60 | List jsExceptionsList = new ArrayList<>(); 61 | Consumer addEntry = jsExceptionsList::add; 62 | devTools.getDomains().events().addJavascriptExceptionListener(addEntry); 63 | 64 | driver.get("https://facebook.com"); 65 | 66 | WebElement link2click = driver.findElement(By.name("login")); 67 | ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", 68 | link2click, "onclick", "throw new Error('My JS Error');"); 69 | link2click.click(); 70 | 71 | Thread.sleep(1000); 72 | for (JavascriptException jsException : jsExceptionsList) { 73 | System.out.println("My JS exception message: " + jsException.getMessage()); 74 | System.out.println("My JS exception system: " + jsException.getSystemInformation()); 75 | jsException.printStackTrace(); 76 | success = true; 77 | } 78 | Thread.sleep(1000); 79 | if (success) { 80 | markStatus("passed", "Got JS exception", driver); 81 | } else { 82 | markStatus("failed", "Didn't got JS exception", driver); 83 | } 84 | 85 | } 86 | 87 | @AfterMethod 88 | public void tearDown() { 89 | try { 90 | driver.quit(); 91 | 92 | } catch ( 93 | 94 | Exception e) { 95 | markStatus("failed", "Got exception!", driver); 96 | e.printStackTrace(); 97 | driver.quit(); 98 | } 99 | } 100 | 101 | public static void markStatus(String status, String reason, WebDriver driver) { 102 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 103 | jsExecute.executeScript("lambda-status=" + status); 104 | System.out.println(reason); 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGRelativeLocator.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import static org.openqa.selenium.support.locators.RelativeLocator.with; 4 | 5 | import java.net.MalformedURLException; 6 | import java.net.URL; 7 | import java.util.HashMap; 8 | 9 | import org.openqa.selenium.By; 10 | import org.openqa.selenium.JavascriptExecutor; 11 | import org.openqa.selenium.WebDriver; 12 | import org.openqa.selenium.WebElement; 13 | import org.openqa.selenium.remote.DesiredCapabilities; 14 | import org.openqa.selenium.remote.RemoteWebDriver; 15 | import org.testng.ITestContext; 16 | import org.testng.annotations.AfterMethod; 17 | import org.testng.annotations.BeforeMethod; 18 | import org.testng.annotations.Test; 19 | import java.lang.reflect.Method; 20 | 21 | public class TestNGRelativeLocator { 22 | 23 | private WebDriver driver; 24 | 25 | @BeforeMethod 26 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 27 | 28 | String hubURL = "https://hub.lambdatest.com/wd/hub"; 29 | 30 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; 31 | 32 | DesiredCapabilities capabilities = new DesiredCapabilities(); 33 | capabilities.setCapability("browserName", "Chrome"); 34 | capabilities.setCapability("browserVersion", "latest"); 35 | 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 | ltOptions.put("tags", Tags); 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 testNGRelativeLocator() throws InterruptedException { 53 | 54 | driver.get("http://cookbook.seleniumacademy.com/mobilebmicalculator.html"); 55 | 56 | // find the height and weight labels using css selector 57 | WebElement heightLabel = driver.findElement(By.cssSelector("label[for='heightCMS']")); 58 | WebElement weightLabel = driver.findElement(By.cssSelector("label[for='weightKg']")); 59 | 60 | // find the height input using toRightOf relative locator 61 | // input is right of height label 62 | WebElement heightInput = driver.findElement(with(By.tagName("input")) 63 | .toRightOf(heightLabel)); 64 | 65 | heightInput.sendKeys("181"); 66 | Thread.sleep(3000); 67 | 68 | // find the weight input using combination of below and toRightOf relative 69 | // locator 70 | // weight input is below height input and right of weight label 71 | WebElement weightInput = driver.findElement(with(By.tagName("input")) 72 | .below(heightInput).toRightOf(weightLabel)); 73 | 74 | weightInput.sendKeys("75"); 75 | Thread.sleep(3000); 76 | 77 | // find the calculate button which is below the weight label 78 | WebElement calculateButton = driver.findElement(with(By.tagName("input")) 79 | .below(weightLabel)); 80 | 81 | calculateButton.click(); 82 | Thread.sleep(3000); 83 | 84 | // find the read only input below calculate button to verify value 85 | if ("22.9".equalsIgnoreCase(driver.findElement(with(By.tagName("input")) 86 | .below(calculateButton)).getAttribute("value"))) { 87 | markStatus("passed", "Value Verified", driver); 88 | Thread.sleep(150); 89 | 90 | System.out.println("TestFinished"); 91 | } else { 92 | markStatus("failed", "Wrong calculation", driver); 93 | } 94 | 95 | } 96 | 97 | @AfterMethod 98 | public void tearDown() { 99 | driver.quit(); 100 | } 101 | 102 | public static void markStatus(String status, String reason, WebDriver driver) { 103 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 104 | jsExecute.executeScript("lambda-status=" + status); 105 | System.out.println(reason); 106 | } 107 | 108 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodoMobile.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.remote.DesiredCapabilities; 9 | import org.openqa.selenium.remote.RemoteWebDriver; 10 | import org.testng.Assert; 11 | import org.testng.ITestContext; 12 | import org.testng.annotations.AfterMethod; 13 | import org.testng.annotations.BeforeMethod; 14 | import org.testng.annotations.Test; 15 | 16 | public class TestNGTodoMobile { 17 | 18 | private RemoteWebDriver driver; 19 | private String Status = "failed"; 20 | 21 | 22 | @BeforeMethod 23 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 24 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 25 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 26 | ; 27 | String hub = "@hub.lambdatest.com/wd/hub"; 28 | 29 | DesiredCapabilities caps = new DesiredCapabilities(); 30 | caps.setCapability("deviceName", "Google Pixel 4a"); 31 | caps.setCapability("build", "Selenium 4"); 32 | caps.setCapability("name", this.getClass().getName()); 33 | caps.setCapability("plugin", "git-testng"); 34 | caps.setCapability("isRealMobile", true); 35 | caps.setCapability("selenium_version", "4.0.0"); 36 | 37 | String[] Tags = new String[] { "Feature", "Tag", "Moderate" }; 38 | caps.setCapability("tags", Tags); 39 | 40 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); 41 | } 42 | 43 | @Test 44 | public void basicTest() throws InterruptedException { 45 | String spanText; 46 | System.out.println("Loading Url"); 47 | Thread.sleep(100); 48 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 49 | 50 | System.out.println("Checking Box"); 51 | driver.findElement(By.name("li1")).click(); 52 | 53 | System.out.println("Checking Another Box"); 54 | driver.findElement(By.name("li2")).click(); 55 | 56 | System.out.println("Checking Box"); 57 | driver.findElement(By.name("li3")).click(); 58 | 59 | System.out.println("Checking Another Box"); 60 | driver.findElement(By.name("li4")).click(); 61 | 62 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 63 | driver.findElement(By.id("addbutton")).click(); 64 | 65 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 66 | driver.findElement(By.id("addbutton")).click(); 67 | 68 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 69 | driver.findElement(By.id("addbutton")).click(); 70 | 71 | System.out.println("Checking Another Box"); 72 | driver.findElement(By.name("li1")).click(); 73 | 74 | System.out.println("Checking Another Box"); 75 | driver.findElement(By.name("li3")).click(); 76 | 77 | System.out.println("Checking Another Box"); 78 | driver.findElement(By.name("li7")).click(); 79 | 80 | System.out.println("Checking Another Box"); 81 | driver.findElement(By.name("li8")).click(); 82 | 83 | System.out.println("Entering Text"); 84 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 85 | 86 | driver.findElement(By.id("addbutton")).click(); 87 | 88 | System.out.println("Checking Another Box"); 89 | driver.findElement(By.name("li9")).click(); 90 | 91 | // Let's also assert that the todo we added is present in the list. 92 | 93 | spanText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[9]/span")).getText(); 94 | Assert.assertEquals("Get Taste of Lambda and Stick to It", spanText); 95 | Status = "passed"; 96 | Thread.sleep(800); 97 | 98 | System.out.println("TestFinished"); 99 | 100 | } 101 | 102 | @AfterMethod 103 | public void tearDown() { 104 | driver.executeScript("lambda-status=" + Status); 105 | driver.quit(); 106 | } 107 | 108 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodoTest.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.remote.DesiredCapabilities; 9 | import org.openqa.selenium.remote.RemoteWebDriver; 10 | import org.testng.Assert; 11 | import org.testng.ITestContext; 12 | import org.testng.annotations.AfterMethod; 13 | import org.testng.annotations.BeforeMethod; 14 | import org.testng.annotations.Test; 15 | 16 | public class TestNGTodoTest { 17 | 18 | private RemoteWebDriver driver; 19 | private String Status = "failed"; 20 | 21 | 22 | @BeforeMethod 23 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 24 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 25 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 26 | ; 27 | String hub = "@hub.lambdatest.com/wd/hub"; 28 | 29 | DesiredCapabilities caps = new DesiredCapabilities(); 30 | caps.setCapability("platform", "Windows 10"); 31 | caps.setCapability("browserName", "Chrome"); 32 | caps.setCapability("version", "latest"); 33 | caps.setCapability("build", "Selenium 4"); 34 | caps.setCapability("name", this.getClass().getName()); 35 | caps.setCapability("plugin", "git-testng"); 36 | caps.setCapability("selenium_version", "4.0.0"); 37 | 38 | String[] Tags = new String[] { "Feature", "Tag", "Moderate" }; 39 | caps.setCapability("tags", Tags); 40 | 41 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); 42 | } 43 | 44 | @Test 45 | public void basicTest() throws InterruptedException { 46 | String spanText; 47 | System.out.println("Loading Url"); 48 | Thread.sleep(100); 49 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 50 | 51 | System.out.println("Checking Box"); 52 | driver.findElement(By.name("li1")).click(); 53 | 54 | System.out.println("Checking Another Box"); 55 | driver.findElement(By.name("li2")).click(); 56 | 57 | System.out.println("Checking Box"); 58 | driver.findElement(By.name("li3")).click(); 59 | 60 | System.out.println("Checking Another Box"); 61 | driver.findElement(By.name("li4")).click(); 62 | 63 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 64 | driver.findElement(By.id("addbutton")).click(); 65 | 66 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 67 | driver.findElement(By.id("addbutton")).click(); 68 | 69 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 70 | driver.findElement(By.id("addbutton")).click(); 71 | 72 | System.out.println("Checking Another Box"); 73 | driver.findElement(By.name("li1")).click(); 74 | 75 | System.out.println("Checking Another Box"); 76 | driver.findElement(By.name("li3")).click(); 77 | 78 | System.out.println("Checking Another Box"); 79 | driver.findElement(By.name("li7")).click(); 80 | 81 | System.out.println("Checking Another Box"); 82 | driver.findElement(By.name("li8")).click(); 83 | 84 | System.out.println("Entering Text"); 85 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 86 | 87 | driver.findElement(By.id("addbutton")).click(); 88 | 89 | System.out.println("Checking Another Box"); 90 | driver.findElement(By.name("li9")).click(); 91 | 92 | // Let's also assert that the todo we added is present in the list. 93 | 94 | spanText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[9]/span")).getText(); 95 | Assert.assertEquals("Get Taste of Lambda and Stick to It", spanText); 96 | Status = "passed"; 97 | Thread.sleep(800); 98 | 99 | System.out.println("TestFinished"); 100 | 101 | } 102 | 103 | @AfterMethod 104 | public void tearDown() { 105 | driver.executeScript("lambda-status=" + Status); 106 | driver.quit(); 107 | } 108 | 109 | } -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/toggleNetworkOffline.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.Optional; 7 | 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.WebDriverException; 10 | import org.openqa.selenium.chromium.ChromiumNetworkConditions; 11 | import org.openqa.selenium.chromium.HasNetworkConditions; 12 | import org.openqa.selenium.devtools.DevTools; 13 | import org.openqa.selenium.devtools.HasDevTools; 14 | import org.openqa.selenium.devtools.v94.browser.Browser; 15 | import org.openqa.selenium.devtools.v94.browser.model.Bounds; 16 | import org.openqa.selenium.devtools.v94.browser.model.WindowID; 17 | import org.openqa.selenium.devtools.v94.browser.model.WindowState; 18 | import org.openqa.selenium.devtools.v94.log.Log; 19 | import org.openqa.selenium.remote.Augmenter; 20 | import org.openqa.selenium.remote.DesiredCapabilities; 21 | import org.openqa.selenium.remote.RemoteWebDriver; 22 | import org.testng.ITestContext; 23 | import org.testng.annotations.AfterMethod; 24 | import org.testng.annotations.BeforeMethod; 25 | import org.testng.annotations.Test; 26 | 27 | public class toggleNetworkOffline { 28 | 29 | private WebDriver driver; 30 | private String Status = "failed"; 31 | 32 | 33 | @BeforeMethod 34 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 35 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 36 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 37 | ; 38 | String hub = "@hub.lambdatest.com/wd/hub"; 39 | 40 | DesiredCapabilities caps = new DesiredCapabilities(); 41 | caps.setCapability("platform", "Windows 10"); 42 | caps.setCapability("browserName", "chrome"); 43 | caps.setCapability("version", "94"); 44 | caps.setCapability("build", "Selenium 4"); 45 | caps.setCapability("name", this.getClass().getName()); 46 | caps.setCapability("plugin", "git-testng"); 47 | caps.setCapability("selenium_version", "4.0.0"); 48 | 49 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; 50 | caps.setCapability("tags", Tags); 51 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); 52 | 53 | } 54 | 55 | @Test 56 | public void toggleOffline() throws InterruptedException { 57 | 58 | driver = new Augmenter().augment(driver); 59 | 60 | driver.get("https://www.duckduckgo.com"); 61 | DevTools devTools = ((HasDevTools) driver).getDevTools(); 62 | devTools.createSession(); 63 | 64 | devTools.send(Log.enable()); 65 | devTools.send(Browser.setWindowBounds(new WindowID(1), new Bounds(Optional.of(20), Optional.of(20), 66 | Optional.of(20), Optional.of(20), Optional.of(WindowState.NORMAL)))); 67 | 68 | Thread.sleep(30000); 69 | 70 | WebDriver augmentedDriver = new Augmenter().augment(driver); 71 | ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions(); 72 | networkConditions.setOffline(true); 73 | ((HasNetworkConditions) augmentedDriver).setNetworkConditions(networkConditions); 74 | 75 | try { 76 | driver.get("https://www.lambdatest.com"); 77 | // "If Network is set to be offline, the previous line should throw an 78 | // exception"; 79 | } catch (WebDriverException ex) { 80 | ((HasNetworkConditions) augmentedDriver).setNetworkConditions(new ChromiumNetworkConditions()); 81 | } 82 | driver.get("https://www.lambdatest.com"); 83 | } 84 | 85 | @AfterMethod 86 | public void tearDown() { 87 | ((RemoteWebDriver) driver).executeScript("lambda-status=" + Status); 88 | driver.quit(); 89 | } 90 | 91 | } --------------------------------------------------------------------------------