Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.DriverHelper.getEnvArgURL
Source:DriverHelper.java
...133 * @param timeout134 * long135 */136 public void openURL(String url, long timeout) {137 final String decryptedURL = getEnvArgURL(cryptoTool.decryptByPattern(url, CRYPTO_PATTERN));138 this.pageURL = decryptedURL;139 WebDriver drv = getDriver();140 141 setPageLoadTimeout(drv, timeout);142 DriverListener.setMessages(Messager.OPENED_URL.getMessage(url), Messager.NOT_OPENED_URL.getMessage(url));143 144 // [VD] there is no sense to use fluent wait here as selenium just don't return something until page is ready!145 // explicitly limit time for the openURL operation146 try {147 Messager.OPENING_URL.info(url);148 drv.get(decryptedURL);149 } catch (UnhandledAlertException e) {150 drv.switchTo().alert().accept();151 } catch (TimeoutException e) {152 trigger("window.stop();"); //try to cancel page loading153 Assert.fail("Unable to open url during " + timeout + "sec!");154 } catch (Exception e) {155 Assert.fail("Undefined error on open url detected: " + e.getMessage(), e);156 } finally {157 //restore default pageLoadTimeout driver timeout158 setPageLoadTimeout(drv, getPageLoadTimeout());159 LOGGER.debug("finished driver.get call.");160 }161 }162 protected void setPageURL(String relURL) {163 String baseURL;164 // if(!"NULL".equalsIgnoreCase(Configuration.get(Parameter.ENV)))165 if (!Configuration.get(Parameter.ENV).isEmpty()) {166 baseURL = Configuration.getEnvArg("base");167 } else {168 baseURL = Configuration.get(Parameter.URL);169 }170 this.pageURL = baseURL + relURL;171 }172 protected void setPageAbsoluteURL(String url) {173 this.pageURL = url;174 }175 public String getPageURL() {176 return this.pageURL;177 } 178 // --------------------------------------------------------------------------179 // Base UI interaction operations180 // --------------------------------------------------------------------------181 /**182 * Method which quickly looks for all element and check that they present183 * during EXPLICIT_TIMEOUT184 *185 * @param elements186 * ExtendedWebElement...187 * @return boolean return true only if all elements present.188 */189 public boolean allElementsPresent(ExtendedWebElement... elements) {190 return allElementsPresent(EXPLICIT_TIMEOUT, elements);191 }192 /**193 * Method which quickly looks for all element and check that they present194 * during timeout sec195 *196 * @param timeout long197 * @param elements198 * ExtendedWebElement...199 * @return boolean return true only if all elements present.200 */201 public boolean allElementsPresent(long timeout, ExtendedWebElement... elements) {202 int index = 0;203 boolean present = true;204 boolean ret = true;205 int counts = 1;206 timeout = timeout / counts;207 if (timeout < 1)208 timeout = 1;209 while (present && index++ < counts) {210 for (int i = 0; i < elements.length; i++) {211 present = elements[i].isElementPresent(timeout);212 if (!present) {213 LOGGER.error(elements[i].getNameWithLocator() + " is not present.");214 ret = false;215 }216 }217 }218 return ret;219 }220 /**221 * Method which quickly looks for all element lists and check that they222 * contain at least one element during SHORT_TIMEOUT223 *224 * @param elements225 * List<ExtendedWebElement>...226 * @return boolean227 */228 @SuppressWarnings("unchecked")229 public boolean allElementListsAreNotEmpty(List<ExtendedWebElement>... elements) {230 return allElementListsAreNotEmpty(SHORT_TIMEOUT, elements);231 }232 /**233 * Method which quickly looks for all element lists and check that they234 * contain at least one element during timeout235 *236 * @param timeout long237 * @param elements238 * List<ExtendedWebElement>...239 * @return boolean return true only if All Element lists contain at least240 * one element241 */242 @SuppressWarnings("unchecked")243 public boolean allElementListsAreNotEmpty(long timeout, List<ExtendedWebElement>... elements) {244 boolean ret;245 int counts = 3;246 timeout = timeout / counts;247 if (timeout < 1)248 timeout = 1;249 for (int i = 0; i < elements.length; i++) {250 boolean present = false;251 int index = 0;252 while (!present && index++ < counts) {253 try {254 present = elements[i].get(0).isElementPresent(timeout);255 } catch (Exception e) {256 present = false;257 }258 }259 ret = (elements[i].size() > 0);260 if (!ret) {261 LOGGER.error("List of elements[" + i + "] from elements " + Arrays.toString(elements) + " is empty.");262 return false;263 }264 }265 return true;266 }267 /**268 * Method which quickly looks for any element presence during269 * SHORT_TIMEOUT270 *271 * @param elements ExtendedWebElement...272 * @return true if any of elements was found.273 */274 public boolean isAnyElementPresent(ExtendedWebElement... elements) {275 return isAnyElementPresent(SHORT_TIMEOUT, elements);276 }277 /**278 * Method which quickly looks for any element presence during timeout sec279 *280 * @param timeout long281 * @param elements282 * ExtendedWebElement...283 * @return true if any of elements was found.284 */285 public boolean isAnyElementPresent(long timeout, ExtendedWebElement... elements) {286 int index = 0;287 int counts = 10;288 timeout = timeout / counts;289 if (timeout < 1)290 timeout = 1;291 while (index++ < counts) {292 for (int i = 0; i < elements.length; i++) {293 if (elements[i].isElementPresent(timeout)) {294 LOGGER.debug(elements[i].getNameWithLocator() + " is present");295 return true;296 }297 }298 }299 300 LOGGER.error("Unable to find any element from array: " + Arrays.toString(elements));301 return false;302 }303 /**304 * return Any Present Element from the list which present during305 * SHORT_TIMEOUT306 *307 * @param elements ExtendedWebElement...308 * @return ExtendedWebElement309 */310 public ExtendedWebElement returnAnyPresentElement(ExtendedWebElement... elements) {311 return returnAnyPresentElement(SHORT_TIMEOUT, elements);312 }313 /**314 * return Any Present Element from the list which present during timeout sec315 *316 * @param timeout long317 * @param elements318 * ExtendedWebElement...319 * @return ExtendedWebElement320 */321 public ExtendedWebElement returnAnyPresentElement(long timeout, ExtendedWebElement... elements) {322 int index = 0;323 int counts = 10;324 timeout = timeout / counts;325 if (timeout < 1)326 timeout = 1;327 while (index++ < counts) {328 for (int i = 0; i < elements.length; i++) {329 if (elements[i].isElementPresent(timeout)) {330 LOGGER.debug(elements[i].getNameWithLocator() + " is present");331 return elements[i];332 }333 }334 }335 //throw exception anyway if nothing was returned inside for cycle336 LOGGER.error("All elements are not present");337 throw new RuntimeException("Unable to find any element from array: " + Arrays.toString(elements));338 }339 /**340 * Check that element with text present.341 * 342 * @param extWebElement to check if element with text is present343 * @param text344 * of element to check.345 * @return element with text existence status.346 */347 public boolean isElementWithTextPresent(final ExtendedWebElement extWebElement, final String text) {348 return isElementWithTextPresent(extWebElement, text, EXPLICIT_TIMEOUT);349 }350 /**351 * Check that element with text present.352 * 353 * @param extWebElement to check if element with text is present354 * @param text355 * of element to check.356 * @param timeout Long357 * @return element with text existence status.358 */359 public boolean isElementWithTextPresent(final ExtendedWebElement extWebElement, final String text, long timeout) {360 return extWebElement.isElementWithTextPresent(text, timeout);361 }362 /**363 * Check that element not present on page.364 * 365 * @param extWebElement to check if element is not present366 * 367 * @return element non-existence status.368 */369 public boolean isElementNotPresent(final ExtendedWebElement extWebElement) {370 return isElementNotPresent(extWebElement, EXPLICIT_TIMEOUT);371 }372 /**373 * Check that element not present on page.374 * 375 * @param extWebElement to check if element is not present376 * @param timeout to wait377 * 378 * @return element non-existence status.379 */380 public boolean isElementNotPresent(final ExtendedWebElement extWebElement, long timeout) {381 return extWebElement.isElementNotPresent(timeout);382 }383 /**384 * Check that element not present on page.385 * 386 * @param element to check if element is not present387 * @param controlInfo String388 * 389 * @return element non-existence status.390 */391 public boolean isElementNotPresent(String controlInfo, final WebElement element) {392 return isElementNotPresent(new ExtendedWebElement(element, controlInfo));393 }394 /**395 * Clicks on element.396 * 397 * @param elements ExtendedWebElements to click398 *399 */400 public void clickAny(ExtendedWebElement... elements) {401 clickAny(EXPLICIT_TIMEOUT, elements);402 }403 /**404 * Clicks on element.405 * 406 * @param elements ExtendedWebElements to click407 * @param timeout to wait408 *409 */410 public void clickAny(long timeout, ExtendedWebElement... elements) {411 // Method which quickly looks for any element and click during timeout412 // sec413 WebDriver drv = getDriver();414 ActionPoller<WebElement> actionPoller = ActionPoller.builder();415 Optional<WebElement> searchableElement = actionPoller.task(() -> {416 WebElement possiblyFoundElement = null;417 for (ExtendedWebElement element : elements) {418 List<WebElement> foundElements = drv.findElements(element.getBy());419 if (foundElements.size() > 0) {420 possiblyFoundElement = foundElements.get(0);421 break;422 }423 }424 return possiblyFoundElement;425 })426 .until(Objects::nonNull)427 .pollEvery(0, ChronoUnit.SECONDS)428 .stopAfter(timeout, ChronoUnit.SECONDS)429 .execute();430 if (searchableElement.isEmpty()) {431 throw new RuntimeException("Unable to click onto any elements from array: " + Arrays.toString(elements));432 }433 searchableElement.get()434 .click();435 }436 437 /*438 * Get and return the source of the last loaded page.439 * @return String440 */441 public String getPageSource() {442 WebDriver drv = getDriver();443 Messager.GET_PAGE_SOURCE.info();444 Wait<WebDriver> wait = new FluentWait<WebDriver>(drv)445 .pollingEvery(Duration.ofMillis(5000)) // there is no sense to refresh url address too often446 .withTimeout(Duration.ofSeconds(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT)))447 .ignoring(WebDriverException.class)448 .ignoring(JavascriptException.class); // org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'outerHTML' of null449 String res = "";450 try {451 res = wait.until(new Function<WebDriver, String>() {452 public String apply(WebDriver driver) {453 return drv.getPageSource();454 }455 });456 } catch (ScriptTimeoutException | TimeoutException e) {457 Messager.FAIL_GET_PAGE_SOURCE.error();458 }459 Messager.GET_PAGE_SOURCE.info();460 return res;461 }462 463 /*464 * Add cookie object into the driver465 * @param Cookie466 */467 public void addCookie(Cookie cookie) {468 WebDriver drv = getDriver();469 DriverListener.setMessages(Messager.ADD_COOKIE.getMessage(cookie.getName()), 470 Messager.FAIL_ADD_COOKIE.getMessage(cookie.getName()));471 Wait<WebDriver> wait = new FluentWait<WebDriver>(drv)472 .pollingEvery(Duration.ofMillis(Configuration.getInt(Parameter.RETRY_INTERVAL)))473 .withTimeout(Duration.ofSeconds(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT)))474 .ignoring(WebDriverException.class)475 .ignoring(JsonException.class); // org.openqa.selenium.json.JsonException: Expected to read a START_MAP but instead have: END. Last 0 characters rea476 wait.until(new Function<WebDriver, Boolean>() {477 public Boolean apply(WebDriver driver) {478 drv.manage().addCookie(cookie);479 return true;480 }481 });482 }483 484 /**485 * Get a string representing the current URL that the browser is looking at.486 * @return url.487 */488 public String getCurrentUrl() {489 return getCurrentUrl(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT));490 }491 492 /**493 * Get a string representing the current URL that the browser is looking at.494 * @param timeout long495 * @return validation result.496 */497 public String getCurrentUrl(long timeout) {498 WebDriver drv = getDriver();499 500 // explicitly limit time for the getCurrentUrl operation501 Future<?> future = Executors.newSingleThreadExecutor().submit(new Callable<String>() {502 public String call() throws Exception {503 //organize fluent waiter for getting url504 Wait<WebDriver> wait = new FluentWait<WebDriver>(drv)505 .pollingEvery(Duration.ofMillis(Configuration.getInt(Parameter.RETRY_INTERVAL)))506 .withTimeout(Duration.ofSeconds(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT)))507 .ignoring(WebDriverException.class)508 .ignoring(JsonException.class); // org.openqa.selenium.json.JsonException: Expected to read a START_MAP but instead have: END. Last 0 characters rea509 return wait.until(new Function<WebDriver, String>() {510 public String apply(WebDriver driver) {511 return drv.getCurrentUrl();512 }513 });514 }515 });516 517 String url = "";518 try {519 url = (String) future.get(timeout, TimeUnit.SECONDS);520 } catch (java.util.concurrent.TimeoutException e) {521 LOGGER.debug("Unable to get driver url during " + timeout + "sec!", e);522 } catch (InterruptedException e) {523 LOGGER.debug("Unable to get driver url during " + timeout + "sec!", e);524 Thread.currentThread().interrupt();525 } catch (ExecutionException e) {526 LOGGER.error("ExecutionException error on get driver url!", e);527 } catch (Exception e) {528 LOGGER.error("Undefined error on get driver url detected!", e);529 }530 531 return url;532 } 533 /**534 * Checks that current URL is as expected.535 * 536 * @param expectedURL537 * Expected Url538 * @return validation result.539 */540 public boolean isUrlAsExpected(String expectedURL) {541 return isUrlAsExpected(expectedURL, Configuration.getInt(Parameter.EXPLICIT_TIMEOUT)); 542 }543 /**544 * Checks that current URL is as expected.545 * 546 * @param expectedURL547 * Expected Url548 * @param timeout long549 * @return validation result.550 */551 public boolean isUrlAsExpected(String expectedURL, long timeout) {552 String decryptedURL = cryptoTool.decryptByPattern(expectedURL, CRYPTO_PATTERN);553 decryptedURL = getEnvArgURL(decryptedURL);554 555 String actualUrl = getCurrentUrl(timeout);556 557 if (LogicUtils.isURLEqual(decryptedURL, actualUrl)) {558 Messager.EXPECTED_URL.info(actualUrl);559 return true;560 } else {561 Messager.UNEXPECTED_URL.error(expectedURL, actualUrl);562 return false;563 } 564 }565 566 567 /**568 * Get full or relative URL considering Env argument569 * 570 * @param decryptedURL String571 * @return url572 */573 private String getEnvArgURL(String decryptedURL) {574 if (!(decryptedURL.contains("http:") || decryptedURL.contains("https:"))) {575 if (Configuration.getEnvArg(Parameter.URL.getKey()).isEmpty()) {576 decryptedURL = Configuration.get(Parameter.URL) + decryptedURL;577 } else {578 decryptedURL = Configuration.getEnvArg(Parameter.URL.getKey()) + decryptedURL;579 }580 }581 return decryptedURL;582 }583 /**584 *585 * @return String saved in clipboard586 */587 public String getClipboardText() {...
getEnvArgURL
Using AI Code Generation
1String url = getEnvArgURL();2String username = getEnvArg("username");3String password = getEnvArgPassword();4String browser = getEnvArg("browser");5String version = getEnvArg("version");6String platform = getEnvArg("platform");7String locale = getEnvArg("locale");8String timezone = getEnvArg("timezone");9String gridURL = getEnvArg("grid_url");10String proxy = getEnvArg("proxy");11String proxyUser = getEnvArg("proxy_user");12String proxyPassword = getEnvArg("proxy_password");13String proxyType = getEnvArg("proxy_type");14String proxyAutoconfigURL = getEnvArg("proxy_autoconfig_url");15String proxySocksUsername = getEnvArg("proxy_socks_username");16String proxySocksPassword = getEnvArg("proxy_socks_password
getEnvArgURL
Using AI Code Generation
1String url = getEnvArgURL("qa");2String url = getEnvArgURL("dev");3String url = getEnvArgURL("stage");4String url = getEnvArgURL("prod");5String url = getEnvArgURL("qa");6String url = getEnvArgURL("dev");7String url = getEnvArgURL("stage");8String url = getEnvArgURL("prod");9String url = getEnvArgURL("qa");10String url = getEnvArgURL("dev");11String url = getEnvArgURL("stage");12String url = getEnvArgURL("prod");13String url = getEnvArgURL("qa");14String url = getEnvArgURL("dev");15String url = getEnvArgURL("stage");16String url = getEnvArgURL("prod");17String url = getEnvArgURL("qa");18String url = getEnvArgURL("dev");19String url = getEnvArgURL("stage");20String url = getEnvArgURL("prod");21String url = getEnvArgURL("qa");22String url = getEnvArgURL("dev");23String url = getEnvArgURL("stage");24String url = getEnvArgURL("prod");25String url = getEnvArgURL("qa");26String url = getEnvArgURL("dev");27String url = getEnvArgURL("stage");28String url = getEnvArgURL("prod");
getEnvArgURL
Using AI Code Generation
1String url = getEnvArgURL("URL");2String browser = getEnvArg("BROWSER");3String platform = getEnvArg("PLATFORM");4String version = getEnvArg("VERSION");5String testType = getEnvArg("TEST_TYPE");6String testName = getEnvArg("TEST_NAME");7String testDescription = getEnvArg("TEST_DESCRIPTION");8String testGroup = getEnvArg("TEST_GROUP");9String testAuthor = getEnvArg("TEST_AUTHOR");10String testData = getEnvArg("TEST_DATA");
getEnvArgURL
Using AI Code Generation
1String url = getEnvArgURL();2driver.get(url);3String customVar = getEnvArg("CUSTOM_VAR");4String customVar = getEnvArg("CUSTOM_VAR", "default value");5String url = Configuration.getEnvArgURL();6driver.get(url);7String customVar = Configuration.getEnvArg("CUSTOM_VAR");8String customVar = Configuration.getEnvArg("CUSTOM_VAR", "default value");9String url = Configuration.getEnvArgURL();10driver.get(url);11String customVar = Configuration.getEnvArg("CUSTOM_VAR");12String customVar = Configuration.getEnvArg("CUSTOM_VAR", "default value");13String url = Configuration.getEnvArgURL();14driver.get(url);15String customVar = Configuration.getEnvArg("CUSTOM_VAR");16String customVar = Configuration.getEnvArg("CUSTOM_VAR", "default value");17String url = Configuration.getEnvArgURL();18driver.get(url);19String customVar = Configuration.getEnvArg("CUSTOM_VAR");20String customVar = Configuration.getEnvArg("CUSTOM_VAR", "default value");21String url = Configuration.getEnvArgURL();22driver.get(url);23String customVar = Configuration.getEnvArg("CUSTOM_VAR");
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!