FindElementAction extends AbstractSeleniumAction { @Override protected final void execute(SeleniumBrowser browser, TestContext context) { WebElement element = browser.getWebDriver().findElement(createBy(context)); if (element == null) { throw new CitrusRuntimeException(String.format("Failed to find element '%s' on page", property + "=" + propertyValue)); } validate(element, browser, context); execute(element, browser, context); } FindElementAction(Builder builder);  FindElementAction(String name, ElementActionBuilder<?, ?> builder); String getProperty(); String getPropertyValue(); String getTagName(); Map<String, String> getAttributes(); Map<String, String> getStyles(); boolean isDisplayed(); boolean isEnabled(); String getText(); By getBy();  }
@Test(dataProvider = "findByProvider") public void testExecuteFindBy(String property, String value, final By by) throws Exception { when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() { @Override public WebElement answer(InvocationOnMock invocation) throws Throwable { By select = (By) invocation.getArguments()[0]; Assert.assertEquals(select.getClass(), by.getClass()); Assert.assertEquals(select.toString(), by.toString()); return element; } }); FindElementAction action = new FindElementAction.Builder() .browser(seleniumBrowser) .element(property, value) .build(); action.execute(context); Assert.assertEquals(context.getVariableObject("button"), element); }
@Test public void testExecuteFindByVariableSupport() throws Exception { when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() { @Override public WebElement answer(InvocationOnMock invocation) throws Throwable { By select = (By) invocation.getArguments()[0]; Assert.assertEquals(select.getClass(), By.ById.class); Assert.assertEquals(select.toString(), By.id("clickMe").toString()); return element; } }); context.setVariable("myId", "clickMe"); FindElementAction action = new FindElementAction.Builder() .browser(seleniumBrowser) .element("id", "${myId}") .build(); action.execute(context); Assert.assertEquals(context.getVariableObject("button"), element); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to find element 'id=myButton' on page") public void testElementNotFound() { when(webDriver.findElement(any(By.class))).thenReturn(null); FindElementAction action = new FindElementAction.Builder() .browser(seleniumBrowser) .element("id", "myButton") .build(); action.execute(context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Unknown selector type: unsupported") public void testElementUnsupportedProperty() { FindElementAction action = new FindElementAction.Builder() .browser(seleniumBrowser) .element("unsupported", "wrong") .build(); action.execute(context); }