org.openqa.selenium.WrapsElement Java Examples

The following examples show how to use org.openqa.selenium.WrapsElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DelegatingWebElement.java    From vividus with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: ElementEquality.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: EventFiringWebDriverTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: EventFiringWebDriverTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: EventFiringWebDriverTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: EventFiringWebDriverTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: RemoteWebElement.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: WebDriverUnpackUtility.java    From java-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void clickWrapsElementNull()
{
    mouseActions.click((WrapsElement) null);
    verifyNoInteractions(webDriverProvider);
    verifyNoInteractions(webUiContext);
}
 
Example #10
Source File: BaseValidationsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testAssertElementStateNullWrapsElement()
{
    State state = mock(State.class);
    boolean result = baseValidations.assertElementState(BUSINESS_DESCRIPTION, state, (WrapsElement) null);
    verifyNoInteractions(state);
    assertFalse(result);
}
 
Example #11
Source File: BaseValidationsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: BaseValidationsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #13
Source File: DropdownStepsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example #14
Source File: CheckboxStepsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #15
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: PointerInput.java    From selenium with Apache License 2.0 5 votes vote down vote up
public Object asArg() {
  Object arg = originObject;
  while (arg instanceof WrapsElement) {
    arg = ((WrapsElement) arg).getWrappedElement();
  }
  return arg;
}
 
Example #17
Source File: EventFiringWebDriver.java    From selenium with Apache License 2.0 5 votes vote down vote up
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()]);
}
 
Example #18
Source File: EventFiringWebDriver.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: MouseActions.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public ClickResult click(WrapsElement element)
{
    if (element != null)
    {
        return click(element.getWrappedElement());
    }
    return new ClickResult();
}
 
Example #20
Source File: BaseValidations.java    From vividus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean assertElementState(String businessDescription, IState state, WrapsElement element)
{
    return element != null && assertElementState(businessDescription, state, element.getWrappedElement());
}
 
Example #21
Source File: WebDriverUtil.java    From vividus with Apache License 2.0 4 votes vote down vote up
public static <T> T unwrap(WebElement webElement, Class<T> clazz)
{
    return unwrap(webElement, WrapsElement.class, WrapsElement::getWrappedElement, clazz);
}
 
Example #22
Source File: CheckboxSteps.java    From vividus with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: IBaseValidations.java    From vividus with Apache License 2.0 votes vote down vote up
boolean assertElementState(String businessDescription, IState state, WrapsElement element); 
Example #24
Source File: IMouseActions.java    From vividus with Apache License 2.0 votes vote down vote up
ClickResult click(WrapsElement element);