How to use getBy method of org.cerberus.service.webdriver.impl.WebDriverService class

Best Cerberus-source code snippet using org.cerberus.service.webdriver.impl.WebDriverService.getBy

Source:WebDriverService.java Github

copy

Full Screen

...92 private static final int TIMEOUT_FOCUS = 1000;9394 private static final Logger LOG = LogManager.getLogger("WebDriverService");9596 private By getBy(Identifier identifier) {9798 LOG.debug("Finding selenium Element : " + identifier.getLocator() + " by : " + identifier.getIdentifier());99100 if (identifier.getIdentifier().equalsIgnoreCase("id")) {101 return By.id(identifier.getLocator());102103 } else if (identifier.getIdentifier().equalsIgnoreCase("name")) {104 return By.name(identifier.getLocator());105106 } else if (identifier.getIdentifier().equalsIgnoreCase("class")) {107 return By.className(identifier.getLocator());108109 } else if (identifier.getIdentifier().equalsIgnoreCase("css")) {110 return By.cssSelector(identifier.getLocator());111112 } else if (identifier.getIdentifier().equalsIgnoreCase("xpath")) {113 return By.xpath(identifier.getLocator());114115 } else if (identifier.getIdentifier().equalsIgnoreCase("link")) {116 return By.linkText(identifier.getLocator());117118 } else if (identifier.getIdentifier().equalsIgnoreCase("data-cerberus")) {119 return By.xpath("//*[@data-cerberus='" + identifier.getLocator() + "']");120121 } else {122 throw new NoSuchElementException(identifier.getIdentifier());123 }124 }125126 private AnswerItem<WebElement> getSeleniumElement(Session session, Identifier identifier, boolean visible, boolean clickable) {127 AnswerItem<WebElement> answer = new AnswerItem<WebElement>();128 MessageEvent msg;129 By locator = this.getBy(identifier);130 LOG.debug("Waiting for Element : " + identifier.getIdentifier() + "=" + identifier.getLocator());131 try {132 WebDriverWait wait = new WebDriverWait(session.getDriver(), TimeUnit.MILLISECONDS.toSeconds(session.getCerberus_selenium_wait_element()));133 WebElement element;134 if (visible) {135 if (clickable) {136 element = wait.until(ExpectedConditions.elementToBeClickable(locator));137 } else {138 element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));139 }140 } else {141 element = wait.until(ExpectedConditions.presenceOfElementLocated(locator));142 }143 answer.setItem(element);144 msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT);145 msg.setDescription(msg.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));146 } catch (TimeoutException exception) {147 LOG.warn("Exception waiting for element :" + exception);148 //throw new NoSuchElementException(identifier.getIdentifier() + "=" + identifier.getLocator());149 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);150 msg.setDescription(msg.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));151 }152 answer.setResultMessage(msg);153 return answer;154 }155156 @Override157 public String getValueFromHTMLVisible(Session session, Identifier identifier) {158 String result = null;159 AnswerItem answer = this.getSeleniumElement(session, identifier, true, false);160 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {161 WebElement webElement = (WebElement) answer.getItem();162 if (webElement != null) {163 if (webElement.getTagName().equalsIgnoreCase("select")) {164 Select select = (Select) webElement;165 result = select.getFirstSelectedOption().getText();166 } else if (webElement.getTagName().equalsIgnoreCase("option") || webElement.getTagName().equalsIgnoreCase("input")) {167 result = webElement.getAttribute("value");168 } else {169 result = webElement.getText();170 }171 }172 }173174 return result;175 }176177 @Override178 public String getValueFromHTML(Session session, Identifier identifier) {179 AnswerItem answer = this.getSeleniumElement(session, identifier, false, false);180 String result = null;181 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {182 WebElement webElement = (WebElement) answer.getItem();183 if (webElement != null) {184 if (webElement.getTagName().equalsIgnoreCase("select")) {185 if (webElement.getAttribute("disabled") == null || webElement.getAttribute("disabled").isEmpty()) {186 Select select = new Select(webElement);187 result = select.getFirstSelectedOption().getText();188 } else {189 result = webElement.getText();190 //result = "Unable to retrieve, element disabled ?";191 }192 } else if (webElement.getTagName().equalsIgnoreCase("option") || webElement.getTagName().equalsIgnoreCase("input")) {193 result = webElement.getAttribute("value");194 } else {195 result = webElement.getText();196 }197 /**198 * If return is empty, we search for hidden tags199 */200 if (StringUtil.isNullOrEmpty(result)) {201 String script = "return arguments[0].innerHTML";202 try {203 result = (String) ((JavascriptExecutor) session.getDriver()).executeScript(script, webElement);204 } catch (Exception e) {205 LOG.debug("getValueFromHTML locator : '" + identifier.getIdentifier() + "=" + identifier.getLocator() + "', exception : " + e.getMessage());206 }207 }208 }209 } else if (answer.isCodeEquals(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT.getCode())) {210 throw new NoSuchElementException(identifier.getIdentifier() + "=" + identifier.getLocator());211 }212 return result;213 }214215 @Override216 public String getAlertText(Session session) {217 Alert alert = session.getDriver().switchTo().alert();218 if (alert != null) {219 return alert.getText();220 }221222 return null;223 }224225 @Override226 public String getValueFromJS(Session session, String script) {227 JavascriptExecutor js = (JavascriptExecutor) session.getDriver();228 Object response = js.executeScript(script);229230 if (response == null) {231 return "";232 }233234 if (response instanceof String) {235 return (String) response;236 }237238 return String.valueOf(response);239 }240241 @Override242 public String getAttributeFromHtml(Session session, Identifier identifier, String attribute) {243 String result = null;244 try {245 AnswerItem answer = this.getSeleniumElement(session, identifier, true, false);246 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {247 WebElement webElement = (WebElement) answer.getItem();248 if (webElement != null) {249 result = webElement.getAttribute(attribute);250 }251 }252 } catch (WebDriverException exception) {253 LOG.warn(exception.toString());254 }255 return result;256 }257258 @Override259 public boolean isElementPresent(Session session, Identifier identifier) {260 try {261 AnswerItem answer = this.getSeleniumElement(session, identifier, false, false);262263 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {264 WebElement webElement = (WebElement) answer.getItem();265266 return webElement != null;267 }268 } catch (NoSuchElementException exception) {269 LOG.warn(exception.toString());270 }271 return false;272 }273274 @Override275 public boolean isElementNotPresent(Session session, Identifier identifier) {276 By locator = this.getBy(identifier);277 LOG.debug("Waiting for Element to be not present : " + identifier.getIdentifier() + "=" + identifier.getLocator());278 try {279 WebDriverWait wait = new WebDriverWait(session.getDriver(), TimeUnit.MILLISECONDS.toSeconds(session.getCerberus_selenium_wait_element()));280 return wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(locator)));281 } catch (TimeoutException exception) {282 LOG.warn("Exception waiting for element to be not present :" + exception);283 return false;284 }285 }286287 @Override288 public boolean isElementVisible(Session session, Identifier identifier) {289 try {290 AnswerItem answer = this.getSeleniumElement(session, identifier, true, false);291 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {292 WebElement webElement = (WebElement) answer.getItem();293 return webElement != null && webElement.isDisplayed();294 }295296 } catch (NoSuchElementException exception) {297 LOG.warn(exception.toString());298 }299 return false;300 }301302 @Override303 public boolean isElementNotVisible(Session session, Identifier identifier) {304 By locator = this.getBy(identifier);305 LOG.debug("Waiting for Element to be not visible : " + identifier.getIdentifier() + "=" + identifier.getLocator());306 try {307 WebDriverWait wait = new WebDriverWait(session.getDriver(), TimeUnit.MILLISECONDS.toSeconds(session.getCerberus_selenium_wait_element()));308 return wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));309 } catch (TimeoutException exception) {310 LOG.warn("Exception waiting for element to be not visible :" + exception);311 return false;312 }313 }314315 @Override316 public String getPageSource(Session session) {317 return session.getDriver().getPageSource();318 }319320 @Override321 public String getTitle(Session session) {322 return session.getDriver().getTitle();323 }324325 /**326 * Return the current URL from Selenium.327 *328 * @param session329 * @param applicationUrl330 * @return current URL without HTTP://IP:PORT/CONTEXTROOT/331 * @throws CerberusEventException Cannot find application host (from332 * Database) inside current URL (from Selenium)333 */334 @Override335 public String getCurrentUrl(Session session, String applicationUrl) throws CerberusEventException {336 /*337 * Example: URL (http://cerberus.domain.fr/Cerberus/mypage/page/index.jsp)<br>338 * will return /mypage/page/index.jsp339 * No matter what, the output current relative URl will start by /340 */341 // We start to remove the protocol part of the urls.342 String cleanedCurrentURL = StringUtil.removeProtocolFromHostURL(session.getDriver().getCurrentUrl());343 String cleanedURL = StringUtil.removeProtocolFromHostURL(applicationUrl);344 // We remove from current url the host part of the application.345 String strings[] = cleanedCurrentURL.split(cleanedURL, 2);346 if (strings.length < 2) {347 MessageEvent msg = new MessageEvent(MessageEventEnum.CONTROL_FAILED_URL_NOT_MATCH_APPLICATION);348 msg.setDescription(msg.getDescription().replace("%HOST%", applicationUrl));349 msg.setDescription(msg.getDescription().replace("%CURRENTURL%", session.getDriver().getCurrentUrl()));350 LOG.warn(msg.toString());351 throw new CerberusEventException(msg);352 }353 String result = StringUtil.addPrefixIfNotAlready(strings[1], "/");354 return result;355 }356357 public File takeScreenShotFile(Session session) {358 boolean event = true;359 long timeout = System.currentTimeMillis() + (session.getCerberus_selenium_wait_element());360 //Try to capture picture. Try again until timeout is WebDriverException is raised.361 while (event) {362 try {363 WebDriver augmentedDriver = new Augmenter().augment(session.getDriver());364 File image = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);365366 if (image != null) {367 //logs for debug purposes368 LOG.info("WebDriverService: screen-shot taken with succes: " + image.getName() + "(size" + image.length() + ")");369 } else {370 LOG.warn("WebDriverService: screen-shot returned null: ");371 }372 return image;373 } catch (WebDriverException exception) {374 if (System.currentTimeMillis() >= timeout) {375 LOG.warn(exception.toString());376 }377 event = false;378 }379 }380381 return null;382 }383384 @Override385 public BufferedImage takeScreenShot(Session session) {386 BufferedImage newImage = null;387 boolean event = true;388 long timeout = System.currentTimeMillis() + (session.getCerberus_selenium_wait_element());389 //Try to capture picture. Try again until timeout is WebDriverException is raised.390 while (event) {391 try {392 WebDriver augmentedDriver = new Augmenter().augment(session.getDriver());393 File image = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);394 BufferedImage bufferedImage = ImageIO.read(image);395396 newImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);397 newImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);398 return newImage;399 } catch (IOException exception) {400 LOG.warn(exception.toString());401 event = false;402 } catch (WebDriverException exception) {403 if (System.currentTimeMillis() >= timeout) {404 LOG.warn(exception.toString());405 event = false;406 }407 }408 }409 return newImage;410 }411412 @Override413 public boolean isElementInElement(Session session, Identifier identifier, Identifier childIdentifier) {414 By elementLocator = this.getBy(identifier);415 By childElementLocator = this.getBy(childIdentifier);416417 return (session.getDriver().findElement(elementLocator) != null418 && session.getDriver().findElement(elementLocator).findElement(childElementLocator) != null);419 }420421 @Override422 public boolean isElementNotClickable(Session session, Identifier identifier) {423 By locator = this.getBy(identifier);424 LOG.debug("Waiting for Element to be not clickable : " + identifier.getIdentifier() + "=" + identifier.getLocator());425 try {426 WebDriverWait wait = new WebDriverWait(session.getDriver(), TimeUnit.MILLISECONDS.toSeconds(session.getCerberus_selenium_wait_element()));427 return wait.until(ExpectedConditions.not(ExpectedConditions.elementToBeClickable(locator)));428 } catch (TimeoutException exception) {429 LOG.warn("Exception waiting for element to be not clickable :" + exception);430 return false;431 }432 }433434 @Override435 public boolean isElementClickable(Session session, Identifier identifier) {436 try {437 AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);438 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {439 WebElement webElement = (WebElement) answer.getItem();440441 return webElement != null;442 }443 } catch (NoSuchElementException exception) {444 LOG.warn(exception.toString());445 }446 return false;447 }448449 @Override450 public MessageEvent doSeleniumActionClick(Session session, final Identifier identifier, boolean waitForVisibility, boolean waitForClickability) {451 MessageEvent message;452 try {453454 AnswerItem answer = this.getSeleniumElement(session, identifier, waitForVisibility, waitForClickability);455 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {456 final WebElement webElement = (WebElement) answer.getItem();457458 if (webElement != null) {459 // We noticed that sometimes, webelement.click never finished whatever the timeout set.460 // Below is an implementation to secure timeout on thread before calling selenium.461 // This is a test that can be extended or clean depending on the result.462 ExecutorService executor = Executors.newCachedThreadPool();463 Callable<MessageEvent> task = new Callable<MessageEvent>() {464 public MessageEvent call() {465 MessageEvent message;466 Actions actions = new Actions(session.getDriver());467 actions.click(webElement);468 actions.build().perform();469 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLICK);470 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));471 return message;472 }473 };474 Future<MessageEvent> future = executor.submit(task);475 try {476 MessageEvent result = future.get(session.getCerberus_selenium_action_click_timeout(), TimeUnit.MILLISECONDS);477 return result;478 } catch (java.util.concurrent.TimeoutException ex) {479 // handle the timeout480 LOG.warn("Exception clicking on element :" + ex, ex);481 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);482 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));483 return message;484 } catch (InterruptedException e) {485 // handle the interrupts486 LOG.warn("Exception clicking on element :" + e, e);487 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CLICK);488 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()).replace("%MESS%", e.toString()));489 return message;490 } catch (ExecutionException e) {491 // handle other exceptions492 LOG.warn("Exception clicking on element :" + e, e);493 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CLICK_NO_SUCH_ELEMENT);494 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()).replace("%MESS%", e.toString()));495 return message;496 } finally {497 future.cancel(true);498 }499 }500 }501502 return answer.getResultMessage();503 } catch (NoSuchElementException exception) {504 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CLICK_NO_SUCH_ELEMENT);505 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));506 LOG.debug(exception.toString());507 return message;508// } catch (TimeoutException exception) {509// message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);510// message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));511// MyLogger.log(WebDriverService.class.getName(), Level.WARN, exception.toString());512// return message;513 } catch (WebDriverException exception) {514 LOG.warn(exception.toString());515 return parseWebDriverException(exception);516 }517518 }519520 @Override521 public MessageEvent doSeleniumActionMouseDown(Session session, Identifier identifier) {522 MessageEvent message;523 try {524525 AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);526 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {527 WebElement webElement = (WebElement) answer.getItem();528 if (webElement != null) {529 Actions actions = new Actions(session.getDriver());530 actions.clickAndHold(webElement);531 actions.build().perform();532 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEDOWN);533 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));534 return message;535 }536 }537538 return answer.getResultMessage();539 } catch (NoSuchElementException exception) {540 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEDOWN_NO_SUCH_ELEMENT);541 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));542 LOG.debug(exception.toString());543 return message;544 } catch (TimeoutException exception) {545 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);546 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));547 LOG.warn(exception.toString());548 return message;549 } catch (WebDriverException exception) {550 LOG.warn(exception.toString());551 return parseWebDriverException(exception);552 }553 }554555 @Override556 public MessageEvent doSeleniumActionMouseUp(Session session, Identifier identifier) {557 MessageEvent message;558 try {559 AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);560 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {561 WebElement webElement = (WebElement) answer.getItem();562 if (webElement != null) {563 Actions actions = new Actions(session.getDriver());564 actions.release(webElement);565 actions.build().perform();566 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEUP);567 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));568 return message;569 }570 }571572 return answer.getResultMessage();573 } catch (NoSuchElementException exception) {574 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEUP_NO_SUCH_ELEMENT);575 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));576 LOG.debug(exception.toString());577 return message;578 } catch (TimeoutException exception) {579 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);580 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));581 LOG.warn(exception.toString());582 return message;583 } catch (WebDriverException exception) {584 LOG.warn(exception.toString());585 return parseWebDriverException(exception);586 }587 }588589 @Override590 public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier identifier) {591 MessageEvent message;592 String windowTitle = identifier.getLocator();593594 String currentHandle;595 // Current serial handle of the window.596 // Add try catch to handle not exist anymore window (like when popup is closed).597 try {598 currentHandle = session.getDriver().getWindowHandle();599 } catch (NoSuchWindowException exception) {600 currentHandle = null;601 LOG.debug("Window is closed ? " + exception.toString());602 }603604 try {605 // Get serials handles list of all browser windows606 Set<String> handles = session.getDriver().getWindowHandles();607608 // Loop into each of them609 for (String windowHandle : handles) {610 if (!windowHandle.equals(currentHandle)) {611 session.getDriver().switchTo().window(windowHandle);612 if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) {613 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW);614 message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));615 return message;616 }617 }618 LOG.debug("windowHandle=" + windowHandle);619 }620 } catch (NoSuchElementException exception) {621 LOG.debug(exception.toString());622 } catch (TimeoutException exception) {623 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);624 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));625 LOG.warn(exception.toString());626 return message;627 } catch (WebDriverException exception) {628 LOG.warn(exception.toString());629 return parseWebDriverException(exception);630 }631 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT);632 message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));633 return message;634 }635636 @Override637 public MessageEvent doSeleniumActionManageDialog(Session session, Identifier identifier) {638 MessageEvent message;639 try {640 if ("ok".equalsIgnoreCase(identifier.getLocator())) {641 // Accept javascript popup dialog.642 session.getDriver().switchTo().alert().accept();643 session.getDriver().switchTo().defaultContent();644 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);645 return message;646 } else if ("cancel".equalsIgnoreCase(identifier.getLocator())) {647 // Dismiss javascript popup dialog.648 session.getDriver().switchTo().alert().dismiss();649 session.getDriver().switchTo().defaultContent();650 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);651 return message;652 }653 } catch (NoSuchWindowException exception) {654 // Add try catch to handle not exist anymore alert popup (like when popup is closed).655 LOG.debug("Alert popup is closed ? " + exception.toString());656 } catch (TimeoutException exception) {657 LOG.warn(exception.toString());658 } catch (WebDriverException exception) {659 LOG.debug("Alert popup is closed ? " + exception.toString());660 return parseWebDriverException(exception);661 }662 return new MessageEvent(MessageEventEnum.ACTION_FAILED_CLOSE_ALERT);663 }664665 private boolean checkIfExpectedWindow(Session session, String identifier, String value) {666667 boolean result = false;668 WebDriverWait wait = new WebDriverWait(session.getDriver(), TIMEOUT_WEBELEMENT);669 String title;670671 switch (identifier) {672 case Identifier.IDENTIFIER_URL: {673674 wait.until(ExpectedConditions.not(ExpectedConditions.urlToBe("about:blank")));675 return session.getDriver().getCurrentUrl().equals(value);676 }677 case Identifier.IDENTIFIER_REGEXTITLE:678 wait.until(ExpectedConditions.not(ExpectedConditions.titleIs("")));679 title = session.getDriver().getTitle();680 Pattern pattern = Pattern.compile(value);681 Matcher matcher = pattern.matcher(title);682 result = matcher.find();683 default:684 wait.until(ExpectedConditions.not(ExpectedConditions.titleIs("")));685 title = session.getDriver().getTitle();686 if (title.equals(value)) {687 result = true;688 }689 }690 return result;691 }692693 @Override694 public MessageEvent doSeleniumActionDoubleClick(Session session, Identifier identifier, boolean waitForVisibility, boolean waitForClickability) {695 MessageEvent message;696 try {697 AnswerItem answer = this.getSeleniumElement(session, identifier, waitForVisibility, waitForClickability);698 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {699 WebElement webElement = (WebElement) answer.getItem();700 if (webElement != null) {701 Actions actions = new Actions(session.getDriver());702 actions.doubleClick(webElement);703 actions.build().perform();704 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_DOUBLECLICK);705 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));706 return message;707 }708 }709 message = answer.getResultMessage();710 return message;711 } catch (NoSuchElementException exception) {712 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_DOUBLECLICK_NO_SUCH_ELEMENT);713 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));714 LOG.debug(exception.toString());715 return message;716 } catch (TimeoutException exception) {717 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);718 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));719 LOG.warn(exception.toString());720 return message;721 } catch (WebDriverException exception) {722 LOG.warn(exception.toString());723 return parseWebDriverException(exception);724 }725 }726727 @Override728 public MessageEvent doSeleniumActionType(Session session, Identifier identifier, String property, String propertyName) {729 MessageEvent message;730 try {731 AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);732 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {733 WebElement webElement = (WebElement) answer.getItem();734 if (webElement != null) {735 webElement.clear();736 if (!StringUtil.isNull(property)) {737 webElement.sendKeys(property);738 }739 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_TYPE);740 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));741 if (!StringUtil.isNull(property)) {742 message.setDescription(message.getDescription().replace("%DATA%", ParameterParserUtil.securePassword(property, propertyName)));743 } else {744 message.setDescription(message.getDescription().replace("%DATA%", "No property"));745 }746 return message;747 }748 }749 return answer.getResultMessage();750 } catch (NoSuchElementException exception) {751 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TYPE_NO_SUCH_ELEMENT);752 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));753 LOG.debug(exception.toString());754 return message;755 } catch (TimeoutException exception) {756 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);757 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));758 LOG.warn(exception.toString());759 return message;760 } catch (WebDriverException exception) {761 LOG.warn(exception.toString());762 return parseWebDriverException(exception);763 }764 }765766 @Override767 public MessageEvent doSeleniumActionMouseOver(Session session, Identifier identifier) {768 MessageEvent message;769 try {770 AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);771 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {772 WebElement menuHoverLink = (WebElement) answer.getItem();773 if (menuHoverLink != null) {774 Actions actions = new Actions(session.getDriver());775 actions.moveToElement(menuHoverLink);776 actions.build().perform();777 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEOVER);778 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));779 return message;780 }781 }782783 return answer.getResultMessage();784 } catch (NoSuchElementException exception) {785 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEOVER_NO_SUCH_ELEMENT);786 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));787 LOG.debug(exception.toString());788 return message;789 } catch (TimeoutException exception) {790 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);791 message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));792 LOG.warn(exception.toString());793 return message;794 } catch (WebDriverException exception) {795 LOG.warn(exception.toString());796 return parseWebDriverException(exception);797 }798 }799800 @Override801 public MessageEvent doSeleniumActionWait(Session session, Identifier identifier) {802 MessageEvent message;803 try {804 WebDriverWait wait = new WebDriverWait(session.getDriver(), TIMEOUT_WEBELEMENT);805 wait.until(ExpectedConditions.presenceOfElementLocated(this.getBy(identifier)));806 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT);807 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));808 return message;809 } catch (TimeoutException exception) {810 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);811 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));812 LOG.debug(exception.toString());813 return message;814 }815 }816817 @Override818 public MessageEvent doSeleniumActionWaitVanish(Session session, Identifier identifier) {819 MessageEvent message;820 try {821 WebDriverWait wait = new WebDriverWait(session.getDriver(), TIMEOUT_WEBELEMENT);822 wait.until(ExpectedConditions.invisibilityOfElementLocated(this.getBy(identifier)));823 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAITVANISH_ELEMENT);824 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));825 return message;826 } catch (TimeoutException exception) {827 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);828 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));829 LOG.debug(exception.toString());830 return message;831 }832 }833834 /**835 * Disable the headless status of the running application preventing Robot836 * to work through a web container ...

Full Screen

Full Screen

getBy

Using AI Code Generation

copy

Full Screen

1 public void test() {2 WebDriver driver = null;3 try {4 driver = new FirefoxDriver();5 driver.findElement(By.name("q")).sendKeys("selenium");6 driver.findElement(By.name("btnG")).click();7 Thread.sleep(2000);8 driver.quit();9 } catch (Exception ex) {10 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);11 } finally {12 driver.quit();13 }14 }15}16I have tried to use the following code to access the test case by its name in a test case but it is not working. It is giving me the following error:Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {name:q}I am using the following code to access the test case by its name:Can anyone help me on this?

Full Screen

Full Screen

getBy

Using AI Code Generation

copy

Full Screen

1WebElement myElement = webdriverService.getBy("myId");2myElement.click();3WebElement myElement = webdriverService.getBy("myId", "myCountry");4myElement.click();5WebElement myElement = webdriverService.getBy("myId", "myCountry", "myLanguage");6myElement.click();7WebElement myElement = webdriverService.getBy("myId", "myCountry", "myLanguage", "myBrowser");8myElement.click();9WebElement myElement = webdriverService.getBy("myId", "myCountry", "myLanguage", "myBrowser", "myVersion");10myElement.click();

Full Screen

Full Screen

getBy

Using AI Code Generation

copy

Full Screen

1WebElement element = webDriverService.getBy("id", "idValue");2WebElement element = webDriverService.getBy("name", "nameValue");3WebElement element = webDriverService.getBy("xpath", "xpathValue");4WebElement element = webDriverService.getBy("cssSelector", "cssSelectorValue");5WebElement element = webDriverService.getBy("className", "classNameValue");6WebElement element = webDriverService.getBy("tagName", "tagNameValue");7WebElement element = webDriverService.getBy("linkText", "linkTextValue");8WebElement element = webDriverService.getBy("partialLinkText", "partialLinkTextValue");9WebElement element = webDriverService.getBy("id", "idValue", "myElement");10WebElement element = webDriverService.getBy("name", "nameValue", "myElement");11WebElement element = webDriverService.getBy("xpath", "xpathValue", "myElement");12WebElement element = webDriverService.getBy("cssSelector", "cssSelectorValue", "myElement");13WebElement element = webDriverService.getBy("className", "classNameValue", "myElement");14WebElement element = webDriverService.getBy("tagName", "tagNameValue", "myElement");15WebElement element = webDriverService.getBy("linkText", "linkTextValue", "myElement");16WebElement element = webDriverService.getBy("partialLinkText", "partialLinkTextValue", "myElement");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful