Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ElementEqualityTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
22
23
import java.util.List;
24
import org.junit.jupiter.api.Test;
25
import org.openqa.selenium.remote.RemoteWebElement;
26
import org.openqa.selenium.testing.JupiterTestBase;
27
import org.openqa.selenium.testing.NotYetImplemented;
28
import org.openqa.selenium.testing.SwitchToTopAfterTest;
29
30
class ElementEqualityTest extends JupiterTestBase {
31
32
@Test
33
void testSameElementLookedUpDifferentWaysShouldBeEqual() {
34
driver.get(pages.simpleTestPage);
35
36
WebElement body = driver.findElement(By.tagName("body"));
37
WebElement xbody = driver.findElements(By.xpath("//body")).get(0);
38
39
assertThat(xbody).isEqualTo(body);
40
}
41
42
@Test
43
void testDifferentElementsShouldNotBeEqual() {
44
driver.get(pages.simpleTestPage);
45
46
List<WebElement> ps = driver.findElements(By.tagName("p"));
47
48
assertThat(ps.get(0).equals(ps.get(1))).isFalse();
49
}
50
51
@Test
52
void testSameElementLookedUpDifferentWaysUsingFindElementShouldHaveSameHashCode() {
53
driver.get(pages.simpleTestPage);
54
WebElement body = driver.findElement(By.tagName("body"));
55
WebElement xbody = driver.findElement(By.xpath("//body"));
56
57
assertThat(xbody.hashCode()).isEqualTo(body.hashCode());
58
}
59
60
@Test
61
void testSameElementLookedUpDifferentWaysUsingFindElementsShouldHaveSameHashCode() {
62
driver.get(pages.simpleTestPage);
63
List<WebElement> body = driver.findElements(By.tagName("body"));
64
List<WebElement> xbody = driver.findElements(By.xpath("//body"));
65
66
assertThat(xbody.get(0).hashCode()).isEqualTo(body.get(0).hashCode());
67
}
68
69
@SwitchToTopAfterTest
70
@Test
71
@NotYetImplemented(SAFARI)
72
public void testAnElementFoundInViaJsShouldHaveSameId() {
73
driver.get(pages.missedJsReferencePage);
74
75
driver.switchTo().frame("inner");
76
WebElement first = driver.findElement(By.id("oneline"));
77
78
WebElement element =
79
(WebElement)
80
((JavascriptExecutor) driver)
81
.executeScript("return document.getElementById('oneline');");
82
83
checkIdEqualityIfRemote(first, element);
84
}
85
86
private void checkIdEqualityIfRemote(WebElement first, WebElement second) {
87
String firstId = getId(unwrapIfNecessary(first));
88
String secondId = getId(unwrapIfNecessary(second));
89
90
assertThat(secondId).isEqualTo(firstId);
91
}
92
93
private String getId(WebElement element) {
94
if (!(element instanceof RemoteWebElement)) {
95
System.err.println("Skipping remote element equality test - not a remote web driver");
96
return null;
97
}
98
99
return ((RemoteWebElement) element).getId();
100
}
101
102
private WebElement unwrapIfNecessary(WebElement element) {
103
if (element instanceof WrapsElement) {
104
return ((WrapsElement) element).getWrappedElement();
105
}
106
return element;
107
}
108
}
109
110