JavaScriptAction extends AbstractSeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { try { if (browser.getWebDriver() instanceof JavascriptExecutor) { JavascriptExecutor jsEngine = ((JavascriptExecutor) browser.getWebDriver()); jsEngine.executeScript(context.replaceDynamicContentInString(script), context.resolveDynamicValuesInArray(arguments.toArray())); List<String> errors = new ArrayList<>(); List<Object> errorObjects = (List<Object>) jsEngine.executeScript("return window._selenide_jsErrors", new Object[]{}); if (errorObjects != null) { for (Object error : errorObjects) { errors.add(error.toString()); } } context.setVariable(SeleniumHeaders.SELENIUM_JS_ERRORS, errors); for (String expected : expectedErrors) { if (!errors.contains(expected)) { throw new ValidationException("Missing JavaScript error " + expected); } } } else { log.warn("Skip javascript action because web driver is missing javascript features"); } } catch (WebDriverException e) { throw new CitrusRuntimeException("Failed to execute JavaScript code", e); } } JavaScriptAction(Builder builder); String getScript(); List<Object> getArguments(); List<String> getExpectedErrors();  }
@Test public void testExecute() throws Exception { when(webDriver.executeScript(eq("return window._selenide_jsErrors"))).thenReturn(Collections.emptyList()); JavaScriptAction action = new JavaScriptAction.Builder() .browser(seleniumBrowser) .script("alert('Hello')") .build(); action.execute(context); Assert.assertNotNull(context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)); Assert.assertEquals(((List) context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)).size(), 0L); verify(webDriver).executeScript(eq("alert('Hello')")); }
@Test public void testExecuteVariableSupport() throws Exception { when(webDriver.executeScript(eq("return window._selenide_jsErrors"))).thenReturn(Collections.emptyList()); context.setVariable("text", "Hello"); JavaScriptAction action = new JavaScriptAction.Builder() .browser(seleniumBrowser) .script("alert('${text}')") .build(); action.execute(context); Assert.assertNotNull(context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)); Assert.assertEquals(((List) context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)).size(), 0L); verify(webDriver).executeScript(eq("alert('Hello')")); }
@Test public void testExecuteWithErrorValidation() throws Exception { when(webDriver.executeScript(eq("return window._selenide_jsErrors"))).thenReturn(Collections.singletonList("This went totally wrong!")); JavaScriptAction action = new JavaScriptAction.Builder() .browser(seleniumBrowser) .script("alert('Hello')") .errors("This went totally wrong!") .build(); action.execute(context); Assert.assertNotNull(context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)); Assert.assertEquals(((List) context.getVariableObject(SeleniumHeaders.SELENIUM_JS_ERRORS)).size(), 1L); verify(webDriver).executeScript(eq("alert('Hello')")); }
@Test(expectedExceptions = ValidationException.class) public void testExecuteWithErrorValidationFailed() throws Exception { when(webDriver.executeScript(eq("return window._selenide_jsErrors"))).thenReturn(Collections.emptyList()); JavaScriptAction action = new JavaScriptAction.Builder() .browser(seleniumBrowser) .script("alert('Hello')") .errors("This went totally wrong!") .build(); action.execute(context); }