diff --git a/.gitignore b/.gitignore index 3174135..39f6bdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Compiled class file *.class +settings.json # Log file *.log diff --git a/README.md b/README.md index 6ac604c..322da27 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Run Selenium Tests With JUnit On LambdaTest +# Run Selenium 4 Tests With JUnit On LambdaTest  @@ -94,19 +94,15 @@ Make sure you have your LambdaTest credentials with you to run test automation s ```java DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserName", "chrome"); - capabilities.setCapability("version", "70.0"); - capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one - capabilities.setCapability("build", "LambdaTestSampleApp"); - capabilities.setCapability("name", "LambdaTestJavaSample"); - capabilities.setCapability("network", true); // To enable network logs - capabilities.setCapability("visual", true); // To enable step by step screenshot - capabilities.setCapability("video", true); // To enable video recording - capabilities.setCapability("console", true); // To capture console logs + capabilities.setCapability("browserName", "chrome"); + capabilities.setCapability("version", "latest"); + capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any + // available one + capabilities.setCapability("build", "Junit Testing Example"); + capabilities.setCapability("name", "Selenium 4 Test"); + capabilities.setCapability("plugin", "git-junit"); ``` -You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/). - ### Executing the Test **Step 4:** The tests can be executed in the terminal using the following command. diff --git a/pom.xml b/pom.xml index 275cbfd..2a62b4f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<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"> +<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.lambdatest</groupId> @@ -9,9 +7,6 @@ <version>1.0-SNAPSHOT</version> - - - <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <surefire.version>2.19.1</surefire.version> @@ -36,7 +31,7 @@ <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> - <version>3.141.59</version> + <version>4.1.2</version> <scope>test</scope> </dependency> <dependency> @@ -97,7 +92,7 @@ <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> - <include>com/lambdatest/JUnitTodo.java</include> + <include>com/lambdatest/Authentication.java</include> </includes> </configuration> </plugin> diff --git a/src/test/java/com/lambdatest/Authentication.java b/src/test/java/com/lambdatest/Authentication.java new file mode 100644 index 0000000..956cb59 --- /dev/null +++ b/src/test/java/com/lambdatest/Authentication.java @@ -0,0 +1,94 @@ +package com.lambdatest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + +import org.openqa.selenium.By; +import org.openqa.selenium.HasAuthentication; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.UsernameAndPassword; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +public class Authentication { + String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); + String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); + public static RemoteWebDriver driver = null; + public String gridURL = "@hub.lambdatest.com/wd/hub"; + public String status = "failed"; + + @Before + public void setup() throws MalformedURLException { + + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(gridURL), capabilities); + System.out.println(driver); + } + + @Test + public void authentication() { + Augmenter augmenter = new Augmenter(); + driver = (RemoteWebDriver) augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + driver = (RemoteWebDriver) augmenter.addDriverAugmentation("chrome", HasAuthentication.class, + (caps, exec) -> (whenThisMatches, useTheseCredentials) -> devTools.getDomains().network() + .addAuthHandler(whenThisMatches, useTheseCredentials)) + .augment(driver); + + ((HasAuthentication) driver).register(UsernameAndPassword.of("foo", "bar")); + + driver.get("http://httpbin.org/basic-auth/foo/bar"); + + String text = driver.findElement(By.tagName("body")).getText(); + System.out.println(text); + if (text.contains("authenticated")) { + markStatus("passed", "Authentication Successful", driver); + } else { + markStatus("failed", "Authentication Failure", driver); + } + + } + + @After + public void tearDown() { + try { + driver.quit(); + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/CDPTestConsole.java b/src/test/java/com/lambdatest/CDPTestConsole.java new file mode 100644 index 0000000..5f1fe63 --- /dev/null +++ b/src/test/java/com/lambdatest/CDPTestConsole.java @@ -0,0 +1,93 @@ +package com.lambdatest; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v97.log.Log; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + +public class CDPTestConsole { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + static Boolean status = false; + + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "97"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void cdpTestConsole() throws InterruptedException { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + devTools.send(Log.enable()); + devTools.addListener(Log.entryAdded(), logEntry -> { + System.out.println("text: " + logEntry.getText()); + System.out.println("level: " + logEntry.getLevel()); + status = true; + }); + driver.get("http://the-internet.herokuapp.com/broken_images"); + Thread.sleep(1000 * 10); + if (status) { + markStatus("passed", "Console logs enteries", driver); + } else { + markStatus("failed", "Did not found Console logs enteries", driver); + } + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/CapturePerformanceMetrics.java b/src/test/java/com/lambdatest/CapturePerformanceMetrics.java new file mode 100644 index 0000000..fab2ada --- /dev/null +++ b/src/test/java/com/lambdatest/CapturePerformanceMetrics.java @@ -0,0 +1,94 @@ +package com.lambdatest; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.List; +import java.util.Optional; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v97.performance.Performance; +import org.openqa.selenium.devtools.v97.performance.model.Metric; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + +public class CapturePerformanceMetrics { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + + static Boolean success = false; + private WebDriver driver; + + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "97"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void capturePerformanceMetrics() { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + devTools.send(Performance.enable(Optional.empty())); + List<Metric> metricList = devTools.send(Performance.getMetrics()); + + driver.get("https://lambdatest.com"); + + for (Metric m : metricList) { + System.out.println(m.getName() + " = " + m.getValue()); + success = true; + } + if (success) { + markStatus("passed", "Performance metrics successfully fetched", driver); + } else { + markStatus("failed", "Unable to fetch Performance metrics", driver); + } + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/EmulateGeolocation.java b/src/test/java/com/lambdatest/EmulateGeolocation.java new file mode 100644 index 0000000..7030b09 --- /dev/null +++ b/src/test/java/com/lambdatest/EmulateGeolocation.java @@ -0,0 +1,107 @@ +package com.lambdatest; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.Map; +import java.util.Optional; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v95.emulation.Emulation; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + +public class EmulateGeolocation { + + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + ChromeOptions options = new ChromeOptions(); + + Map<String, Object> prefs = new HashMap<String, Object>(); + prefs.put("googlegeolocationaccess.enabled", true); + prefs.put("profile.default_content_setting_values.geolocation", 1); // 1:allow 2:block + prefs.put("profile.default_content_setting_values.notifications", 1); + prefs.put("profile.managed_default_content_settings", 1); + options.setExperimentalOption("prefs", prefs); + capabilities.setCapability(ChromeOptions.CAPABILITY, options); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + + System.out.println(driver); + } + + @Test + public void emulateGeolocation() { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + // setGeolocationOverride() takes input lattitude, longitude and accuracy as + // parameters. + devTools.send(Emulation.setGeolocationOverride(Optional.of(28.622409), + Optional.of(77.364925), + Optional.of(1))); + driver.get("https://my-location.org"); + + String address = driver.findElement(By.id("address")).getText(); + System.out.println(address); + if (address.contains("Noida")) { + markStatus("passed", "I am in Noida", driver); + } else { + markStatus("failed", "I am not in Noida", driver); + } + + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/EmulateNetworkConditions.java b/src/test/java/com/lambdatest/EmulateNetworkConditions.java new file mode 100644 index 0000000..03cac38 --- /dev/null +++ b/src/test/java/com/lambdatest/EmulateNetworkConditions.java @@ -0,0 +1,92 @@ +package com.lambdatest; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.Optional; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v95.network.Network; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + +public class EmulateNetworkConditions { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + + System.out.println(driver); + } + + @Test + public void emulateNetworkConditions() throws InterruptedException { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + devTools.send(Network.emulateNetworkConditions(false, 0, 2 * 1024 * 1024, 2 * 1024 * 1024, Optional.empty())); + driver.get("https://fast.com"); + Thread.sleep(15000); + String speed = driver.findElement(By.id("speed-value")).getText(); + String units = driver.findElement(By.id("speed-units")).getText(); + System.out.println(speed); + System.out.println(units); + if (Float.parseFloat(speed) < 2 && units.equals("Mbps")) { + markStatus("passed", "Speed is in the limit of 2 Mbps", driver); + } else { + markStatus("failed", "Speed is not in the limit of 2 Mbp", driver); + } + + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/InterceptNetwork.java b/src/test/java/com/lambdatest/InterceptNetwork.java new file mode 100644 index 0000000..770c0c3 --- /dev/null +++ b/src/test/java/com/lambdatest/InterceptNetwork.java @@ -0,0 +1,107 @@ +package com.lambdatest; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.function.Supplier; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.NetworkInterceptor; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.remote.http.HttpResponse; +import org.openqa.selenium.remote.http.Route; + + + + + +public class InterceptNetwork { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void interceptNetwork() { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + Supplier<InputStream> message = () -> new ByteArrayInputStream( + "Creamy, delicious cheese!".getBytes(StandardCharsets.UTF_8)); + + NetworkInterceptor interceptor = new NetworkInterceptor( + driver, + Route.matching(req -> true) + .to(() -> req -> new HttpResponse() + .setStatus(200) + .addHeader("Content-Type", StandardCharsets.UTF_8.toString()) + .setContent(message))); + driver.get("https://example-sausages-site.com"); + String source = driver.getPageSource(); + System.out.println(source); + + if (source.contains("delicious cheese!")) { + markStatus("passed", "Source contains the contents", driver); + } else { + markStatus("failed", "Content was not found in the source", driver); + } + interceptor.close(); + + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/JUnitConcurrentTodo.java b/src/test/java/com/lambdatest/JUnitConcurrentTodo.java index 6cf7f8d..b39be5b 100644 --- a/src/test/java/com/lambdatest/JUnitConcurrentTodo.java +++ b/src/test/java/com/lambdatest/JUnitConcurrentTodo.java @@ -1,4 +1,5 @@ package com.lambdatest; + import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; @@ -10,43 +11,46 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.LinkedList; - + @RunWith(Parallelized.class) public class JUnitConcurrentTodo { String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); public String gridURL = "@hub.lambdatest.com/wd/hub"; - - public String platform; - public String browserName; - public String browserVersion; + + public String platform; + public String browserName; + public String browserVersion; public RemoteWebDriver driver = null; - public String status = "failed"; - - @Parameterized.Parameters - public static LinkedList<String[]> getEnvironments() throws Exception { + public String status = "failed"; + + @Parameterized.Parameters + public static LinkedList<String[]> getEnvironments() throws Exception { LinkedList<String[]> env = new LinkedList<String[]>(); - env.add(new String[]{"Windows 10", "chrome", "latest"}); - env.add(new String[]{"Windows 10","firefox","latest"}); - env.add(new String[]{"Windows 10","internet explorer","latest"}); + env.add(new String[] { "Windows 10", "chrome", "latest" }); + env.add(new String[] { "Windows 10", "firefox", "latest" }); + env.add(new String[] { "Windows 10", "internet explorer", "latest" }); return env; } - + public JUnitConcurrentTodo(String platform, String browserName, String browserVersion) { this.platform = platform; this.browserName = browserName; this.browserVersion = browserVersion; - } + } + @Before public void setUp() throws Exception { - DesiredCapabilities capabilities = new DesiredCapabilities(); + DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", browserName); capabilities.setCapability("version", browserVersion); - capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one + capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any + // available one capabilities.setCapability("build", "JUnitParallelSample"); capabilities.setCapability("name", "JUnitParallelSampleTest"); // capabilities.setCapability("network", true); // To enable network logs - // capabilities.setCapability("visual", true); // To enable step by step screenshot + // capabilities.setCapability("visual", true); // To enable step by step + // screenshot // capabilities.setCapability("video", true); // To enable video recording // capabilities.setCapability("console", true); // To capture console logs try { @@ -57,31 +61,36 @@ public void setUp() throws Exception { System.out.println(e.getMessage()); } } + @Test public void testParallel() throws Exception { - try { - //Change it to production page + try { + // Change it to production page driver.get("https://lambdatest.github.io/sample-todo-app/"); - //Let's mark done first two items in the list. - driver.findElement(By.name("li1")).click(); + // Let's mark done first two items in the list. + driver.findElement(By.name("li1")).click(); driver.findElement(By.name("li2")).click(); - - // Let's add an item in the list. - driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); + + // Let's add an item in the list. + driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); driver.findElement(By.id("addbutton")).click(); - - // Let's check that the item we added is added in the list. - String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); + + // Let's check that the item we added is added in the list. + String enteredText = driver.findElement(By + .xpath( + "/html/body/div/div/div/ul/li[6]/span")) + .getText(); if (enteredText.equals("Yey, Let's add it to list")) { status = "passed"; } } catch (Exception e) { System.out.println(e.getMessage()); } - } + } + @After public void tearDown() throws Exception { - if (driver != null) { + if (driver != null) { driver.executeScript("lambda-status=" + status); driver.quit(); } diff --git a/src/test/java/com/lambdatest/JUnitTodo.java b/src/test/java/com/lambdatest/JUnitTodo.java deleted file mode 100644 index f9e608d..0000000 --- a/src/test/java/com/lambdatest/JUnitTodo.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.lambdatest; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.By; - -import java.net.MalformedURLException; -import java.net.URL; - -public class JUnitTodo { - String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); - String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); - public static RemoteWebDriver driver = null; - public String gridURL = "@hub.lambdatest.com/wd/hub"; - public String status = "failed"; - @Before - public void setUp() throws Exception { - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserName", "chrome"); - capabilities.setCapability("version", "latest"); - capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any available one - capabilities.setCapability("build", "LambdaTestSampleApp"); - capabilities.setCapability("name", "LambdaTestJavaSample"); - // capabilities.setCapability("network", true); // To enable network logs - // capabilities.setCapability("visual", true); // To enable step by step screenshot - // capabilities.setCapability("video", true); // To enable video recording - // capabilities.setCapability("console", true); // To capture console logs - try { - driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + gridURL), capabilities); - } catch (MalformedURLException e) { - System.out.println("Invalid grid URL"); - } catch (Exception e) { - System.out.println(e.getMessage()); - } - } - - @Test - public void testSimple() throws Exception { - try { - //Change it to production page - driver.get("https://lambdatest.github.io/sample-todo-app/"); - - //Let's mark done first two items in the list. - driver.findElement(By.name("li1")).click(); - driver.findElement(By.name("li2")).click(); - - // Let's add an item in the list. - driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); - driver.findElement(By.id("addbutton")).click(); - - // Let's check that the item we added is added in the list. - String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); - if (enteredText.equals("Yey, Let's add it to list")) { - status = "passed"; - } - } catch (Exception e) { - System.out.println(e.getMessage()); - } - } - @After - public void tearDown() throws Exception { - if (driver != null) { - driver.executeScript("lambda-status=" + status); - driver.quit(); - } - } -} - diff --git a/src/test/java/com/lambdatest/JavaScriptExceptions.java b/src/test/java/com/lambdatest/JavaScriptExceptions.java new file mode 100644 index 0000000..e4d869d --- /dev/null +++ b/src/test/java/com/lambdatest/JavaScriptExceptions.java @@ -0,0 +1,109 @@ +package com.lambdatest; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptException; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + + +public class JavaScriptExceptions { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + static Boolean success = false; + + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void javaScriptExceptions() throws InterruptedException { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + List<JavascriptException> jsExceptionsList = new ArrayList<>(); + Consumer<JavascriptException> addEntry = jsExceptionsList::add; + devTools.getDomains().events().addJavascriptExceptionListener(addEntry); + + driver.get("https://facebook.com"); + + WebElement link2click = driver.findElement(By.name("login")); + ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", + link2click, "onclick", "throw new Error('My JS Error');"); + link2click.click(); + + Thread.sleep(1000); + for (JavascriptException jsException : jsExceptionsList) { + System.out.println("My JS exception message: " + jsException.getMessage()); + System.out.println("My JS exception system: " + jsException.getSystemInformation()); + jsException.printStackTrace(); + success = true; + } + Thread.sleep(1000); + if (success) { + markStatus("passed", "Got JS exception", driver); + } else { + markStatus("failed", "Didn't got JS exception", driver); + } + + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/OverrideDeviceMode.java b/src/test/java/com/lambdatest/OverrideDeviceMode.java new file mode 100644 index 0000000..c7d2e84 --- /dev/null +++ b/src/test/java/com/lambdatest/OverrideDeviceMode.java @@ -0,0 +1,95 @@ +package com.lambdatest; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.Optional; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v97.emulation.Emulation; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + + + +public class OverrideDeviceMode { + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; + + private WebDriver driver; + + + + @Before + public void setup() throws MalformedURLException { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "97"); + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name",this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void overrideDevicemode() { + Augmenter augmenter = new Augmenter(); + driver = augmenter.augment(driver); + + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + devTools.send(Emulation.setDeviceMetricsOverride(414, 736, 50, true, Optional.empty(), Optional.empty(), + Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), + Optional.empty(), Optional.empty())); + driver.get("http://whatismyscreenresolution.net/"); + String resolution = driver.findElement(By.id("resolution")).getText(); + + System.out.println(resolution); + if (resolution.equals("414x736")) { + markStatus("passed", "Found screen resolution: 414x736", driver); + } else { + markStatus("failed", "Not found screen resolution: 414x736", driver); + } + } + + @After + public void tearDown() { + try { + driver.quit(); + + } catch ( + + Exception e) { + markStatus("failed", "Got exception!", driver); + e.printStackTrace(); + driver.quit(); + } + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/RelativeLocator.java b/src/test/java/com/lambdatest/RelativeLocator.java new file mode 100644 index 0000000..54fa518 --- /dev/null +++ b/src/test/java/com/lambdatest/RelativeLocator.java @@ -0,0 +1,110 @@ +package com.lambdatest; + +import static org.openqa.selenium.support.locators.RelativeLocator.with; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + + +public class RelativeLocator { + + private WebDriver driver; + + @Before + public void setup() throws MalformedURLException { + + String hubURL = "https://hub.lambdatest.com/wd/hub"; + + String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; + + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); + ltOptions.put("user", System.getenv("LT_USERNAME")); + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); + ltOptions.put("build", "Selenium 4"); + ltOptions.put("name", this.getClass().getName()); + ltOptions.put("platformName", "Windows 10"); + ltOptions.put("seCdp", true); + ltOptions.put("selenium_version", "4.0.0"); + ltOptions.put("tags", Tags); + capabilities.setCapability("LT:Options", ltOptions); + + driver = new RemoteWebDriver(new URL(hubURL), capabilities); + System.out.println(driver); + } + + @Test + public void relativeLocator() throws InterruptedException { + + driver.get("http://cookbook.seleniumacademy.com/mobilebmicalculator.html"); + + // find the height and weight labels using css selector + WebElement heightLabel = driver.findElement(By.cssSelector("label[for='heightCMS']")); + WebElement weightLabel = driver.findElement(By.cssSelector("label[for='weightKg']")); + + // find the height input using toRightOf relative locator + // input is right of height label + WebElement heightInput = driver.findElement(with(By.tagName("input")) + .toRightOf(heightLabel)); + + heightInput.sendKeys("181"); + Thread.sleep(3000); + + // find the weight input using combination of below and toRightOf relative + // locator + // weight input is below height input and right of weight label + WebElement weightInput = driver.findElement(with(By.tagName("input")) + .below(heightInput).toRightOf(weightLabel)); + + weightInput.sendKeys("75"); + Thread.sleep(3000); + + // find the calculate button which is below the weight label + WebElement calculateButton = driver.findElement(with(By.tagName("input")) + .below(weightLabel)); + + calculateButton.click(); + Thread.sleep(3000); + + // find the read only input below calculate button to verify value + if ("22.9".equalsIgnoreCase(driver.findElement(with(By.tagName("input")) + .below(calculateButton)).getAttribute("value"))) { + markStatus("passed", "Value Verified", driver); + Thread.sleep(150); + + System.out.println("TestFinished"); + } else { + markStatus("failed", "Wrong calculation", driver); + } + + } + + @After + public void tearDown() { + driver.quit(); + } + + public static void markStatus(String status, String reason, WebDriver driver) { + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; + jsExecute.executeScript("lambda-status=" + status); + System.out.println(reason); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lambdatest/toggleNetworkOffline.java b/src/test/java/com/lambdatest/toggleNetworkOffline.java new file mode 100644 index 0000000..d28a844 --- /dev/null +++ b/src/test/java/com/lambdatest/toggleNetworkOffline.java @@ -0,0 +1,89 @@ +package com.lambdatest; + + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Optional; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.chromium.ChromiumNetworkConditions; +import org.openqa.selenium.chromium.HasNetworkConditions; +import org.openqa.selenium.devtools.DevTools; +import org.openqa.selenium.devtools.HasDevTools; +import org.openqa.selenium.devtools.v97.browser.Browser; +import org.openqa.selenium.devtools.v97.browser.model.Bounds; +import org.openqa.selenium.devtools.v97.browser.model.WindowID; +import org.openqa.selenium.devtools.v97.browser.model.WindowState; +import org.openqa.selenium.devtools.v97.log.Log; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + + + +public class toggleNetworkOffline { + + private WebDriver driver; + private String Status = "failed"; + + + @Before + public void setup() throws MalformedURLException { + String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); + String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); + ; + String hub = "@hub.lambdatest.com/wd/hub"; + + DesiredCapabilities caps = new DesiredCapabilities(); + caps.setCapability("platform", "Windows 10"); + caps.setCapability("browserName", "chrome"); + caps.setCapability("version", "97"); + caps.setCapability("build", "Selenium 4"); + caps.setCapability("name", this.getClass().getName()); + caps.setCapability("plugin", "git-junit"); + caps.setCapability("selenium_version", "4.0.0"); + + String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; + caps.setCapability("tags", Tags); + driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); + + } + + @Test + public void toggleOffline() throws InterruptedException { + + driver = new Augmenter().augment(driver); + + driver.get("https://www.duckduckgo.com"); + DevTools devTools = ((HasDevTools) driver).getDevTools(); + devTools.createSession(); + + devTools.send(Log.enable()); + devTools.send(Browser.setWindowBounds(new WindowID(1), new Bounds(Optional.of(20), Optional.of(20), + Optional.of(20), Optional.of(20), Optional.of(WindowState.NORMAL)))); + + Thread.sleep(30000); + + WebDriver augmentedDriver = new Augmenter().augment(driver); + ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions(); + networkConditions.setOffline(true); + ((HasNetworkConditions) augmentedDriver).setNetworkConditions(networkConditions); + + try { + driver.get("https://www.lambdatest.com"); + // "If Network is set to be offline, the previous line should throw an + // exception"; + } catch (WebDriverException ex) { + ((HasNetworkConditions) augmentedDriver).setNetworkConditions(new ChromiumNetworkConditions()); + } + driver.get("https://www.lambdatest.com"); + } + + @After + public void tearDown() { + ((RemoteWebDriver) driver).executeScript("lambda-status=" + Status); + driver.quit(); + } + +} \ No newline at end of file