public static void setAttribute(WebElement element, String attributeName, String value) { WrapsDriver wrappedElement = (WrapsDriver) element; JavascriptExecutor driver = (JavascriptExecutor) wrappedElement.getWrappedDriver(); driver.executeScript( "arguments[0].setAttribute(arguments[1], arguments[2])", element, attributeName, value); }
public void selectTab(String tabName) { try { int idx = 0; boolean found = false; List<WebElement> tabs = _jQueryUITab.findElements(By.cssSelector(".ui-tabs-nav > li")); for (WebElement tab : tabs) { if (tabName.equals(tab.getText().toString())) { WrapsDriver wrappedElement = (WrapsDriver) _jQueryUITab; JavascriptExecutor driver = (JavascriptExecutor) wrappedElement.getWrappedDriver(); driver.executeScript( "jQuery(arguments[0]).tabs().tabs('select',arguments[1]);", _jQueryUITab, idx); found = true; break; } idx++; } if (found == false) { throw new Exception("Could not find tab '" + tabName + "'"); } } catch (Exception e) { // TODO: handle exception } }
@Override public void mouseMoveAt(String locator, String coordString) { WebElement webElement = getWebElement(locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); if (coordString.contains(",")) { String[] coords = coordString.split(","); int x = GetterUtil.getInteger(coords[0]); int y = GetterUtil.getInteger(coords[1]); actions.moveToElement(webElement, x, y); actions.clickAndHold(webElement); } else { actions.moveToElement(webElement); actions.clickAndHold(webElement); } Action action = actions.build(); action.perform(); }
public static void typeAlloyEditor(WebDriver webDriver, String locator, String value) { WebElement webElement = getWebElement(webDriver, locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; StringBuilder sb = new StringBuilder(); sb.append("CKEDITOR.instances[\""); String titleAttribute = getAttribute(webDriver, locator + "@title"); int x = titleAttribute.indexOf(","); int y = titleAttribute.indexOf(",", x + 1); if (y == -1) { y = titleAttribute.length(); } sb.append(titleAttribute.substring(x + 2, y)); sb.append("\"].setData(\""); sb.append(HtmlUtil.escapeJS(value.replace("\\", "\\\\"))); sb.append("\");"); javascriptExecutor.executeScript(sb.toString()); }
public static void executeJavaScriptEvent( WebDriver webDriver, String locator, String eventType, String event) { WebElement webElement = getWebElement(webDriver, locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; if (!webElement.isDisplayed()) { scrollWebElementIntoView(webDriver, webElement); } StringBuilder sb = new StringBuilder(6); sb.append("var element = arguments[0];"); sb.append("var event = document.createEvent('"); sb.append(eventType); sb.append("');event.initEvent('"); sb.append(event); sb.append("', true, false);"); sb.append("element.dispatchEvent(event);"); javascriptExecutor.executeScript(sb.toString(), webElement); }
@Override public void keyUp(String locator, String keySequence) { WebElement webElement = getWebElement(locator); if (keySequence.startsWith("\\")) { int index = GetterUtil.getInteger(keySequence.substring(1)); Keys keys = _keysArray[index]; if ((index >= 48) && (index <= 90)) { webElement.sendKeys(StringPool.ASCII_TABLE[index]); } else if ((index == 16) || (index == 17) || (index == 18)) { WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); actions.keyUp(webElement, keys); Action action = actions.build(); action.perform(); } else { webElement.sendKeys(keys); } } else { webElement.sendKeys(keySequence); } }
public static Point getFramePoint(WebDriver webDriver) { int x = 0; int y = 0; WebElement bodyWebElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); WebDriver.TargetLocator targetLocator = wrappedWebDriver.switchTo(); targetLocator.window(_defaultWindowHandle); for (WebElement webElement : _frameWebElements) { Point point = webElement.getLocation(); x += point.getX(); y += point.getY(); targetLocator.frame(webElement); } return new Point(x, y); }
public static File captureElementBitmap(WebElement element) throws Exception { // Get the WrapsDriver of the WebElement WrapsDriver wrapsDriver = (WrapsDriver) element; // Get the entire Screenshot from the driver of passed WebElement File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE); // Create an instance of Buffered Image from captured screenshot BufferedImage img = ImageIO.read(screen); // Get the Width and Height of the WebElement using getSize() int width = element.getSize().getWidth(); int height = element.getSize().getHeight(); // Create a rectangle using Width and Height Rectangle rect = new Rectangle(width, height); // Get the Location of WebElement in a Point. // This will provide X & Y co-ordinates of the WebElement Point p = element.getLocation(); // Create image by for element using its location and size. // This will give image data specific to the WebElement BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height); // Write back the image data for element in File object ImageIO.write(dest, "png", screen); // Return the File object containing image data return screen; }
@Override public void clickAt(String locator, String coordString) { if (locator.contains("x:")) { String url = getHtmlNodeHref(locator); open(url); } else { WebElement webElement = getWebElement(locator); if (coordString.contains(",")) { WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); String[] coords = coordString.split(","); int x = GetterUtil.getInteger(coords[0]); int y = GetterUtil.getInteger(coords[1]); actions.moveToElement(webElement, x, y); actions.click(); Action action = actions.build(); action.perform(); } else { webElement.click(); } } }
protected static void scrollWebElementIntoView(WebDriver webDriver, WebElement webElement) { WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; javascriptExecutor.executeScript("arguments[0].scrollIntoView();", webElement); }
public static int getViewportHeight(WebDriver webDriver) { WebElement bodyWebElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; return GetterUtil.getInteger(javascriptExecutor.executeScript("return window.innerHeight;")); }
public static void scrollBy(WebDriver webDriver, String coordString) { WebElement webElement = getWebElement(webDriver, "//html"); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; javascriptExecutor.executeScript("window.scrollBy(" + coordString + ");"); }
public static String getEval(WebDriver webDriver, String script) { WebElement webElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; return (String) javascriptExecutor.executeScript(script); }
public static void highlightElement(WebElement element) { for (int i = 0; i < 5; i++) { WrapsDriver wrappedElement = (WrapsDriver) element; JavascriptExecutor driver = (JavascriptExecutor) wrappedElement.getWrappedDriver(); driver.executeScript( "arguments[0].setAttribute('style', arguments[1]);", element, "color: green; border: 2px solid green;"); driver.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); } }
protected static void scrollWebElementIntoView(WebDriver webDriver, WebElement webElement) { if (!webElement.isDisplayed() || isObscured(webDriver, webElement)) { WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; javascriptExecutor.executeScript("arguments[0].scrollIntoView(false);", webElement); } }
public static Point getWindowPoint(WebDriver webDriver) { WebElement bodyWebElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); WebDriver.Options options = wrappedWebDriver.manage(); WebDriver.Window window = options.window(); return window.getPosition(); }
public static int getScrollOffsetY(WebDriver webDriver) { WebElement bodyWebElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; Object pageYOffset = javascriptExecutor.executeScript("return window.pageYOffset;"); return GetterUtil.getInteger(pageYOffset); }
@Override public void mouseRelease() { WebElement bodyWebElement = getWebElement("//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); actions.release(); Action action = actions.build(); action.perform(); }
@Override public void doubleClick(String locator) { WebElement webElement = getWebElement(locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); actions.doubleClick(webElement); Action action = actions.build(); action.perform(); }
public static void typeEditor(WebDriver webDriver, String locator, String value) { WrapsDriver wrapsDriver = (WrapsDriver) getWebElement(webDriver, locator); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrapsDriver.getWrappedDriver(); StringBuilder sb = new StringBuilder(); sb.append("CKEDITOR.instances[\""); sb.append(getEditorName(webDriver, locator)); sb.append("\"].setData(\""); sb.append(HtmlUtil.escapeJS(value.replace("\\", "\\\\"))); sb.append("\");"); javascriptExecutor.executeScript(sb.toString()); }
@Override public void mouseDown(String locator) { WebElement webElement = getWebElement(locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); actions.moveToElement(webElement); actions.clickAndHold(webElement); Action action = actions.build(); action.perform(); }
@Override public void mouseOut(String locator) { WebElement webElement = getWebElement(locator); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); actions.moveToElement(webElement); actions.moveByOffset(10, 10); Action action = actions.build(); action.perform(); }
@Override public void setWindowSize(String coordString) { WebElement bodyWebElement = getWebElement("//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); WebDriver.Options options = webDriver.manage(); WebDriver.Window window = options.window(); String[] screenResolution = StringUtil.split(coordString, ","); int x = GetterUtil.getInteger(screenResolution[0]); int y = GetterUtil.getInteger(screenResolution[1]); window.setSize(new Dimension(x, y)); }
@Override public void dragAndDropToObject( String locatorOfObjectToBeDragged, String locatorOfDragDestinationObject) { WebElement objectToBeDraggedWebElement = getWebElement(locatorOfObjectToBeDragged); WrapsDriver wrapsDriver = (WrapsDriver) objectToBeDraggedWebElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); Actions actions = new Actions(webDriver); WebElement dragDestinationObjectWebElement = getWebElement(locatorOfDragDestinationObject); actions.dragAndDrop(objectToBeDraggedWebElement, dragDestinationObjectWebElement); Action action = actions.build(); action.perform(); }
public static void makeVisible(WebDriver webDriver, String locator) { WebElement bodyWebElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver; StringBuilder sb = new StringBuilder(); sb.append("var element = arguments[0];"); sb.append("element.style.cssText = 'display:inline !important';"); sb.append("element.style.overflow = 'visible';"); sb.append("element.style.minHeight = '1px';"); sb.append("element.style.minWidth = '1px';"); sb.append("element.style.opacity = '1';"); sb.append("element.style.visibility = 'visible';"); WebElement locatorWebElement = getWebElement(webDriver, locator); javascriptExecutor.executeScript(sb.toString(), locatorWebElement); }
protected static boolean isObscured(WebDriver webDriver, WebElement webElement) { WrapsDriver wrapsDriver = (WrapsDriver) webElement; JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrapsDriver.getWrappedDriver(); StringBuilder sb = new StringBuilder(); sb.append("var element = arguments[0];"); sb.append("console.log(element);"); sb.append("var rect = element.getBoundingClientRect();"); sb.append("elementX = (rect.right + rect.left) / 2;"); sb.append("elementY = (rect.top + rect.bottom) / 2;"); sb.append("var newElement = "); sb.append("document.elementFromPoint(elementX, elementY);"); sb.append("if (element == newElement) {"); sb.append("return false;}"); sb.append("return true;"); Boolean isObscured = (Boolean) javascriptExecutor.executeScript(sb.toString(), webElement); return isObscured.booleanValue(); }
@Override public void assertJavaScriptErrors(String ignoreJavaScriptError) throws Exception { if (!TestPropsValues.TEST_ASSSERT_JAVASCRIPT_ERRORS) { return; } String location = getLocation(); if (!location.contains("localhost")) { return; } String pageSource = null; try { pageSource = getPageSource(); } catch (Exception e) { WebDriver.TargetLocator targetLocator = switchTo(); targetLocator.window(defaultWindowHandle); pageSource = getPageSource(); } if (pageSource.contains( "html id=\"feedHandler\" xmlns=" + "\"http://www.w3.org/1999/xhtml\"")) { return; } WebElement webElement = getWebElement("//body"); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver webDriver = wrapsDriver.getWrappedDriver(); List<JavaScriptError> javaScriptErrors = JavaScriptError.readErrors(webDriver); if (!javaScriptErrors.isEmpty()) { for (JavaScriptError javaScriptError : javaScriptErrors) { String javaScriptErrorValue = javaScriptError.toString(); System.out.println("JS_ERROR: " + javaScriptErrorValue); if (Validator.isNotNull(ignoreJavaScriptError) && javaScriptErrorValue.contains(ignoreJavaScriptError)) { continue; } // LPS-41634 if (javaScriptErrorValue.contains("TypeError: d.config.doc.defaultView is null")) { continue; } // LPS-41634 if (javaScriptErrorValue.contains("NS_ERROR_NOT_INITIALIZED:")) { continue; } // LPS-42469 if (javaScriptErrorValue.contains("https://apis.google.com/_/+1/fastbutton")) { continue; } throw new Exception(javaScriptErrorValue); } } }
public static void assertJavaScriptErrors(WebDriver webDriver, String ignoreJavaScriptError) throws Exception { if (!PropsValues.TEST_ASSERT_JAVASCRIPT_ERRORS) { return; } String location = getLocation(webDriver); if (!location.contains("localhost")) { return; } String pageSource = null; try { pageSource = webDriver.getPageSource(); } catch (Exception e) { WebDriver.TargetLocator targetLocator = webDriver.switchTo(); targetLocator.window(_defaultWindowHandle); pageSource = webDriver.getPageSource(); } if (pageSource.contains( "html id=\"feedHandler\" xmlns=" + "\"http://www.w3.org/1999/xhtml\"")) { return; } List<JavaScriptError> javaScriptErrors = new ArrayList<>(); try { WebElement webElement = getWebElement(webDriver, "//body"); WrapsDriver wrapsDriver = (WrapsDriver) webElement; WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver(); javaScriptErrors.addAll(JavaScriptError.readErrors(wrappedWebDriver)); } catch (Exception e) { } List<Exception> exceptions = new ArrayList<>(); if (!javaScriptErrors.isEmpty()) { for (JavaScriptError javaScriptError : javaScriptErrors) { String javaScriptErrorValue = javaScriptError.toString(); if (Validator.isNotNull(ignoreJavaScriptError) && javaScriptErrorValue.contains(ignoreJavaScriptError)) { continue; } if (LiferaySeleniumHelper.isInIgnoreErrorsFile(javaScriptErrorValue, "javascript")) { continue; } String message = "JAVA_SCRIPT_ERROR: " + javaScriptErrorValue; System.out.println(message); exceptions.add(new PoshiRunnerWarningException(message)); } } if (!exceptions.isEmpty()) { LiferaySeleniumHelper.addToJavaScriptExceptions(exceptions); throw exceptions.get(0); } }