Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ByTest.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.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.assertj.core.api.Assertions.assertThatNoException;
23
import static org.mockito.Mockito.mock;
24
import static org.mockito.Mockito.verify;
25
import static org.mockito.Mockito.verifyNoMoreInteractions;
26
import static org.openqa.selenium.By.ByClassName;
27
import static org.openqa.selenium.By.ByCssSelector;
28
import static org.openqa.selenium.By.ById;
29
import static org.openqa.selenium.By.ByLinkText;
30
import static org.openqa.selenium.By.ByName;
31
import static org.openqa.selenium.By.ByPartialLinkText;
32
import static org.openqa.selenium.By.ByTagName;
33
import static org.openqa.selenium.By.ByXPath;
34
import static org.openqa.selenium.json.Json.MAP_TYPE;
35
36
import java.util.List;
37
import java.util.Map;
38
import org.junit.jupiter.api.Tag;
39
import org.junit.jupiter.api.Test;
40
import org.openqa.selenium.json.Json;
41
42
@Tag("UnitTests")
43
class ByTest {
44
45
@Test
46
void shouldUseFindsByNameToLocateElementsByName() {
47
final SearchContext driver = mock(SearchContext.class);
48
49
By.cssSelector("cheese").findElement(driver);
50
By.cssSelector("peas").findElements(driver);
51
52
verify(driver).findElement(By.cssSelector("cheese"));
53
verify(driver).findElements(By.cssSelector("peas"));
54
verifyNoMoreInteractions(driver);
55
}
56
57
@Test
58
void shouldUseXpathLocateElementsByXpath() {
59
SearchContext driver = mock(SearchContext.class);
60
61
By.xpath(".//*[@name = 'cheese']").findElement(driver);
62
By.xpath(".//*[@name = 'peas']").findElements(driver);
63
64
verify(driver).findElement(By.xpath(".//*[@name = 'cheese']"));
65
verify(driver).findElements(By.xpath(".//*[@name = 'peas']"));
66
verifyNoMoreInteractions(driver);
67
}
68
69
@Test
70
void searchesByTagNameIfSupported() {
71
SearchContext context = mock(SearchContext.class);
72
73
By.tagName("foo").findElement(context);
74
By.tagName("bar").findElements(context);
75
76
verify(context).findElement(By.tagName("foo"));
77
verify(context).findElements(By.tagName("bar"));
78
verifyNoMoreInteractions(context);
79
}
80
81
@Test
82
void innerClassesArePublicSoThatTheyCanBeReusedElsewhere() {
83
assertThat(new ByXPath("a")).hasToString("By.xpath: a");
84
assertThat(new ById("a")).hasToString("By.id: a");
85
assertThat(new ByClassName("a")).hasToString("By.className: a");
86
assertThat(new ByLinkText("a")).hasToString("By.linkText: a");
87
assertThat(new ByName("a")).hasToString("By.name: a");
88
assertThat(new ByTagName("a")).hasToString("By.tagName: a");
89
assertThat(new ByCssSelector("a")).hasToString("By.cssSelector: a");
90
assertThat(new ByPartialLinkText("a")).hasToString("By.partialLinkText: a");
91
}
92
93
// See https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/2917
94
@Test
95
void testHashCodeDoesNotFallIntoEndlessRecursion() {
96
By locator =
97
new By() {
98
@Override
99
public List<WebElement> findElements(SearchContext context) {
100
return null;
101
}
102
};
103
assertThatNoException().isThrownBy(locator::hashCode);
104
}
105
106
@Test
107
void ensureMultipleClassNamesAreNotAccepted() {
108
assertThatExceptionOfType(InvalidSelectorException.class)
109
.isThrownBy(() -> By.className("one two"));
110
}
111
112
@Test
113
void ensureIdIsSerializedProperly() {
114
// Although it's not legal, make sure we handle the case where people use spaces.
115
By by = By.id("one two");
116
117
Json json = new Json();
118
Map<String, Object> blob = json.toType(json.toJson(by), MAP_TYPE);
119
120
assertThat(blob)
121
.hasSize(2)
122
.containsEntry("using", "css selector")
123
.containsEntry("value", "#one\\ two");
124
}
125
}
126
127