下面列出了怎么用org.openqa.selenium.WrapsElement的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof WebElement))
{
return false;
}
WebElement that = (WebElement) obj;
if (that instanceof WrapsElement)
{
that = ((WrapsElement) that).getWrappedElement();
}
return wrappedElement.equals(that);
}
/**
* This method extract an instance of {@link WebDriver} from the given {@link SearchContext}.
* @param searchContext is an instance of {@link SearchContext}. It may be the instance of
* {@link WebDriver} or {@link org.openqa.selenium.WebElement} or some other
* user's extension/implementation.
* Note: if you want to use your own implementation then it should implement
* {@link WrapsDriver} or {@link WrapsElement}
* @return the instance of {@link WebDriver}.
* Note: if the given {@link SearchContext} is not
* {@link WebDriver} and it doesn't implement
* {@link WrapsDriver} or {@link WrapsElement} then this method returns null.
*
*/
public static WebDriver unpackWebDriverFromSearchContext(SearchContext searchContext) {
if (searchContext instanceof WebDriver) {
return (WebDriver) searchContext;
}
if (searchContext instanceof WrapsDriver) {
return unpackWebDriverFromSearchContext(
((WrapsDriver) searchContext).getWrappedDriver());
}
// Search context it is not only Webdriver. Webelement is search context too.
// RemoteWebElement and MobileElement implement WrapsDriver
if (searchContext instanceof WrapsElement) {
return unpackWebDriverFromSearchContext(
((WrapsElement) searchContext).getWrappedElement());
}
return null;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WebElement)) {
return false;
}
WebElement other = (WebElement) obj;
while (other instanceof WrapsElement) {
other = ((WrapsElement) other).getWrappedElement();
}
if (!(other instanceof RemoteWebElement)) {
return false;
}
RemoteWebElement otherRemoteWebElement = (RemoteWebElement) other;
return id.equals(otherRemoteWebElement.id);
}
@Test
public void shouldWrapElementFoundWhenCallingScripts() {
final WebDriver mockedDriver = mock(WebDriver.class,
withSettings().extraInterfaces(JavascriptExecutor.class));
final WebElement stubbedElement = mock(WebElement.class);
when(((JavascriptExecutor) mockedDriver).executeScript("foo"))
.thenReturn(stubbedElement);
EventFiringWebDriver testedDriver = new EventFiringWebDriver(mockedDriver);
Object res = testedDriver.executeScript("foo");
verify((JavascriptExecutor) mockedDriver).executeScript("foo");
assertThat(res).isInstanceOf(WebElement.class).isInstanceOf(WrapsElement.class);
assertThat(((WrapsElement) res).getWrappedElement()).isSameAs(stubbedElement);
}
@Test
public void testShouldUnpackListOfElementArgsWhenCallingScripts() {
final WebDriver mockedDriver = mock(WebDriver.class,
withSettings().extraInterfaces(JavascriptExecutor.class));
final WebElement mockElement = mock(WebElement.class);
when(mockedDriver.findElement(By.id("foo"))).thenReturn(mockElement);
EventFiringWebDriver testedDriver = new EventFiringWebDriver(mockedDriver);
testedDriver.register(new AbstractWebDriverEventListener() {});
final WebElement foundElement = testedDriver.findElement(By.id("foo"));
assertThat(foundElement).isInstanceOf(WrapsElement.class);
assertThat(((WrapsElement) foundElement).getWrappedElement()).isSameAs(mockElement);
List<Object> args = Arrays.asList("before", foundElement, "after");
testedDriver.executeScript("foo", args);
verify((JavascriptExecutor) mockedDriver).executeScript("foo", args);
}
@Test
public void shouldWrapMultipleElementsFoundWhenCallingScripts() {
final WebDriver mockedDriver = mock(WebDriver.class,
withSettings().extraInterfaces(JavascriptExecutor.class));
final WebElement stubbedElement1 = mock(WebElement.class);
final WebElement stubbedElement2 = mock(WebElement.class);
when(((JavascriptExecutor) mockedDriver).executeScript("foo"))
.thenReturn(Arrays.asList(stubbedElement1, stubbedElement2));
EventFiringWebDriver testedDriver = new EventFiringWebDriver(mockedDriver);
Object res = testedDriver.executeScript("foo");
verify((JavascriptExecutor) mockedDriver).executeScript("foo");
assertThat(res).isInstanceOf(List.class);
List<Object> resList = (List<Object>) res;
resList.forEach(el -> assertThat(el).isInstanceOf(WrapsElement.class));
assertThat(((WrapsElement) resList.get(0)).getWrappedElement()).isSameAs(stubbedElement1);
assertThat(((WrapsElement) resList.get(1)).getWrappedElement()).isSameAs(stubbedElement2);
}
@Test
public void testShouldUnpackMapOfElementArgsWhenCallingScripts() {
final WebDriver mockedDriver = mock(WebDriver.class,
withSettings().extraInterfaces(JavascriptExecutor.class));
final WebElement mockElement = mock(WebElement.class);
when(mockedDriver.findElement(By.id("foo"))).thenReturn(mockElement);
EventFiringWebDriver testedDriver = new EventFiringWebDriver(mockedDriver);
testedDriver.register(mock(WebDriverEventListener.class));
final WebElement foundElement = testedDriver.findElement(By.id("foo"));
assertThat(foundElement).isInstanceOf(WrapsElement.class);
assertThat(((WrapsElement) foundElement).getWrappedElement()).isSameAs(mockElement);
ImmutableMap<String, Object> args = ImmutableMap.of(
"foo", "bar",
"element", foundElement,
"nested", Arrays.asList("before", foundElement, "after")
);
testedDriver.executeScript("foo", args);
verify((JavascriptExecutor) mockedDriver).executeScript("foo", args);
}
@Override
public Boolean call() {
WebElement one = getElement();
WebElement two = getKnownElements().get(otherId);
// Unwrap the elements, if necessary
if (one instanceof WrapsElement) {
one = ((WrapsElement) one).getWrappedElement();
}
if (two instanceof KnownElements.ProxiedElement) {
two = ((KnownElements.ProxiedElement) two).getWrappedElement();
}
return one.equals(two);
}
@Override
public ClickResult click(WrapsElement element)
{
if (element != null)
{
return click(element.getWrappedElement());
}
return new ClickResult();
}
@Test
void clickWrapsElement()
{
WrapsElement wrapsElement = mock(WrapsElement.class);
WebElement webElement = mock(WebElement.class);
when(wrapsElement.getWrappedElement()).thenReturn(webElement);
MouseActions spy = spy(mouseActions);
ClickResult expectedResult = new ClickResult();
doReturn(expectedResult).when(spy).click(webElement);
ClickResult actualResult = spy.click(wrapsElement);
assertEquals(expectedResult, actualResult);
}
@Test
void clickWrapsElementNull()
{
mouseActions.click((WrapsElement) null);
verifyNoInteractions(webDriverProvider);
verifyNoInteractions(webUiContext);
}
@Test
void testAssertElementStateNullWrapsElement()
{
State state = mock(State.class);
boolean result = baseValidations.assertElementState(BUSINESS_DESCRIPTION, state, (WrapsElement) null);
verifyNoInteractions(state);
assertFalse(result);
}
@Test
void testAssertElementStateNWhenWrapsNullElement()
{
WrapsElement wrapsElement = mock(WrapsElement.class);
when(wrapsElement.getWrappedElement()).thenReturn(null);
State state = mock(State.class);
boolean result = baseValidations.assertElementState(BUSINESS_DESCRIPTION, state, wrapsElement);
verifyNoInteractions(state);
assertFalse(result);
}
@Test
void testAssertWrapsElementStateSuccess()
{
when(mockedWebDriverProvider.get()).thenReturn(mockedWebDriver);
String mockedExpectedConditionToString = mockedExpectedCondition.toString();
WrapsElement wrapsElement = mock(WrapsElement.class);
when(wrapsElement.getWrappedElement()).thenReturn(mockedWebElement);
State state = mock(State.class);
doReturn(mockedExpectedCondition).when(state).getExpectedCondition(mockedWebElement);
when(softAssert.assertThat(eq(BUSINESS_DESCRIPTION), eq(mockedExpectedConditionToString),
eq(mockedWebDriver), argThat(matcher -> matcher instanceof ExpectedConditionsMatcher)))
.thenReturn(Boolean.TRUE);
assertTrue(baseValidations.assertElementState(BUSINESS_DESCRIPTION, state, wrapsElement));
}
@Test
void ifDropDownWithNameFoundState()
{
WebElement element = findDropDownListWithParameters(false);
dropdownSteps.isDropDownWithNameFound(DropDownState.ENABLED, DROP_DOWN_LIST_NAME);
verify(baseValidations).assertElementState(eq("The found drop down is ENABLED"), eq(DropDownState.ENABLED),
argThat((WrapsElement select) -> select.getWrappedElement().equals(element)));
}
@Test
void testIfCheckboxWithAttributeAndStateExist()
{
when(baseValidations.assertIfElementExists(
String.format(CHECKBOX_WITH_ATTR_VALUE, CHECKBOX_ATTRIBUTE_TYPE, CHECKBOX_ATTRIBUTE_VALUE),
new SearchAttributes(ActionAttributeType.XPATH, CHECKBOX_XPATH))).thenReturn(webElement);
checkboxSteps.ifCheckboxWithAttributeExists(State.ENABLED, CHECKBOX_ATTRIBUTE_TYPE, CHECKBOX_ATTRIBUTE_VALUE);
verify(baseValidations).assertElementState(THE_FOUND_CHECKBOX_IS + State.ENABLED, State.ENABLED,
(WrapsElement) new Checkbox(webElement));
}
public Object asArg() {
Object arg = originObject;
while (arg instanceof WrapsElement) {
arg = ((WrapsElement) arg).getWrappedElement();
}
return arg;
}
private Class<?>[] extractInterfaces(Object object) {
Set<Class<?>> allInterfaces = new HashSet<>();
allInterfaces.add(WrapsDriver.class);
if (object instanceof WebElement) {
allInterfaces.add(WrapsElement.class);
}
extractInterfaces(allInterfaces, object.getClass());
return allInterfaces.toArray(new Class<?>[allInterfaces.size()]);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WebElement)) {
return false;
}
WebElement other = (WebElement) obj;
if (other instanceof WrapsElement) {
other = ((WrapsElement) other).getWrappedElement();
}
return underlyingElement.equals(other);
}
public static <T> T unwrap(WebElement webElement, Class<T> clazz)
{
return unwrap(webElement, WrapsElement.class, WrapsElement::getWrappedElement, clazz);
}
@Override
public boolean assertElementState(String businessDescription, IState state, WrapsElement element)
{
return element != null && assertElementState(businessDescription, state, element.getWrappedElement());
}
/**
* Checks if a checkbox with the specified <b>attribute</b> exists in context and it has expected state
* <p>Actions performed at this step:</p>
* <ul>
* <li>Finds a checkbox specified by an <b>attribute type</b> with an <b>attribute value</b>;</li>
* <li>Compares an actual checkbox 'state' with expected;</li>
* </ul>
* @param state A state value of the element (<i>Possible values:</i>
* <b>ENABLED, DISABLED, SELECTED, NOT_SELECTED, VISIBLE,
* NOT_VISIBLE</b>)
* @param attributeType A type of the attribute (for ex. <i>'name', 'id', 'title'</i>)
* @param attributeValue A value of the attribute
* @return Web element - a <b>checkbox</b> that meets the requirements,
* <b> null</b> - if there are no expected elements.
*/
@Then("a [$state] checkbox with the attribute '$attributeType'='$attributeValue' exists")
public WebElement ifCheckboxWithAttributeExists(State state, String attributeType, String attributeValue)
{
Checkbox checkbox = ifCheckboxWithAttributeExists(attributeType, attributeValue);
baseValidations.assertElementState(THE_FOUND_CHECKBOX_IS + state, state, (WrapsElement) checkbox);
return checkbox;
}
ClickResult click(WrapsElement element);
boolean assertElementState(String businessDescription, IState state, WrapsElement element);