Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement.isElementWithTextPresent
Source:ExtendedWebElement.java
...381 *382 * @param text of element to check.383 * @return element with text existence status.384 */385 public boolean isElementWithTextPresent(final String text) {386 return isElementWithTextPresent(text, EXPLICIT_TIMEOUT);387 }388 public boolean isElementWithTextPresent(final String text, long timeout) {389 boolean result;390 final String decryptedText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);391 wait = new WebDriverWait(getDriver(), timeout, RETRY_TIME);392 try {393 wait.until((Function<WebDriver, Object>) dr -> {394 try {395 element = findElement(timeout);396 return element.isDisplayed() && element.getText().contains(decryptedText);397 } catch (Exception e) {398 LOGGER.debug(e.getMessage(), e.getCause());399 return false;400 }401 });402 result = true;403 summary.log(Messager.ELEMENT_WITH_TEXT_PRESENT.info(getName(), text));404 } catch (Exception e) {405 LOGGER.debug(e.getMessage(), e.getCause());406 result = false;407 summary.log(Messager.ELEMENT_WITH_TEXT_NOT_PRESENT.error(getNameWithLocator(), text));408 }409 return result;410 }411 public boolean clickIfPresent() {412 return clickIfPresent(EXPLICIT_TIMEOUT);413 }414 public boolean clickIfPresent(long timeout) {415 boolean result;416 WebDriver drv = getDriver();417 setImplicitTimeout(1);418 wait = new WebDriverWait(drv, timeout, RETRY_TIME);419 try {420 wait.until((Function<WebDriver, Object>) dr -> findElement(timeout).isDisplayed());421 captureElements();422 element.click();423 String msg = Messager.ELEMENT_CLICKED.info(getName());424 summary.log(msg);425 result = true;426 } catch (Exception e) {427 LOGGER.debug(e.getMessage(), e.getCause());428 result = false;429 }430 setImplicitTimeout();431 return result;432 }433 /**434 * Types text to specified element.435 *436 * @param text to type.437 */438 public void type(String text) {439 captureElements();440 String msg;441 final String decryptedText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);442 WebDriver drv = getDriver();443 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);444 try {445 element = findElement(EXPLICIT_TIMEOUT);446 wait.until((Function<WebDriver, Object>) dr -> element.isDisplayed());447 scrollTo();448 element.clear();449 element.sendKeys(decryptedText);450 msg = Messager.KEYS_SEND_TO_ELEMENT.info(text, getName());451 summary.log(msg);452 } catch (StaleElementReferenceException e) {453 element = findStaleElement();454 LOGGER.debug(e.getMessage(), e.getCause());455 element.clear();456 element.sendKeys(decryptedText);457 msg = Messager.KEYS_SEND_TO_ELEMENT.info(text, getName());458 summary.log(msg);459 } catch (Exception e) {460 msg = Messager.KEYS_NOT_SEND_TO_ELEMENT.error(text, getNameWithLocator());461 summary.log(msg);462 throw new RuntimeException(msg, e);463 }464 Screenshot.capture(drv, msg);465 }466 /**467 * Set implicit timeout to default IMPLICIT_TIMEOUT value.468 */469 public void setImplicitTimeout() {470 setImplicitTimeout(IMPLICIT_TIMEOUT);471 }472 /**473 * Set implicit timeout.474 *475 * @param timeout in seconds. Minimal value - 1 second476 */477 public void setImplicitTimeout(long timeout) {478 if (timeout < 1) {479 timeout = 1;480 }481 try {482 getDriver().manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);483 } catch (Exception e) {484 LOGGER.error("Unable to set implicit timeout to " + timeout, e);485 getDriver().manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);486 }487 }488 /**489 * Safe click on element, used to reduce any problems with that action.490 *491 * @param startTimer Start time492 */493 private void clickSafe(long timeout, boolean startTimer) {494 boolean clicked = false;495 Exception reason = null;496 if (startTimer) {497 timer = System.currentTimeMillis();498 }499 try {500 findElement(timeout).click();501 clicked = true;502 } catch (UnhandledAlertException e) {503 LOGGER.debug(e.getMessage(), e.getCause());504 getDriver().switchTo().alert().accept();505 } catch (StaleElementReferenceException e) {506 element = findStaleElement();507 LOGGER.debug(e.getMessage(), e.getCause());508 } catch (Exception e) {509 LOGGER.debug(e.getMessage(), e.getCause());510 scrollTo();511 reason = e;512 }513 if (!clicked) {514 pause((double) RETRY_TIME / 1000);515 //repeat again until timeout achieved516 if (System.currentTimeMillis() - timer < timeout * 1000) {517 clickSafe(timeout, false);518 } else {519 String msg = Messager.ELEMENT_NOT_CLICKED.error(getNameWithLocator());520 summary.log(msg);521 throw new RuntimeException(msg, reason);522 }523 }524 }525 public void scrollTo() {526 if (Configuration.get(Parameter.DRIVER_TYPE).toLowerCase().contains(SpecialKeywords.MOBILE)) {527 LOGGER.debug("scrollTo javascript is unsupported for mobile devices!");528 return;529 }530 try {531 Locatable locatableElement = (Locatable) findElement(EXPLICIT_TIMEOUT);532 //[VD] onScreen should be updated onto onPage as only 2nd one returns real coordinates without scrolling... read below material for details533 //https://groups.google.com/d/msg/selenium-developers/nJR5VnL-3Qs/uqUkXFw4FSwJ534 //[CB] onPage -> inViewPort535 //https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/remote/RemoteWebElement.java?r=abc64b1df10d5f5d72d11fba37fabf5e85644081536 int y = locatableElement.getCoordinates().inViewPort().getY();537 int offset = R.CONFIG.getInt("scroll_to_element_y_offset");538 ((JavascriptExecutor) getDriver()).executeScript("window.scrollBy(0," + (y - offset) + ");");539 } catch (Exception e) {540 // TODO: calm error logging as it is too noisy541 //LOGGER.debug("Scroll to element: " + getName() + " not performed!" + e.getMessage());542 }543 }544 /**545 * Inputs file path to specified element.546 *547 * @param filePath path548 */549 public void attachFile(String filePath) {550 String msg;551 final String decryptedFilePath = cryptoTool.decryptByPattern(filePath, CRYPTO_PATTERN);552 WebDriver drv = getDriver();553 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);554 try {555 element = findElement(EXPLICIT_TIMEOUT);556 wait.until((Function<WebDriver, Object>) dr -> element.isDisplayed());557 element.sendKeys(decryptedFilePath);558 msg = Messager.FILE_ATTACHED.info(filePath);559 summary.log(msg);560 } catch (Exception e) {561 msg = Messager.FILE_NOT_ATTACHED.error(filePath);562 summary.log(msg);563 throw new RuntimeException(msg, e);564 }565 Screenshot.capture(drv, msg);566 }567 /**568 * Check checkbox569 * <p>570 * for checkbox Element571 */572 public void check() {573 if (isElementPresent() && !findElement(EXPLICIT_TIMEOUT).isSelected()) {574 click();575 String msg = Messager.CHECKBOX_CHECKED.info(getName());576 summary.log(msg);577 Screenshot.capture(getDriver(), msg);578 }579 }580 /**581 * Uncheck checkbox582 * <p>583 * for checkbox Element584 */585 public void uncheck() {586 if (isElementPresent() && findElement(EXPLICIT_TIMEOUT).isSelected()) {587 click();588 String msg = Messager.CHECKBOX_UNCHECKED.info(getName());589 summary.log(msg);590 Screenshot.capture(getDriver(), msg);591 }592 }593 /**594 * Get checkbox state.595 *596 * @return - current state597 */598 public boolean isChecked() {599 assertElementPresent();600 element = findElement(EXPLICIT_TIMEOUT);601 boolean res = element.isSelected();602 if (element.getAttribute("checked") != null) {603 res |= element.getAttribute("checked").equalsIgnoreCase("true");604 }605 return res;606 }607 /**608 * Get selected elements from one-value select.609 *610 * @return selected value611 */612 public String getSelectedValue() {613 assertElementPresent();614 return new Select(findElement(EXPLICIT_TIMEOUT)).getAllSelectedOptions().get(0).getText();615 }616 /**617 * Get selected elements from multi-value select.618 *619 * @return selected values620 */621 public List<String> getSelectedValues() {622 assertElementPresent();623 Select s = new Select(findElement(EXPLICIT_TIMEOUT));624 List<String> values = new ArrayList<String>();625 for (WebElement we : s.getAllSelectedOptions()) {626 values.add(we.getText());627 }628 return values;629 }630 private WebDriver getDriver() {631 return driver;632 }633 /**634 * Selects text in specified select element.635 *636 * @param selectText select text637 * @return true if item selected, otherwise false.638 */639 public boolean select(final String selectText) {640 boolean isSelected = false;641 final String decryptedSelectText = cryptoTool.decryptByPattern(selectText, CRYPTO_PATTERN);642 WebDriver drv = getDriver();643 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);644 645 final Select s = new Select(findElement(EXPLICIT_TIMEOUT));646 String msg = null;647 try {648 wait.until((Function<WebDriver, Object>) dr -> {649 try {650 s.selectByVisibleText(decryptedSelectText);651 return true;652 } catch (Exception e) {653 //do nothing654 }655 return false;656 });657 isSelected = true;658 msg = Messager.SELECT_BY_TEXT_PERFORMED.info(selectText, getName());659 } catch (Exception e) {660 msg = Messager.SELECT_BY_TEXT_NOT_PERFORMED.error(selectText, getNameWithLocator());661 e.printStackTrace();662 }663 summary.log(msg);664 Screenshot.capture(drv, msg);665 return isSelected;666 }667 /**668 * Select multiple text values in specified select element.669 *670 * @param values final String[]671 * @return boolean.672 */673 public boolean select(final String[] values) {674 boolean result = true;675 for (String value : values) {676 if (!select(value)) {677 result = false;678 }679 }680 return result;681 }682 /**683 * Selects value according to text value matcher.684 *685 * @param matcher {@link} BaseMatcher686 * @return true if item selected, otherwise false.687 * <p>688 * Usage example:689 * BaseMatcher<String> match=new BaseMatcher<String>() {690 * {@literal @}Override691 * public boolean matches(Object actual) {692 * return actual.toString().contains(RequiredText);693 * }694 * {@literal @}Override695 * public void describeTo(Description description) {696 * }697 * };698 */699 public boolean selectByMatcher(final BaseMatcher<String> matcher) {700 boolean isSelected = false;701 WebDriver drv = getDriver();702 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);703 704 final Select s = new Select(findElement(EXPLICIT_TIMEOUT));705 String msg = null;706 707 try {708 wait.until((Function<WebDriver, Object>) dr -> {709 try {710 String fullTextValue = null;711 for (WebElement option : s.getOptions()) {712 if (matcher.matches(option.getText())) {713 fullTextValue = option.getText();714 break;715 }716 }717 s.selectByVisibleText(fullTextValue);718 return true;719 } catch (Exception e) {720 LOGGER.debug(e.getMessage(), e.getCause());721 }722 return false;723 });724 isSelected = true;725 msg = Messager.SELECT_BY_MATCHER_TEXT_PERFORMED.info(matcher.toString(), getName());726 } catch (Exception e) {727 msg = Messager.SELECT_BY_MATCHER_TEXT_NOT_PERFORMED.error(matcher.toString(), getNameWithLocator());728 e.printStackTrace();729 }730 summary.log(msg);731 Screenshot.capture(drv, msg);732 return isSelected;733 }734 /**735 * Selects first value according to partial text value.736 *737 * @param partialSelectText select by partial text738 * @return true if item selected, otherwise false.739 */740 public boolean selectByPartialText(final String partialSelectText) {741 boolean isSelected = false;742 WebDriver drv = getDriver();743 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);744 745 final Select s = new Select(findElement(EXPLICIT_TIMEOUT));746 String msg = null;747 try {748 wait.until((Function<WebDriver, Object>) dr -> {749 try {750 String fullTextValue = null;751 for (WebElement option : s.getOptions()) {752 if (option.getText().contains(partialSelectText)) {753 fullTextValue = option.getText();754 break;755 }756 }757 s.selectByVisibleText(fullTextValue);758 return true;759 } catch (Exception e) {760 LOGGER.debug(e.getMessage(), e.getCause());761 }762 return false;763 });764 isSelected = true;765 msg = Messager.SELECT_BY_TEXT_PERFORMED.info(partialSelectText, getName());766 } catch (Exception e) {767 msg = Messager.SELECT_BY_TEXT_NOT_PERFORMED.error(partialSelectText, getNameWithLocator());768 e.printStackTrace();769 }770 summary.log(msg);771 Screenshot.capture(drv, msg);772 return isSelected;773 }774 /**775 * Selects item by index in specified select element.776 *777 * @param index to select by778 * @return true if item selected, otherwise false.779 */780 public boolean select(final int index) {781 boolean isSelected = false;782 WebDriver drv = getDriver();783 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);784 785 final Select s = new Select(findElement(EXPLICIT_TIMEOUT));786 String msg = null;787 try {788 wait.until((Function<WebDriver, Object>) dr -> {789 try {790 s.selectByIndex(index);791 return true;792 } catch (Exception e) {793 LOGGER.debug(e.getMessage(), e.getCause());794 }795 return false;796 });797 isSelected = true;798 msg = Messager.SELECT_BY_INDEX_PERFORMED.info(String.valueOf(index), getName());799 } catch (Exception e) {800 msg = Messager.SELECT_BY_INDEX_NOT_PERFORMED.error(String.valueOf(index), getNameWithLocator());801 e.printStackTrace();802 }803 summary.log(msg);804 Screenshot.capture(drv, msg);805 return isSelected;806 }807 // --------------------------------------------------------------------------808 // Base UI validations809 // --------------------------------------------------------------------------810 public void assertElementPresent() {811 assertElementPresent(EXPLICIT_TIMEOUT);812 }813 public void assertElementPresent(long timeout) {814 if (isElementPresent(timeout)) {815 Screenshot.capture(getDriver(), Messager.ELEMENT_PRESENT.getMessage(getName()));816 } else {817 Assert.fail(Messager.ELEMENT_NOT_PRESENT.getMessage(getNameWithLocator()));818 }819 }820 public void assertElementWithTextPresent(final String text) {821 assertElementWithTextPresent(text, EXPLICIT_TIMEOUT);822 }823 public void assertElementWithTextPresent(final String text, long timeout) {824 if (isElementWithTextPresent(text, timeout)) {825 Screenshot.capture(getDriver(), Messager.ELEMENT_WITH_TEXT_PRESENT.getMessage(getName(), text));826 } else {827 Assert.fail(Messager.ELEMENT_WITH_TEXT_NOT_PRESENT.getMessage(getNameWithLocator(), text));828 }829 }830 /**831 * Find Extended Web Element on page using By starting search from this object.832 *833 * @param by Selenium By locator834 * @return ExtendedWebElement if exists otherwise null.835 */836 public ExtendedWebElement findExtendedWebElement(By by) {837 return findExtendedWebElement(by, by.toString(), EXPLICIT_TIMEOUT);838 }...
Source:DriverHelper.java
...330 * @param text331 * of element to check.332 * @return element with text existence status.333 */334 public boolean isElementWithTextPresent(final ExtendedWebElement extWebElement, final String text) {335 return isElementWithTextPresent(extWebElement, text, EXPLICIT_TIMEOUT);336 }337 /**338 * Check that element with text present.339 * 340 * @param extWebElement to check if element with text is present341 * @param text342 * of element to check.343 * @param timeout Long344 * @return element with text existence status.345 */346 public boolean isElementWithTextPresent(final ExtendedWebElement extWebElement, final String text, long timeout) {347 return extWebElement.isElementWithTextPresent(text, timeout);348 }349 /**350 * Check that element not present on page.351 * 352 * @param extWebElement to check if element is not present353 * 354 * @return element non-existence status.355 */356 public boolean isElementNotPresent(final ExtendedWebElement extWebElement) {357 return isElementNotPresent(extWebElement, EXPLICIT_TIMEOUT);358 }359 /**360 * Check that element not present on page.361 * ...
Source:LoginAndDashboardPage.java
...133 public String successfullogin() 134 {135 txtdashboard.isPresent(timeout);136 String vActualRes=txtdashboard.getText();137 txtdashboard.isElementWithTextPresent(vActualRes);138 return vActualRes; 139 }140 141 public String unSuccessfulLogin() 142 {143 try {144 Thread.sleep(3000);145 } catch (InterruptedException e) {146 // TODO Auto-generated catch block147 e.printStackTrace();148 }149 btnsignIn.isPresent(timeout);150 String vActualRes=btnsignIn.getText();151 System.out.println(vActualRes);152 btnsignIn.isElementWithTextPresent(vActualRes);153 return vActualRes; 154 }155 public void searchLead(String sLeadID) 156 {157 editSearchLID.click();158 159 editSearchLID.type(sLeadID);160 161 ((JavascriptExecutor) driver).executeScript("mobile: performEditorAction", ImmutableMap.of("action", "Search"));162 163 txtLeadID.isPresent(timeout);164 165 }166 ...
isElementWithTextPresent
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.support.FindBy;4import org.openqa.selenium.support.PageFactory;5import org.testng.Assert;6import org.testng.annotations.Test;7import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;8import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy;9import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy.OpeningStrategy;10import com.qaprosoft.carina.core.foundation.webdriver.listener.DriverListener;11public class Test1 extends DriverListener {12 private ExtendedWebElement welcomeText;13 public void test1() {14 WebDriver driver = getDriver();15 PageFactory.initElements(driver, this);16 Assert.assertTrue(welcomeText.isElementWithTextPresent("Welcome", 10, OpeningStrategy.CLICK, "click"));17 }18}19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.WebElement;21import org.openqa.selenium.support.FindBy;22import org.openqa.selenium.support.PageFactory;23import org.testng.Assert;24import org.testng.annotations.Test;25import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;26import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy;27import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy.OpeningStrategy;28import com.qaprosoft.carina.core.foundation.webdriver.listener.DriverListener;29public class Test2 extends DriverListener {30 private ExtendedWebElement welcomeText;31 public void test1() {32 WebDriver driver = getDriver();33 PageFactory.initElements(driver, this);34 Assert.assertTrue(welcomeText.isElementWithTextPresent("Welcome"));
isElementWithTextPresent
Using AI Code Generation
1import java.util.List;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.testng.Assert;6import org.testng.annotations.Test;7import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;8import com.qaprosoft.carina.demo.gui.pages.HomePage;9import com.qaprosoft.carina.demo.gui.pages.NewsPage;10import com.qaprosoft.carina.demo.gui.pages.NewsPageBase;11import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem;12import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle;13import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText;14import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink;15import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon;16import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage;17import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText;18import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText.ArticleTitleImageTextLink;19import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText.ArticleTitleImageTextLink.ArticleTitleImageTextLinkText;20import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText.ArticleTitleImageTextLink.ArticleTitleImageTextLinkText.ArticleTitleImageTextLinkTextIcon;21import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText.ArticleTitleImageTextLink.ArticleTitleImageTextLinkText.ArticleTitleImageTextLinkTextIcon.ArticleTitleImageTextLinkTextIconText;22import com.qaprosoft.carina.demo.gui.pages.NewsPageBase.ArticleItem.ArticleTitle.ArticleTitleText.ArticleTitleLink.ArticleTitleIcon.ArticleTitleImage.ArticleTitleImageText.ArticleTitleImageTextLink.ArticleTitle
isElementWithTextPresent
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.support.FindBy;4import org.openqa.selenium.support.PageFactory;5import org.testng.Assert;6import org.testng.annotations.Test;7import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;8import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;9import com.qaprosoft.carina.core.gui.AbstractPage;
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!!