How to use attributeToBe method of org.openqa.selenium.support.ui.ExpectedConditions class

Best Selenium code snippet using org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe

Source:ExpectedConditionsTest.java Github

copy

Full Screen

...25import static org.mockito.Mockito.verifyZeroInteractions;26import static org.mockito.Mockito.when;27import static org.openqa.selenium.support.ui.ExpectedConditions.and;28import static org.openqa.selenium.support.ui.ExpectedConditions.attributeContains;29import static org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe;30import static org.openqa.selenium.support.ui.ExpectedConditions.attributeToBeNotEmpty;31import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe;32import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOf;33import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfAllElements;34import static org.openqa.selenium.support.ui.ExpectedConditions.not;35import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBe;36import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBeLessThan;37import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBeMoreThan;38import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfWindowsToBe;39import static org.openqa.selenium.support.ui.ExpectedConditions.or;40import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfNestedElementLocatedBy;41import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfNestedElementsLocatedBy;42import static org.openqa.selenium.support.ui.ExpectedConditions.textMatches;43import static org.openqa.selenium.support.ui.ExpectedConditions.textToBe;44import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement;45import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated;46import static org.openqa.selenium.support.ui.ExpectedConditions.urlContains;47import static org.openqa.selenium.support.ui.ExpectedConditions.urlMatches;48import static org.openqa.selenium.support.ui.ExpectedConditions.urlToBe;49import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;50import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElements;51import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElementsLocatedBy;52import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfNestedElementsLocatedBy;53import com.google.common.collect.Sets;54import org.junit.Before;55import org.junit.Test;56import org.mockito.Mock;57import org.mockito.Mockito;58import org.mockito.MockitoAnnotations;59import org.openqa.selenium.By;60import org.openqa.selenium.NoSuchElementException;61import org.openqa.selenium.StaleElementReferenceException;62import org.openqa.selenium.TimeoutException;63import org.openqa.selenium.WebDriver;64import org.openqa.selenium.WebDriverException;65import org.openqa.selenium.WebElement;66import java.time.Duration;67import java.time.Instant;68import java.util.ArrayList;69import java.util.Arrays;70import java.util.List;71import java.util.Set;72import java.util.regex.Pattern;73public class ExpectedConditionsTest {74 @Mock75 private WebDriver mockDriver;76 @Mock77 private WebElement mockElement;78 @Mock79 private WebElement mockNestedElement;80 @Mock81 private java.time.Clock mockClock;82 @Mock83 private Sleeper mockSleeper;84 @Mock85 private GenericCondition mockCondition;86 private FluentWait<WebDriver> wait;87 @Before88 public void setUpMocks() {89 MockitoAnnotations.initMocks(this);90 wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)91 .withTimeout(Duration.ofSeconds(1))92 .pollingEvery(Duration.ofMillis(250));93 // Set up a time series that extends past the end of our wait's timeout94 when(mockClock.instant()).thenReturn(95 EPOCH,96 EPOCH.plusMillis(250),97 EPOCH.plusMillis(500),98 EPOCH.plusMillis(1000),99 EPOCH.plusMillis(2000));100 }101 @Test102 public void waitingForUrlToBeOpened_urlToBe() {103 final String url = "http://some_url";104 when(mockDriver.getCurrentUrl()).thenReturn(url);105 wait.until(urlToBe(url));106 }107 @Test108 public void waitingForUrlToBeOpened_urlContains() {109 final String url = "http://some_url";110 when(mockDriver.getCurrentUrl()).thenReturn(url);111 wait.until(urlContains("some_url"));112 }113 @Test114 public void waitingForUrlToBeOpened_urlMatches() {115 final String url = "http://some-dynamic:4000/url";116 when(mockDriver.getCurrentUrl()).thenReturn(url);117 wait.until(urlMatches(".*:\\d{4}\\/url"));118 }119 @Test120 public void negative_waitingForUrlToBeOpened_urlToBe() {121 final String url = "http://some_url";122 when(mockDriver.getCurrentUrl()).thenReturn(url);123 assertThatExceptionOfType(TimeoutException.class)124 .isThrownBy(() -> wait.until(urlToBe(url + "/malformed")));125 }126 @Test127 public void negative_waitingForUrlToBeOpened_urlContains() {128 final String url = "http://some_url";129 when(mockDriver.getCurrentUrl()).thenReturn(url);130 assertThatExceptionOfType(TimeoutException.class)131 .isThrownBy(() -> wait.until(urlContains("/malformed")));132 }133 @Test134 public void negative_waitingForUrlToBeOpened_urlMatches() {135 final String url = "http://some-dynamic:4000/url";136 when(mockDriver.getCurrentUrl()).thenReturn(url);137 assertThatExceptionOfType(TimeoutException.class)138 .isThrownBy(() -> wait.until(urlMatches(".*\\/malformed.*")));139 }140 @Test141 public void waitingForVisibilityOfElement_elementAlreadyVisible() {142 when(mockElement.isDisplayed()).thenReturn(true);143 assertThat(wait.until(visibilityOf(mockElement))).isSameAs(mockElement);144 verifyZeroInteractions(mockSleeper);145 }146 @Test147 public void waitingForVisibilityOfElement_elementBecomesVisible() throws InterruptedException {148 when(mockElement.isDisplayed()).thenReturn(false, false, true);149 assertThat(wait.until(visibilityOf(mockElement))).isSameAs(mockElement);150 verify(mockSleeper, times(2)).sleep(Duration.ofMillis(250));151 }152 @Test153 public void waitingForVisibilityOfElement_elementNeverBecomesVisible()154 throws InterruptedException {155 Mockito.reset(mockClock);156 when(mockClock.instant()).thenReturn(EPOCH, EPOCH.plusMillis(500), EPOCH.plusMillis(3000));157 when(mockElement.isDisplayed()).thenReturn(false, false);158 assertThatExceptionOfType(TimeoutException.class)159 .isThrownBy(() -> wait.until(visibilityOf(mockElement)));160 verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));161 }162 @Test163 public void waitingForVisibilityOfElementInverse_elementNotVisible() {164 when(mockElement.isDisplayed()).thenReturn(false);165 assertThat(wait.until(not(visibilityOf(mockElement)))).isTrue();166 verifyZeroInteractions(mockSleeper);167 }168 @Test169 public void booleanExpectationsCanBeNegated() {170 ExpectedCondition<Boolean> expectation = not(obj -> false);171 assertThat(expectation.apply(mockDriver)).isTrue();172 }173 @Test174 public void waitingForVisibilityOfElementInverse_elementStaysVisible()175 throws InterruptedException {176 Mockito.reset(mockClock);177 when(mockClock.instant()).thenReturn(EPOCH, EPOCH.plusMillis(500), EPOCH.plusMillis(3000));178 when(mockElement.isDisplayed()).thenReturn(true, true);179 assertThatExceptionOfType(TimeoutException.class)180 .isThrownBy(() -> wait.until(not(visibilityOf(mockElement))));181 verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));182 }183 @Test184 public void invertingAConditionThatReturnsFalse() {185 ExpectedCondition<Boolean> expectation = not(obj -> false);186 assertThat(expectation.apply(mockDriver)).isTrue();187 }188 @Test189 public void invertingAConditionThatReturnsNull() {190 when(mockCondition.apply(mockDriver)).thenReturn(null);191 assertThat(wait.until(not(mockCondition))).isTrue();192 verifyZeroInteractions(mockSleeper);193 }194 @Test195 public void invertingAConditionThatAlwaysReturnsTrueTimesout() {196 ExpectedCondition<Boolean> expectation = not(obj -> true);197 assertThat(expectation.apply(mockDriver)).isFalse();198 }199 @Test200 public void doubleNegatives_conditionThatReturnsFalseTimesOut() {201 ExpectedCondition<Boolean> expectation = not(not(obj -> false));202 assertThat(expectation.apply(mockDriver)).isFalse();203 }204 @Test205 public void doubleNegatives_conditionThatReturnsNull() {206 ExpectedCondition<Boolean> expectation = not(not(obj -> null));207 assertThat(expectation.apply(mockDriver)).isFalse();208 }209 @Test210 public void waitingForVisibilityOfAllElementsLocatedByReturnsListOfElements() {211 List<WebElement> webElements = singletonList(mockElement);212 String testSelector = "testSelector";213 when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);214 when(mockElement.isDisplayed()).thenReturn(true);215 List<WebElement> returnedElements =216 wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));217 assertThat(returnedElements).isEqualTo(webElements);218 }219 @Test220 public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenElementNotDisplayed() {221 List<WebElement> webElements = singletonList(mockElement);222 String testSelector = "testSelector";223 when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);224 when(mockElement.isDisplayed()).thenReturn(false);225 assertThatExceptionOfType(TimeoutException.class)226 .isThrownBy(() -> wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))));227 }228 @Test229 public void waitingForVisibilityOfAllElementsLocatedByThrowsStaleExceptionWhenElementIsStale() {230 List<WebElement> webElements = singletonList(mockElement);231 String testSelector = "testSelector";232 when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);233 when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));234 assertThatExceptionOfType(StaleElementReferenceException.class)235 .isThrownBy(() -> wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))));236 }237 @Test238 public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenNoElementsFound() {239 List<WebElement> webElements = new ArrayList<>();240 String testSelector = "testSelector";241 when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);242 assertThatExceptionOfType(TimeoutException.class)243 .isThrownBy(() -> wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))));244 }245 @Test246 public void waitingForVisibilityOfAllElementsReturnsListOfElements() {247 List<WebElement> webElements = singletonList(mockElement);248 when(mockElement.isDisplayed()).thenReturn(true);249 List<WebElement> returnedElements = wait.until(visibilityOfAllElements(webElements));250 assertThat(returnedElements).isEqualTo(webElements);251 }252 @Test253 public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenElementNotDisplayed() {254 List<WebElement> webElements = singletonList(mockElement);255 when(mockElement.isDisplayed()).thenReturn(false);256 assertThatExceptionOfType(TimeoutException.class)257 .isThrownBy(() -> wait.until(visibilityOfAllElements(webElements)));258 }259 @Test260 public void waitingForVisibilityOfAllElementsThrowsStaleElementReferenceExceptionWhenElementIsStale() {261 List<WebElement> webElements = singletonList(mockElement);262 when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));263 assertThatExceptionOfType(StaleElementReferenceException.class)264 .isThrownBy(() -> wait.until(visibilityOfAllElements(webElements)));265 }266 @Test267 public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenNoElementsFound() {268 List<WebElement> webElements = new ArrayList<>();269 assertThatExceptionOfType(TimeoutException.class)270 .isThrownBy(() -> wait.until(visibilityOfAllElements(webElements)));271 }272 @Test273 public void waitingForVisibilityOfReturnsElement() {274 when(mockElement.isDisplayed()).thenReturn(true);275 WebElement returnedElement = wait.until(visibilityOf(mockElement));276 assertThat(returnedElement).isEqualTo(mockElement);277 }278 @Test279 public void waitingForVisibilityOfThrowsTimeoutExceptionWhenElementNotDisplayed() {280 when(mockElement.isDisplayed()).thenReturn(false);281 assertThatExceptionOfType(TimeoutException.class)282 .isThrownBy(() -> wait.until(visibilityOf(mockElement)));283 }284 @Test285 public void waitingForVisibilityOfThrowsStaleElementReferenceExceptionWhenElementIsStale() {286 when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));287 assertThatExceptionOfType(StaleElementReferenceException.class)288 .isThrownBy(() -> wait.until(visibilityOf(mockElement)));289 }290 @Test291 public void waitingForTextToBePresentInElementLocatedReturnsElement() {292 String testSelector = "testSelector";293 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);294 when(mockElement.getText()).thenReturn("testText");295 assertThat(296 wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText")))297 .isTrue();298 }299 @Test300 public void waitingForTextToBePresentInElementLocatedReturnsElementWhenTextContainsSaidText() {301 String testSelector = "testSelector";302 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);303 when(mockElement.getText()).thenReturn("testText");304 assertThat(wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "test")))305 .isTrue();306 }307 @Test308 public void waitingForHtmlAttributeToBeEqualForElementLocatedReturnsTrueWhenAttributeIsEqualToSaidText() {309 String testSelector = "testSelector";310 String attributeName = "attributeName";311 String attributeValue = "attributeValue";312 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);313 when(mockElement.getAttribute(attributeName)).thenReturn(attributeValue);314 when(mockElement.getCssValue(attributeName)).thenReturn("");315 assertThat(wait.until(316 attributeToBe(By.cssSelector(testSelector), attributeName, attributeValue))).isTrue();317 }318 @Test319 public void waitingForCssAttributeToBeEqualForElementLocatedReturnsTrueWhenAttributeIsEqualToSaidText() {320 String testSelector = "testSelector";321 String attributeName = "attributeName";322 String attributeValue = "attributeValue";323 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);324 when(mockElement.getAttribute(attributeName)).thenReturn("");325 when(mockElement.getCssValue(attributeName)).thenReturn(attributeValue);326 assertThat(wait.until(327 attributeToBe(By.cssSelector(testSelector), attributeName, attributeValue))).isTrue();328 }329 @Test330 public void waitingForCssAttributeToBeEqualForElementLocatedThrowsTimeoutExceptionWhenAttributeIsNotEqual() {331 String testSelector = "testSelector";332 String attributeName = "attributeName";333 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);334 when(mockElement.getAttribute(attributeName)).thenReturn("");335 when(mockElement.getCssValue(attributeName)).thenReturn("");336 assertThatExceptionOfType(TimeoutException.class)337 .isThrownBy(() -> wait.until(attributeToBe(By.cssSelector(testSelector), attributeName, "test")));338 }339 @Test340 public void waitingForHtmlAttributeToBeEqualForWebElementReturnsTrueWhenAttributeIsEqualToSaidText() {341 String attributeName = "attributeName";342 String attributeValue = "attributeValue";343 when(mockElement.getAttribute(attributeName)).thenReturn(attributeValue);344 when(mockElement.getCssValue(attributeName)).thenReturn("");345 assertThat(wait.until(attributeToBe(mockElement, attributeName, attributeValue))).isTrue();346 }347 @Test348 public void waitingForCssAttributeToBeEqualForWebElementReturnsTrueWhenAttributeIsEqualToSaidText() {349 String attributeName = "attributeName";350 String attributeValue = "attributeValue";351 when(mockElement.getAttribute(attributeName)).thenReturn("");352 when(mockElement.getCssValue(attributeName)).thenReturn(attributeValue);353 assertThat(wait.until(attributeToBe(mockElement, attributeName, attributeValue))).isTrue();354 }355 @Test356 public void waitingForCssAttributeToBeEqualForWebElementThrowsTimeoutExceptionWhenAttributeIsNotEqual() {357 String attributeName = "attributeName";358 when(mockElement.getAttribute(attributeName)).thenReturn("");359 when(mockElement.getCssValue(attributeName)).thenReturn("");360 assertThatExceptionOfType(TimeoutException.class)361 .isThrownBy(() -> wait.until(attributeToBe(mockElement, attributeName, "test")));362 }363 @Test364 public void waitingForHtmlAttributeToBeEqualForElementLocatedReturnsTrueWhenAttributeContainsEqualToSaidText() {365 String testSelector = "testSelector";366 String attributeName = "attributeName";367 String attributeValue = "test attributeValue test";368 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);369 when(mockElement.getAttribute(attributeName)).thenReturn(attributeValue);370 when(mockElement.getCssValue(attributeName)).thenReturn("");371 assertThat(wait.until(372 attributeContains(By.cssSelector(testSelector), attributeName, "attributeValue"))).isTrue();373 }374 @Test375 public void waitingForCssAttributeToBeEqualForElementLocatedReturnsTrueWhenAttributeContainsEqualToSaidText() {376 String testSelector = "testSelector";377 String attributeName = "attributeName";378 String attributeValue = "test attributeValue test";379 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);380 when(mockElement.getAttribute(attributeName)).thenReturn("");381 when(mockElement.getCssValue(attributeName)).thenReturn(attributeValue);382 assertThat(wait.until(383 attributeContains(By.cssSelector(testSelector), attributeName, "attributeValue"))).isTrue();384 }385 @Test386 public void waitingForCssAttributeToBeEqualForElementLocatedThrowsTimeoutExceptionWhenAttributeContainsNotEqual() {387 By parent = By.cssSelector("parent");388 String attributeName = "attributeName";389 when(mockDriver.findElement(parent)).thenReturn(mockElement);390 when(mockElement.getAttribute(attributeName)).thenReturn("");391 when(mockElement.getCssValue(attributeName)).thenReturn("");392 assertThatExceptionOfType(TimeoutException.class)393 .isThrownBy(() -> wait.until(attributeContains(parent, attributeName, "test")));394 }395 @Test396 public void waitingForHtmlAttributeToBeEqualForWebElementReturnsTrueWhenAttributeContainsEqualToSaidText() {397 String attributeName = "attributeName";398 String attributeValue = "test attributeValue test";399 when(mockElement.getAttribute(attributeName)).thenReturn(attributeValue);400 when(mockElement.getCssValue(attributeName)).thenReturn("");401 assertThat(wait.until(attributeContains(mockElement, attributeName, "attributeValue"))).isTrue();402 }403 @Test404 public void waitingForCssAttributeToBeEqualForWebElementReturnsTrueWhenAttributeContainsEqualToSaidText() {405 String attributeName = "attributeName";406 String attributeValue = "test attributeValue test";407 when(mockElement.getAttribute(attributeName)).thenReturn("");408 when(mockElement.getCssValue(attributeName)).thenReturn(attributeValue);409 assertThat(wait.until(attributeContains(mockElement, attributeName, "attributeValue"))).isTrue();410 }411 @Test412 public void waitingForCssAttributeToBeEqualForWebElementThrowsTimeoutExceptionWhenAttributeContainsNotEqual() {413 String attributeName = "attributeName";414 when(mockElement.getAttribute(attributeName)).thenReturn("");415 when(mockElement.getCssValue(attributeName)).thenReturn("");416 assertThatExceptionOfType(TimeoutException.class)417 .isThrownBy(() -> wait.until(attributeContains(mockElement, attributeName, "test")));418 }419 @Test420 public void waitingForTextToBeEqualForElementLocatedReturnsTrueWhenTextIsEqualToSaidText() {421 String testSelector = "testSelector";422 String testText = "test text";423 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);424 when(mockElement.getText()).thenReturn(testText);425 assertThat(wait.until(textToBe(By.cssSelector(testSelector), testText))).isTrue();426 }427 @Test428 public void waitingForAttributeToBeNotEmptyForElementLocatedReturnsTrueWhenAttributeIsNotEmptyCss() {429 String attributeName = "test";430 when(mockElement.getAttribute(attributeName)).thenReturn("");431 when(mockElement.getCssValue(attributeName)).thenReturn("test1");432 assertThat(wait.until(attributeToBeNotEmpty(mockElement, attributeName))).isTrue();433 }434 @Test435 public void waitingForAttributeToBeNotEmptyForElementLocatedReturnsTrueWhenAttributeIsNotEmptyHtml() {436 String attributeName = "test";437 when(mockElement.getAttribute(attributeName)).thenReturn("test1");438 when(mockElement.getCssValue(attributeName)).thenReturn("");439 assertThat(wait.until(attributeToBeNotEmpty(mockElement, attributeName))).isTrue();440 }441 @Test442 public void waitingForTextToBeEqualForElementLocatedThrowsTimeoutExceptionWhenTextIsNotEqual() {443 String testSelector = "testSelector";444 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);445 when(mockElement.getText()).thenReturn("");446 assertThatExceptionOfType(TimeoutException.class)447 .isThrownBy(() -> wait.until(textToBe(By.cssSelector(testSelector), "test")));448 }449 @Test450 public void waitingForAttributetToBeNotEmptyForElementLocatedThrowsTimeoutExceptionWhenAttributeIsEmpty() {451 String attributeName = "test";452 when(mockElement.getAttribute(attributeName)).thenReturn("");453 when(mockElement.getCssValue(attributeName)).thenReturn("");454 assertThatExceptionOfType(TimeoutException.class)455 .isThrownBy(() -> wait.until(attributeToBeNotEmpty(mockElement, attributeName)));456 }457 @Test458 public void waitingForOneOfExpectedConditionsToHavePositiveResultWhenAllFailed() {459 String attributeName = "test";460 when(mockElement.getText()).thenReturn("");461 when(mockElement.getCssValue(attributeName)).thenReturn("");462 when(mockElement.getAttribute(attributeName)).thenReturn("");463 assertThatExceptionOfType(TimeoutException.class)464 .isThrownBy(() -> wait.until(or(textToBePresentInElement(mockElement, "test"),465 attributeToBe(mockElement, attributeName, "test"))));466 }467 @Test468 public void waitForOneOfExpectedConditionsToHavePositiveResultWhenFirstPositive() {469 String attributeName = "test";470 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);471 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);472 when(mockElement.getText()).thenReturn("");473 assertThat(wait.until(or(attributeToBe(mockElement, attributeName, attributeName),474 textToBePresentInElement(mockElement, attributeName)))).isTrue();475 }476 @Test477 public void waitForOneOfExpectedConditionsToHavePositiveResultWhenAllPositive() {478 String attributeName = "test";479 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);480 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);481 when(mockElement.getText()).thenReturn(attributeName);482 assertThat(wait.until(or(attributeToBe(mockElement, attributeName, attributeName),483 textToBePresentInElement(mockElement, attributeName)))).isTrue();484 }485 @Test486 public void waitForOneOfExpectedConditionsToHavePositiveResultWhenSecondPositive() {487 String attributeName = "test";488 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);489 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);490 when(mockElement.getText()).thenReturn("");491 assertThat(wait.until(or(textToBePresentInElement(mockElement, attributeName),492 attributeToBe(mockElement, attributeName, attributeName)))).isTrue();493 }494 @Test495 public void waitForOneOfExpectedConditionsToHavePositiveResultWhenOneThrows() {496 String attributeName = "test";497 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);498 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);499 when(mockElement.getText()).thenThrow(new NoSuchElementException(""));500 assertThat(wait.until(or(textToBePresentInElement(mockElement, attributeName),501 attributeToBe(mockElement, attributeName, attributeName)))).isTrue();502 }503 @Test504 public void waitForOneOfExpectedConditionsToHavePositiveResultWhenAllThrow() {505 String attributeName = "test";506 when(mockElement.getAttribute(attributeName)).thenThrow(new NoSuchElementException(""));507 when(mockElement.getCssValue(attributeName)).thenThrow(new NoSuchElementException(""));508 when(mockElement.getText()).thenThrow(new NoSuchElementException(""));509 assertThatExceptionOfType(NoSuchElementException.class)510 .isThrownBy(() -> wait.until(or(textToBePresentInElement(mockElement, attributeName),511 attributeToBe(mockElement, attributeName, attributeName))));512 }513 @Test514 public void waitingForAllExpectedConditionsToHavePositiveResultWhenAllFailed() {515 String attributeName = "test";516 when(mockElement.getText()).thenReturn("");517 when(mockElement.getCssValue(attributeName)).thenReturn("");518 when(mockElement.getAttribute(attributeName)).thenReturn("");519 assertThatExceptionOfType(TimeoutException.class)520 .isThrownBy(() -> wait.until(and(textToBePresentInElement(mockElement, "test"),521 attributeToBe(mockElement, attributeName, "test"))));522 }523 @Test524 public void waitingForAllExpectedConditionsToHavePositiveResultWhenFirstFailed() {525 String attributeName = "test";526 when(mockElement.getText()).thenReturn("");527 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);528 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);529 assertThatExceptionOfType(TimeoutException.class)530 .isThrownBy(() -> wait.until(and(textToBePresentInElement(mockElement, "test"),531 attributeToBe(mockElement, attributeName, attributeName))));532 }533 @Test534 public void waitingForAllExpectedConditionsToHavePositiveResultWhenSecondFailed() {535 String attributeName = "test";536 when(mockElement.getText()).thenReturn(attributeName);537 when(mockElement.getCssValue(attributeName)).thenReturn("");538 when(mockElement.getAttribute(attributeName)).thenReturn("");539 assertThatExceptionOfType(TimeoutException.class)540 .isThrownBy(() -> wait.until(and(textToBePresentInElement(mockElement, attributeName),541 attributeToBe(mockElement, attributeName, attributeName))));542 }543 @Test544 public void waitingForAllExpectedConditionsToHavePositiveResultWhenAllPositive() {545 String attributeName = "test";546 when(mockElement.getText()).thenReturn(attributeName);547 when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);548 when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);549 assertThat(wait.until(and(textToBePresentInElement(mockElement, attributeName),550 attributeToBe(mockElement, attributeName, attributeName)))).isTrue();551 }552 @Test553 public void waitingForTextMatchingPatternWhenTextExists() {554 String testSelector = "testSelector";555 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);556 when(mockElement.getText()).thenReturn("123");557 assertThat(wait.until(textMatches(By.cssSelector(testSelector), compile("\\d")))).isTrue();558 }559 @Test560 public void waitingForTextMatchingPatternWhenTextDoesntExist() {561 String testSelector = "testSelector";562 when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);563 when(mockElement.getText()).thenReturn("test");564 assertThatExceptionOfType(TimeoutException.class)...

Full Screen

Full Screen

Source:BasePage.java Github

copy

Full Screen

...70 }71 protected WebElement elementToBeVisible(WebElement element) {72 return wait.until(ExpectedConditions.visibilityOf(element));73 }74 protected Boolean attributeToBe(WebElement element, String attribute, String value) {75 return wait.until(ExpectedConditions.attributeToBe(element, attribute, value));76 }77 /**78 * Общий метод по заполнения полей ввода79 *80 * @param field - веб-елемент поле ввода81 * @param value - значение вводимое в поле82 */83 public void fillInputField(WebElement field, String value) {84 scrollToElementJs(field);85 elementToBeClickable(field).click();86 field.sendKeys(value);87 }88 /**89 * Общий метод по заполнению полей с датой...

Full Screen

Full Screen

Source:BaseTest.java Github

copy

Full Screen

...31 return getWait().withMessage("Element can not be located " + element)32 .until(ExpectedConditions.visibilityOf(element));33 }34 // public boolean waitForInputValueToBe(WebElement element,String text){35// return getWait().withMessage("No").until(ExpectedConditions.or(ExpectedConditions.attributeToBe(element,"value","xx")));36// }37// private FluentWait<WebDriver> getFluentWait(WebDriver webDriver) {38//39// return new FluentWait<>(getDriver());40// }41//42// public void fluentWait(By element) {43// wait.withMessage("error message").withTimeout(Duration.ofSeconds(30))44// .pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class)45// .until(ExpectedConditions.invisibilityOfElementLocated(element));46// }47//48// public void waitForAttribute(By element) {49// getFluentWait(getDriver()).withTimeout(Duration.ofSeconds(30))50// .pollingEvery(Duration.ofSeconds(1)).ignoring(NoSuchAttributeException.class)51// .until(ExpectedConditions.attributeToBe(element, "hidden", "true"));52// }53 @AfterMethod(alwaysRun = true)54 public void methodTearDown(ITestResult result){55 if(ITestResult.FAILURE==result.getStatus() ) {56 Helper helper =new Helper();57 String filePhat="C:\\Users\\HP\\Desktop\\FileScreen\\";58 String fileName = UUID.randomUUID()+".png";59 helper.screenShot(getDriver(),filePhat+fileName);60 }61 }62//@BeforeTest(alwaysRun = true)63// public void beforeTest(){64// System.out.println("before Test");65//}...

Full Screen

Full Screen

Source:BrowserUtils.java Github

copy

Full Screen

...24 System.out.println("Entering text as"+text);25 wait.until(ExpectedConditions.visibilityOf(element));26 element.clear();27 element.sendKeys(text);28 wait.until(ExpectedConditions.attributeToBe(element,"value", text));29 System.out.println("Entering text as"+text);30 }31}32//package renastech.utils;33//34//import org.openqa.selenium.WebElement;35//import org.openqa.selenium.support.ui.ExpectedCondition;36//import org.openqa.selenium.support.ui.ExpectedConditions;37//import org.openqa.selenium.support.ui.WebDriverWait;38//39//import java.util.concurrent.TimeUnit;40//41//public class BrowserUtils extends Driver {42//43// public static void staticWait(int second){44// try {45// Thread.sleep(1000 * second);46// } catch (InterruptedException e) {47// e.printStackTrace();48// }49// }50//51// public void setWaitTime(){52//53// driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);54// }55// public static WebDriverWait wait = new WebDriverWait(Driver.getDriver(),30);56//57// public static void clickAndWait(WebElement element){58// wait.until(ExpectedConditions.elementToBeClickable(element)).click();59// }60//61// public static void waitEnterText(WebElement element,String text){62//63// System.out.println("Entering Text as "+ text);64// wait.until(ExpectedConditions.visibilityOf(element));65// element.clear();66// element.sendKeys(text);67// wait.until(ExpectedConditions.attributeToBe(element,"value", text));68// System.out.println("Entered Text as "+ text);69//70//71// }72 //Sync issue73 // implicit way to do Wait74 // explicit way to do Wait75//}...

Full Screen

Full Screen

Source:WaitCondition.java Github

copy

Full Screen

...18 allPresent(ExpectedConditions::presenceOfAllElementsLocatedBy),19 valueToBe(ExpectedConditions::textToBe),20 textPresentInValue((BiFunction<By, String, ExpectedCondition<?>>)21 ExpectedConditions::textToBePresentInElementValue),22 attributeToBe((TriFunction<By, String, String, ExpectedCondition<?>>)23 ExpectedConditions::attributeToBe);24 private final TriFunction<?, ?, ?, ExpectedCondition<?>> type;25 <T, V> WaitCondition(final Function<T, ExpectedCondition<?>> condition) {26 this((T locator, V text) -> condition.apply(locator));27 }28 <T, V, E> WaitCondition(final BiFunction<T, V, ExpectedCondition<?>> condition) {29 this((T arg1, V arg2, E arg3) -> condition.apply(arg1, arg2));30 }31 @SuppressWarnings("unchecked")32 public <T, V, E, R> TriFunction<T, V, E, R> getType() {33 return (TriFunction<T, V, E, R>) type;34 }35}...

Full Screen

Full Screen

Source:ProductPage.java Github

copy

Full Screen

...35 public void AddNewProduct(String oldcount)36 {37 wait.until(elementToBeClickable(SpecialPropProduct()));38 add.click();39 wait.until(not(ExpectedConditions.attributeToBe(By.cssSelector("#cart .quantity"), "textContent", oldcount)));40 }41 // public By TextBasket() {return By.xpath("//span [contains(@class, 'quantity')]");}42}...

Full Screen

Full Screen

Source:Test146.java Github

copy

Full Screen

...20 FluentWait<ChromeDriver> w=new FluentWait<ChromeDriver>(driver);21 w.withTimeout(Duration.ofSeconds(20));22 w.pollingEvery(Duration.ofMillis(1000));23 ExpectedCondition<WebElement> ec1=ExpectedConditions.visibilityOfElementLocated(By.name("q"));24 ExpectedCondition<Boolean> ec2=ExpectedConditions.attributeToBe(By.name("q"),"title","search");25 w.until(ExpectedConditions.or(ec1,ec2));26 //close site27 driver.close();28 }29}...

Full Screen

Full Screen

Source:Helpers.java Github

copy

Full Screen

...7 public Helpers() {8 }9 public void waitForSpinnerToClose(WebDriverWait wait) {10 WebElement spinner = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='serverSideDataTable_processing']")));11 wait.until(ExpectedConditions.attributeToBe(spinner, "style", "display: block;"));12 wait.until(ExpectedConditions.attributeToBe(spinner, "style", "display: none;"));13 }14}...

Full Screen

Full Screen

attributeToBe

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7import org.testng.Assert;8import org.testng.annotations.AfterMethod;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.Test;11public class AttributeToBe {12 private WebDriver driver;13 private WebDriverWait wait;14 public void setUp() {15 driver = new ChromeDriver();16 wait = new WebDriverWait(driver, 10);17 }18 public void attributeToBe() {19 WebElement searchBox = driver.findElement(By.name("q"));20 searchBox.sendKeys("Selenium");21 WebElement searchButton = driver.findElement(By.name("btnK"));22 searchButton.click();23 wait.until(ExpectedConditions.attributeToBe(By.name("btnK"), "value", "Google Search"));24 Assert.assertEquals(searchButton.getAttribute("value"), "Google Search");25 }26 public void tearDown() {27 driver.quit();28 }29}30public static ExpectedCondition<Boolean> attributeContains(final By locator, final String attribute, final String value)31package com.javacodegeeks.testng;32import org.openqa.selenium.By;33import org.openqa.selenium.WebDriver;34import org.openqa.selenium.WebElement;35import org.openqa.selenium.chrome.ChromeDriver;36import org.openqa.selenium.support.ui.ExpectedConditions;37import org.openqa.selenium.support.ui.WebDriverWait;38import org.testng.Assert;39import org.testng.annotations.AfterMethod;40import org.testng.annotations.BeforeMethod;41import org.testng.annotations.Test;42public class AttributeContains {

Full Screen

Full Screen

attributeToBe

Using AI Code Generation

copy

Full Screen

1package com.automationtestinghub;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import org.testng.annotations.Test;9public class WaitForAttribute {10 public void waitForAttribute() {11 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Saurabh\\Downloads\\chromedriver_win32\\chromedriver.exe");12 WebDriver driver = new ChromeDriver();13 WebDriverWait wait = new WebDriverWait(driver, 20);14 System.out.println(element.getText());15 driver.quit();16 }17}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful