@Override public void executeAfterJSPreconditionHasBeenSatisfied(WebDriver driver, ReplayingState state) { By elementsLocator = By.className(initialCollectorClass); List<WebElement> webElements = driver.findElements(elementsLocator); Optional<WebElement> webElementOptional = webElements.stream() .filter(this::elementTextIsEqualToAndIsDisplayed) .findFirst(); if (!webElementOptional.isPresent()) { throw new ElementNotSelectableException("Element with class " + initialCollectorClass + " and text " + text + " was not found" ); } webElementOptional.get().click(); }
@Test public void throwsExceptionIfNoSuitableElementsAreFound() { WebElement incorrectWebElement = mock(WebElement.class); when(incorrectWebElement.getAttribute(INNER_HTML)).thenReturn("sometATPhing"); when(incorrectWebElement.isDisplayed()).thenReturn(true); List<WebElement> webElements = new ArrayList<>(); webElements.add(incorrectWebElement); By elementsLocator = By.className(initialCollectorClass); WebDriver webDriver = mock(WebDriver.class); when(webDriver.findElements(elementsLocator)).thenReturn(webElements); ClickClassByText clickClassByText = new ClickClassByText(initialCollectorClass, text, eventName, expectsHttp); thrown.expect(ElementNotSelectableException.class); clickClassByText.executeAfterJSPreconditionHasBeenSatisfied(webDriver, mock(ReplayingState.class)); }
@Override protected ExecuteResult execute(final WebSiteConnector connector) { try { final Alert alert = connector.getDriver().switchTo().alert(); alert.sendKeys(insertVariableValues(text)); LOGGER.info(LEARNER_MARKER, "Send text '{}' to prompt window (ignoreFailure: {}, negated: {}).", text, ignoreFailure, negated); return getSuccessOutput(); } catch (NoAlertPresentException | ElementNotSelectableException e) { LOGGER.info(LEARNER_MARKER, "Failed to send text '{}' to prompt window (ignoreFailure: {}, negated: {}).", text, ignoreFailure, negated); return getFailedOutput(); } }
@Test public void throwsExceptionIfNoElementsAreFound() { List<WebElement> webElements = new ArrayList<>(); By elementsLocator = By.className(initialCollectorClass); WebDriver webDriver = mock(WebDriver.class); when(webDriver.findElements(elementsLocator)).thenReturn(webElements); ClickClassByText clickClassByText = new ClickClassByText(initialCollectorClass, text, eventName, expectsHttp); thrown.expect(ElementNotSelectableException.class); clickClassByText.executeAfterJSPreconditionHasBeenSatisfied(webDriver, mock(ReplayingState.class)); }