|
| 1 | +package com.lambdatest; |
| 2 | + |
| 3 | + |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.HashMap; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +import org.openqa.selenium.By; |
| 13 | +import org.openqa.selenium.JavascriptException; |
| 14 | +import org.openqa.selenium.JavascriptExecutor; |
| 15 | +import org.openqa.selenium.WebDriver; |
| 16 | +import org.openqa.selenium.WebElement; |
| 17 | +import org.openqa.selenium.devtools.DevTools; |
| 18 | +import org.openqa.selenium.devtools.HasDevTools; |
| 19 | +import org.openqa.selenium.remote.Augmenter; |
| 20 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 21 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 22 | + |
| 23 | + |
| 24 | +public class JavaScriptExceptions { |
| 25 | + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; |
| 26 | + static Boolean success = false; |
| 27 | + |
| 28 | + private WebDriver driver; |
| 29 | + |
| 30 | + public void setup() throws MalformedURLException { |
| 31 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 32 | + capabilities.setCapability("browserName", "Chrome"); |
| 33 | + capabilities.setCapability("browserVersion", "latest"); |
| 34 | + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); |
| 35 | + ltOptions.put("user", System.getenv("LT_USERNAME")); |
| 36 | + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); |
| 37 | + ltOptions.put("build", "Selenium 4"); |
| 38 | + ltOptions.put("name",this.getClass().getName()); |
| 39 | + ltOptions.put("platformName", "Windows 10"); |
| 40 | + ltOptions.put("seCdp", true); |
| 41 | + ltOptions.put("selenium_version", "4.0.0"); |
| 42 | + capabilities.setCapability("LT:Options", ltOptions); |
| 43 | + |
| 44 | + driver = new RemoteWebDriver(new URL(hubURL), capabilities); |
| 45 | + System.out.println(driver); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public void javaScriptExceptions() throws InterruptedException { |
| 50 | + Augmenter augmenter = new Augmenter(); |
| 51 | + driver = augmenter.augment(driver); |
| 52 | + |
| 53 | + DevTools devTools = ((HasDevTools) driver).getDevTools(); |
| 54 | + devTools.createSession(); |
| 55 | + |
| 56 | + List<JavascriptException> jsExceptionsList = new ArrayList<>(); |
| 57 | + Consumer<JavascriptException> addEntry = jsExceptionsList::add; |
| 58 | + devTools.getDomains().events().addJavascriptExceptionListener(addEntry); |
| 59 | + |
| 60 | + driver.get("https://facebook.com"); |
| 61 | + |
| 62 | + WebElement link2click = driver.findElement(By.name("login")); |
| 63 | + ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", |
| 64 | + link2click, "onclick", "throw new Error('My JS Error');"); |
| 65 | + link2click.click(); |
| 66 | + |
| 67 | + Thread.sleep(1000); |
| 68 | + for (JavascriptException jsException : jsExceptionsList) { |
| 69 | + System.out.println("My JS exception message: " + jsException.getMessage()); |
| 70 | + System.out.println("My JS exception system: " + jsException.getSystemInformation()); |
| 71 | + jsException.printStackTrace(); |
| 72 | + success = true; |
| 73 | + } |
| 74 | + Thread.sleep(1000); |
| 75 | + if (success) { |
| 76 | + markStatus("passed", "Got JS exception", driver); |
| 77 | + } else { |
| 78 | + markStatus("failed", "Didn't got JS exception", driver); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + public void tearDown() { |
| 84 | + try { |
| 85 | + driver.quit(); |
| 86 | + |
| 87 | + } catch ( |
| 88 | + |
| 89 | + Exception e) { |
| 90 | + markStatus("failed", "Got exception!", driver); |
| 91 | + e.printStackTrace(); |
| 92 | + driver.quit(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static void markStatus(String status, String reason, WebDriver driver) { |
| 97 | + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; |
| 98 | + jsExecute.executeScript("lambda-status=" + status); |
| 99 | + System.out.println(reason); |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) throws MalformedURLException, InterruptedException { |
| 103 | + JavaScriptExceptions test = new JavaScriptExceptions(); |
| 104 | + test.setup(); |
| 105 | + test.javaScriptExceptions(); |
| 106 | + test.tearDown(); |
| 107 | + } |
| 108 | +} |
0 commit comments