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

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

Source:WebDriverService.java Github

copy

Full Screen

...132 }133 return by;134 }135136 private WebElement getWebElementUsingQuerySelector(Session session, String querySelector) {137138 WebDriver driver = session.getDriver();139140 /**141 * Build the query splitting the querySelector142 */143 String structure[] = querySelector.split(">>");144145 StringBuilder script = new StringBuilder();146 script.append("document");147148 if (structure.length == 1) {149 script.append(".querySelector('" + structure[0] + "')");150 } else {151 for (int index = 0; index < structure.length - 1; index++) {152 script.append(".querySelector('" + structure[index] + "').shadowRoot");153 }154 script.append(".querySelector('" + structure[structure.length - 1] + "')");155 }156157 /**158 * Loop until timeout is reached to scroll to the element and retrieve159 * it160 */161 WebElement finalElement = null;162 long start = new Date().getTime();163164 int i = 0;165 long elapsedSinceStart = new Date().getTime() - start;166 boolean scrolled = false;167 while ((elapsedSinceStart < session.getCerberus_selenium_wait_element())) {168169 elapsedSinceStart = new Date().getTime() - start;170 i++;171 LOG.debug("QUERY_SELECTOR ATTEMPT #" + i + " / Elapsed time from beginning : " + elapsedSinceStart + " (timeout : " + session.getCerberus_selenium_wait_element() + ")");172173 try {174 Thread.sleep(500);175176 //Scroll to element177 if (!scrolled) {178 ((JavascriptExecutor) driver).executeScript(script.toString() + ".scrollIntoView()");179 LOG.debug("Scrolled into View : ");180 scrolled = true;181 }182183 //Get element184 finalElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return " + script);185186 //If element retrieved is on the visible part of the page, break187 if (finalElement.getLocation().getX() < driver.manage().window().getSize().getWidth()188 && finalElement.getLocation().getY() < driver.manage().window().getSize().getHeight()) {189 LOG.debug("FOUND : " + finalElement);190 return finalElement;191 } else {192 scrolled = false;193 LOG.debug("Element '" + querySelector + "' " + finalElement.getLocation() + " is out of the visible screen " + driver.manage().window().getSize() + " >> Retrying to scroll");194 }195196 } catch (Exception ex) {197 LOG.debug("NOT FOUND : " + querySelector);198 }199200 }201202 return null;203 }204205 @Override206 public MessageEvent scrollTo(Session session, Identifier identifier, String text) {207 MessageEvent message = null;208 WebElement webElement = null;209210 try {211 if (StringUtil.isNullOrEmpty(text)) {212 AnswerItem answer = this.getSeleniumElement(session, identifier, false, false);213 if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {214 webElement = (WebElement) answer.getItem();215 }216 } else {217 webElement = session.getDriver().findElement(By.xpath("//*[contains(text()," + text + ")]"));218 }219220 if (webElement != null) {221222 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SCROLLTO).resolveDescription("VALUE", identifier.toString());223 if (StringUtil.isNullOrEmpty(text)) {224 scrollElement(session, webElement);225 } else {226 scrollText(session, webElement);227 }228 }229230 return message;231232 } catch (NoSuchElementException exception) {233 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SCROLL_NO_SUCH_ELEMENT);234 message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));235 LOG.debug(exception.toString());236 return message;237 } catch (Exception e) {238 LOG.error("An error occured during scroll to (element:" + identifier, e);239 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC);240 message.setDescription(message.getDescription().replace("%DETAIL%", e.getMessage()));241 return message;242243 }244245 }246247 private void scrollText(Session session, WebElement element) {248 // Create instance of Javascript executor249 JavascriptExecutor je = (JavascriptExecutor) session.getDriver();250251 // now execute query which actually will scroll until that element is not appeared on page.252 je.executeScript("arguments[0].scrollIntoView(true);window.scrollBy(" + session.getCerberus_selenium_autoscroll_horizontal_offset() + "," + session.getCerberus_selenium_autoscroll_vertical_offset() + ");", element);253 }254255 private void scrollElement(Session session, WebElement element) {256 /**257 * WebElement element =258 * driver.findElement(By.id(identifier.getLocator())); Actions actions =259 * new Actions(driver); actions.moveToElement(element);260 * actions.perform();261 */262 ((JavascriptExecutor) session.getDriver()).executeScript("arguments[0].scrollIntoView();window.scrollBy(" + session.getCerberus_selenium_autoscroll_horizontal_offset() + "," + session.getCerberus_selenium_autoscroll_vertical_offset() + ");", element);263264 }265266 private AnswerItem<WebElement> getSeleniumElement(Session session, Identifier identifier, boolean visible, boolean clickable) {267 AnswerItem<WebElement> answer = new AnswerItem<>();268 MessageEvent msg;269 By locator;270 String erratumMessage = "";271 LOG.debug("Waiting for Element : " + identifier.getIdentifier() + "=" + identifier.getLocator());272273 /**274 * If the identifier is using erratum, we trigger here the Erratum algo275 * and convert to xpath.276 */277 if (identifier.getIdentifier().equals(Identifier.IDENTIFIER_ERRATUM) && identifier.getLocator() != null) {278 LOG.debug("ERRATUM SELECTED ============================================");279 if (!identifier.getLocator().contains(ERRATUM_SEPARATOR)) {280 LOG.warn("Erratum value is missing separator");281 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_ERRATUM_MISSING_SEPARATOR);282 msg.resolveDescription("SEPARATOR", ERRATUM_SEPARATOR);283 answer.setResultMessage(msg);284 return answer;285 }286 String newXpath = getNewXPathFromErratum(session, identifier);287 LOG.debug("NEW XPATH = " + newXpath);288 if (!StringUtil.isNullOrEmpty(newXpath)) {289 locator = By.xpath(newXpath);290 identifier.setIdentifier(Identifier.IDENTIFIER_XPATH);291 identifier.setLocator(newXpath);292 erratumMessage = " (converted by erratum)";293 } else {294 LOG.warn("No valid xpath found by Erratum");295 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_ERRATUM_ELEMENT_NOT_FOUND);296 answer.setResultMessage(msg);297 return answer;298 }299 } else if (identifier.getIdentifier().equals(Identifier.IDENTIFIER_QUERYSELECTOR)) {300 WebElement element = getWebElementUsingQuerySelector(session, identifier.getLocator());301 if (element != null) {302 answer.setItem(getWebElementUsingQuerySelector(session, identifier.getLocator()));303 msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT);304 msg.resolveDescription("ELEMENT", identifier.getIdentifier() + "=" + identifier.getLocator());305 } else {306 msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT);307 msg.resolveDescription("ELEMENT", identifier.getIdentifier() + "=" + identifier.getLocator() + erratumMessage);308 }309 answer.setResultMessage(msg);310 return answer;311 } else {312 locator = this.getBy(identifier);313 }314315 /**316 * locator now content the right definition so we can wait the element ...

Full Screen

Full Screen

getWebElementUsingQuerySelector

Using AI Code Generation

copy

Full Screen

1public WebElement getWebElementUsingQuerySelector(String querySelector) {2 JavascriptExecutor js = (JavascriptExecutor) driver;3 String script = "return document.querySelector('" + querySelector + "')";4 return (WebElement) js.executeScript(script);5}6public List<WebElement> getWebElementsUsingQuerySelectorAll(String querySelector) {7 JavascriptExecutor js = (JavascriptExecutor) driver;8 String script = "return document.querySelectorAll('" + querySelector + "')";9 return (List<WebElement>) js.executeScript(script);10}11public List<WebElement> getWebElementsUsingXPath(String xPath) {12 return driver.findElements(By.xpath(xPath));13}14public WebElement getWebElementUsingXPath(String xPath) {15 return driver.findElement(By.xpath(xPath));16}17public WebElement getWebElementUsingId(String id) {18 return driver.findElement(By.id(id));19}20public WebElement getWebElementUsingName(String name) {21 return driver.findElement(By.name(name));22}23public WebElement getWebElementUsingClassName(String className) {24 return driver.findElement(By.className(className));25}26public WebElement getWebElementUsingLinkText(String linkText) {27 return driver.findElement(By.linkText(linkText));28}29public WebElement getWebElementUsingPartialLinkText(String partialLinkText) {30 return driver.findElement(By.partialLinkText(partialLinkText));31}32public WebElement getWebElementUsingTagName(String tagName) {33 return driver.findElement(By.tagName(tagName));34}

Full Screen

Full Screen

getWebElementUsingQuerySelector

Using AI Code Generation

copy

Full Screen

1WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");2WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");3WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");4WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");5WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");6WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");7WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");8WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");9WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");10WebElement element = webDriverService.getWebElementUsingQuerySelector("div#id");

Full Screen

Full Screen

getWebElementUsingQuerySelector

Using AI Code Generation

copy

Full Screen

1WebDriver driver = appService.getDriver(appService.getApplicationObject(test, testCaseExecution));2WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector);3WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout);4WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout, retry);5WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout, retry, wait);6WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout, retry, wait, exception);7WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout, retry, wait, exception, message);8WebElement element = webDriverService.getWebElementUsingQuerySelector(driver, querySelector, timeout, retry, wait, exception, message, screenshot);

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