Path: blob/trunk/java/test/org/openqa/selenium/ElementEqualityTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2122import java.util.List;23import org.junit.jupiter.api.Test;24import org.openqa.selenium.remote.RemoteWebElement;25import org.openqa.selenium.testing.JupiterTestBase;26import org.openqa.selenium.testing.NotYetImplemented;27import org.openqa.selenium.testing.SwitchToTopAfterTest;2829class ElementEqualityTest extends JupiterTestBase {3031@Test32void testSameElementLookedUpDifferentWaysShouldBeEqual() {33driver.get(pages.simpleTestPage);3435WebElement body = driver.findElement(By.tagName("body"));36WebElement xbody = driver.findElements(By.xpath("//body")).get(0);3738assertThat(xbody).isEqualTo(body);39}4041@Test42void testDifferentElementsShouldNotBeEqual() {43driver.get(pages.simpleTestPage);4445List<WebElement> ps = driver.findElements(By.tagName("p"));4647assertThat(ps.get(0).equals(ps.get(1))).isFalse();48}4950@Test51void testSameElementLookedUpDifferentWaysUsingFindElementShouldHaveSameHashCode() {52driver.get(pages.simpleTestPage);53WebElement body = driver.findElement(By.tagName("body"));54WebElement xbody = driver.findElement(By.xpath("//body"));5556assertThat(xbody.hashCode()).isEqualTo(body.hashCode());57}5859@Test60void testSameElementLookedUpDifferentWaysUsingFindElementsShouldHaveSameHashCode() {61driver.get(pages.simpleTestPage);62List<WebElement> body = driver.findElements(By.tagName("body"));63List<WebElement> xbody = driver.findElements(By.xpath("//body"));6465assertThat(xbody.get(0).hashCode()).isEqualTo(body.get(0).hashCode());66}6768@SwitchToTopAfterTest69@Test70@NotYetImplemented(SAFARI)71public void testAnElementFoundInViaJsShouldHaveSameId() {72driver.get(pages.missedJsReferencePage);7374driver.switchTo().frame("inner");75WebElement first = driver.findElement(By.id("oneline"));7677WebElement element =78(WebElement)79((JavascriptExecutor) driver)80.executeScript("return document.getElementById('oneline');");8182checkIdEqualityIfRemote(first, element);83}8485private void checkIdEqualityIfRemote(WebElement first, WebElement second) {86String firstId = getId(unwrapIfNecessary(first));87String secondId = getId(unwrapIfNecessary(second));8889assertThat(secondId).isEqualTo(firstId);90}9192private String getId(WebElement element) {93if (!(element instanceof RemoteWebElement)) {94System.err.println("Skipping remote element equality test - not a remote web driver");95return null;96}9798return ((RemoteWebElement) element).getId();99}100101private WebElement unwrapIfNecessary(WebElement element) {102if (element instanceof WrapsElement) {103return ((WrapsElement) element).getWrappedElement();104}105return element;106}107}108109110