Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ElementFindingTest.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.openqa.selenium.testing.drivers.Browser.CHROME;
23
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
24
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
25
import static org.openqa.selenium.testing.drivers.Browser.IE;
26
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
27
28
import java.util.List;
29
import org.junit.jupiter.api.Test;
30
import org.openqa.selenium.testing.Ignore;
31
import org.openqa.selenium.testing.JupiterTestBase;
32
import org.openqa.selenium.testing.NeedsFreshDriver;
33
import org.openqa.selenium.testing.NotYetImplemented;
34
import org.openqa.selenium.testing.SwitchToTopAfterTest;
35
36
class ElementFindingTest extends JupiterTestBase {
37
38
// By.id positive
39
40
@Test
41
void testShouldBeAbleToFindASingleElementById() {
42
driver.get(pages.xhtmlTestPage);
43
WebElement element = driver.findElement(By.id("linkId"));
44
assertThat(element.getAttribute("id")).isEqualTo("linkId");
45
}
46
47
@Test
48
void testShouldBeAbleToFindASingleElementByNumericId() {
49
driver.get(pages.nestedPage);
50
WebElement element = driver.findElement(By.id("2"));
51
assertThat(element.getAttribute("id")).isEqualTo("2");
52
}
53
54
@Test
55
void testShouldBeAbleToFindASingleElementByIdWithNonAlphanumericCharacters() {
56
driver.get(pages.nestedPage);
57
WebElement element = driver.findElement(By.id("white space"));
58
assertThat(element.getText()).isEqualTo("space");
59
WebElement element2 = driver.findElement(By.id("css#.chars"));
60
assertThat(element2.getText()).isEqualTo("css escapes");
61
}
62
63
@Test
64
void testShouldBeAbleToFindMultipleElementsById() {
65
driver.get(pages.nestedPage);
66
List<WebElement> elements = driver.findElements(By.id("test_id"));
67
assertThat(elements).hasSize(2);
68
}
69
70
@Test
71
void testShouldBeAbleToFindMultipleElementsByNumericId() {
72
driver.get(pages.nestedPage);
73
List<WebElement> elements = driver.findElements(By.id("2"));
74
assertThat(elements).hasSize(8);
75
}
76
77
@Test
78
void testShouldBeAbleToFindMultipleElementsByIdWithNonAlphanumericCharacters() {
79
driver.get(pages.nestedPage);
80
List<WebElement> elements = driver.findElements(By.id("white space"));
81
assertThat(elements).hasSize(2);
82
List<WebElement> elements2 = driver.findElements(By.id("css#.chars"));
83
assertThat(elements2).hasSize(2);
84
}
85
86
// By.id negative
87
88
@Test
89
void testShouldNotBeAbleToLocateByIdASingleElementThatDoesNotExist() {
90
driver.get(pages.formPage);
91
assertThatExceptionOfType(NoSuchElementException.class)
92
.isThrownBy(() -> driver.findElement(By.id("nonExistentButton")));
93
}
94
95
@Test
96
void testShouldNotBeAbleToLocateByIdMultipleElementsThatDoNotExist() {
97
driver.get(pages.formPage);
98
List<WebElement> elements = driver.findElements(By.id("nonExistentButton"));
99
assertThat(elements.size()).isZero();
100
}
101
102
@Test
103
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
104
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
105
void testFindingASingleElementByEmptyIdShouldThrow() {
106
driver.get(pages.formPage);
107
assertThatExceptionOfType(InvalidSelectorException.class)
108
.isThrownBy(() -> driver.findElement(By.id("")));
109
}
110
111
@Test
112
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
113
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
114
public void testFindingMultipleElementsByEmptyIdShouldThrow() {
115
driver.get(pages.formPage);
116
assertThatExceptionOfType(InvalidSelectorException.class)
117
.isThrownBy(() -> driver.findElements(By.id("")));
118
}
119
120
@Test
121
void testFindingASingleElementByIdWithSpaceShouldThrow() {
122
driver.get(pages.formPage);
123
assertThatExceptionOfType(NoSuchElementException.class)
124
.isThrownBy(() -> driver.findElement(By.id("nonexistent button")));
125
}
126
127
@Test
128
void testFindingMultipleElementsByIdWithSpaceShouldReturnEmptyList() {
129
driver.get(pages.formPage);
130
List<WebElement> elements = driver.findElements(By.id("nonexistent button"));
131
assertThat(elements.size()).isZero();
132
}
133
134
// By.name positive
135
136
@Test
137
void testShouldBeAbleToFindASingleElementByName() {
138
driver.get(pages.formPage);
139
WebElement element = driver.findElement(By.name("checky"));
140
assertThat(element.getAttribute("value")).isEqualTo("furrfu");
141
}
142
143
@Test
144
void testShouldBeAbleToFindMultipleElementsByName() {
145
driver.get(pages.nestedPage);
146
List<WebElement> elements = driver.findElements(By.name("checky"));
147
assertThat(elements.size()).isGreaterThan(1);
148
}
149
150
@Test
151
void testShouldBeAbleToFindAnElementThatDoesNotSupportTheNameProperty() {
152
driver.get(pages.nestedPage);
153
WebElement element = driver.findElement(By.name("div1"));
154
assertThat(element.getAttribute("name")).isEqualTo("div1");
155
}
156
157
// By.name negative
158
159
@Test
160
void testShouldNotBeAbleToLocateByNameASingleElementThatDoesNotExist() {
161
driver.get(pages.formPage);
162
assertThatExceptionOfType(NoSuchElementException.class)
163
.isThrownBy(() -> driver.findElement(By.name("nonExistentButton")));
164
}
165
166
@Test
167
void testShouldNotBeAbleToLocateByNameMultipleElementsThatDoNotExist() {
168
driver.get(pages.formPage);
169
List<WebElement> elements = driver.findElements(By.name("nonExistentButton"));
170
assertThat(elements).isEmpty();
171
}
172
173
@Test
174
void testFindingASingleElementByEmptyNameShouldThrow() {
175
driver.get(pages.formPage);
176
assertThatExceptionOfType(NoSuchElementException.class)
177
.isThrownBy(() -> driver.findElement(By.name("")));
178
}
179
180
@Test
181
void testFindingMultipleElementsByEmptyNameShouldReturnEmptyList() {
182
driver.get(pages.formPage);
183
List<WebElement> elements = driver.findElements(By.name(""));
184
assertThat(elements).isEmpty();
185
}
186
187
@Test
188
void testFindingASingleElementByNameWithSpaceShouldThrow() {
189
driver.get(pages.formPage);
190
assertThatExceptionOfType(NoSuchElementException.class)
191
.isThrownBy(() -> driver.findElement(By.name("nonexistent button")));
192
}
193
194
@Test
195
void testFindingMultipleElementsByNameWithSpaceShouldReturnEmptyList() {
196
driver.get(pages.formPage);
197
List<WebElement> elements = driver.findElements(By.name("nonexistent button"));
198
assertThat(elements).isEmpty();
199
}
200
201
// By.tagName positive
202
203
@Test
204
void testShouldBeAbleToFindASingleElementByTagName() {
205
driver.get(pages.formPage);
206
WebElement element = driver.findElement(By.tagName("input"));
207
assertThat(element.getTagName().toLowerCase()).isEqualTo("input");
208
}
209
210
@Test
211
void testShouldBeAbleToFindMultipleElementsByTagName() {
212
driver.get(pages.formPage);
213
List<WebElement> elements = driver.findElements(By.tagName("input"));
214
assertThat(elements.size()).isGreaterThan(1);
215
}
216
217
// By.tagName negative
218
219
@Test
220
void testShouldNotBeAbleToLocateByTagNameASingleElementThatDoesNotExist() {
221
driver.get(pages.formPage);
222
assertThatExceptionOfType(NoSuchElementException.class)
223
.isThrownBy(() -> driver.findElement(By.tagName("nonExistentButton")));
224
}
225
226
@Test
227
void testShouldNotBeAbleToLocateByTagNameMultipleElementsThatDoNotExist() {
228
driver.get(pages.formPage);
229
List<WebElement> elements = driver.findElements(By.tagName("nonExistentButton"));
230
assertThat(elements).isEmpty();
231
}
232
233
@Test
234
void testFindingASingleElementByEmptyTagNameShouldThrow() {
235
driver.get(pages.formPage);
236
assertThatExceptionOfType(InvalidSelectorException.class)
237
.isThrownBy(() -> driver.findElement(By.tagName("")));
238
}
239
240
@Test
241
void testFindingMultipleElementsByEmptyTagNameShouldThrow() {
242
driver.get(pages.formPage);
243
assertThatExceptionOfType(InvalidSelectorException.class)
244
.isThrownBy(() -> driver.findElements(By.tagName("")));
245
}
246
247
@Test
248
void testFindingASingleElementByTagNameWithSpaceShouldThrow() {
249
driver.get(pages.formPage);
250
assertThatExceptionOfType(NoSuchElementException.class)
251
.isThrownBy(() -> driver.findElement(By.tagName("nonexistent button")));
252
}
253
254
@Test
255
void testFindingMultipleElementsByTagNameWithSpaceShouldReturnEmptyList() {
256
driver.get(pages.formPage);
257
List<WebElement> elements = driver.findElements(By.tagName("nonexistent button"));
258
assertThat(elements).isEmpty();
259
}
260
261
// By.className positive
262
263
@Test
264
void testShouldBeAbleToFindASingleElementByClass() {
265
driver.get(pages.xhtmlTestPage);
266
WebElement element = driver.findElement(By.className("extraDiv"));
267
assertThat(element.getText()).startsWith("Another div starts here.");
268
}
269
270
@Test
271
void testShouldBeAbleToFindMultipleElementsByClassName() {
272
driver.get(pages.xhtmlTestPage);
273
List<WebElement> elements = driver.findElements(By.className("nameC"));
274
assertThat(elements.size()).isGreaterThan(1);
275
}
276
277
@Test
278
void testShouldFindElementByClassWhenItIsTheFirstNameAmongMany() {
279
driver.get(pages.xhtmlTestPage);
280
WebElement element = driver.findElement(By.className("nameA"));
281
assertThat(element.getText()).isEqualTo("An H2 title");
282
}
283
284
@Test
285
void testShouldFindElementByClassWhenItIsTheLastNameAmongMany() {
286
driver.get(pages.xhtmlTestPage);
287
WebElement element = driver.findElement(By.className("nameC"));
288
assertThat(element.getText()).isEqualTo("An H2 title");
289
}
290
291
@Test
292
void testShouldFindElementByClassWhenItIsInTheMiddleAmongMany() {
293
driver.get(pages.xhtmlTestPage);
294
WebElement element = driver.findElement(By.className("nameBnoise"));
295
assertThat(element.getText()).isEqualTo("An H2 title");
296
}
297
298
@Test
299
void testShouldFindElementByClassWhenItsNameIsSurroundedByWhitespace() {
300
driver.get(pages.xhtmlTestPage);
301
WebElement element = driver.findElement(By.className("spaceAround"));
302
assertThat(element.getText()).isEqualTo("Spaced out");
303
}
304
305
@Test
306
void testShouldFindElementsByClassWhenItsNameIsSurroundedByWhitespace() {
307
driver.get(pages.xhtmlTestPage);
308
List<WebElement> elements = driver.findElements(By.className("spaceAround"));
309
assertThat(elements).hasSize(1);
310
assertThat(elements.get(0).getText()).isEqualTo("Spaced out");
311
}
312
313
// By.className negative
314
315
@Test
316
void testShouldNotFindElementByClassWhenTheNameQueriedIsShorterThanCandidateName() {
317
driver.get(pages.xhtmlTestPage);
318
assertThatExceptionOfType(NoSuchElementException.class)
319
.isThrownBy(() -> driver.findElement(By.className("nameB")));
320
}
321
322
@Test
323
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
324
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
325
void testFindingASingleElementByEmptyClassNameShouldThrow() {
326
driver.get(pages.xhtmlTestPage);
327
assertThatExceptionOfType(InvalidSelectorException.class)
328
.isThrownBy(() -> driver.findElement(By.className("")));
329
}
330
331
@Test
332
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
333
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
334
void testFindingMultipleElementsByEmptyClassNameShouldThrow() {
335
driver.get(pages.xhtmlTestPage);
336
assertThatExceptionOfType(InvalidSelectorException.class)
337
.isThrownBy(() -> driver.findElements(By.className("")));
338
}
339
340
@Test
341
void testFindingASingleElementByCompoundClassNameShouldThrow() {
342
driver.get(pages.xhtmlTestPage);
343
assertThatExceptionOfType(InvalidSelectorException.class)
344
.isThrownBy(() -> driver.findElement(By.className("a b")));
345
}
346
347
@Test
348
void testFindingMultipleElementsByCompoundClassNameShouldThrow() {
349
driver.get(pages.xhtmlTestPage);
350
assertThatExceptionOfType(InvalidSelectorException.class)
351
.isThrownBy(() -> driver.findElements(By.className("a b")));
352
}
353
354
@Test
355
public void testShouldBeAbleToFindASingleElementByAWeirdLookingClassName() {
356
driver.get(pages.xhtmlTestPage);
357
WebElement element = driver.findElement(By.className("cls-!@#$%^&*"));
358
assertThat(element.getAttribute("class")).isEqualTo("cls-!@#$%^&*");
359
}
360
361
@Test
362
public void testShouldBeAbleToFindMultipleElementsByAWeirdLookingClassName() {
363
driver.get(pages.xhtmlTestPage);
364
List<WebElement> elements = driver.findElements(By.className("cls-!@#$%^&*"));
365
assertThat(elements).hasSize(1);
366
assertThat(elements.get(0).getAttribute("class")).isEqualTo("cls-!@#$%^&*");
367
}
368
369
// By.xpath positive
370
371
@Test
372
void testShouldBeAbleToFindASingleElementByXPath() {
373
driver.get(pages.xhtmlTestPage);
374
WebElement element = driver.findElement(By.xpath("//h1"));
375
assertThat(element.getText()).isEqualTo("XHTML Might Be The Future");
376
}
377
378
@Test
379
void testShouldBeAbleToFindMultipleElementsByXPath() {
380
driver.get(pages.xhtmlTestPage);
381
List<WebElement> elements = driver.findElements(By.xpath("//div"));
382
assertThat(elements).hasSize(13);
383
}
384
385
@Test
386
void testShouldBeAbleToFindManyElementsRepeatedlyByXPath() {
387
driver.get(pages.xhtmlTestPage);
388
String xpathString = "//node()[contains(@id,'id')]";
389
assertThat(driver.findElements(By.xpath(xpathString))).hasSize(3);
390
391
xpathString = "//node()[contains(@id,'nope')]";
392
assertThat(driver.findElements(By.xpath(xpathString))).isEmpty();
393
}
394
395
@Test
396
void testShouldBeAbleToIdentifyElementsByClass() {
397
driver.get(pages.xhtmlTestPage);
398
WebElement header = driver.findElement(By.xpath("//h1[@class='header']"));
399
assertThat(header.getText()).isEqualTo("XHTML Might Be The Future");
400
}
401
402
@Test
403
void testShouldBeAbleToFindAnElementByXPathWithMultipleAttributes() {
404
driver.get(pages.formPage);
405
WebElement element =
406
driver.findElement(
407
By.xpath("//form[@name='optional']/input[@type='submit' and @value='Click!']"));
408
assertThat(element.getTagName()).isEqualToIgnoringCase("input");
409
assertThat(element.getAttribute("value")).isEqualTo("Click!");
410
}
411
412
@Test
413
void testFindingALinkByXpathShouldLocateAnElementWithTheGivenText() {
414
driver.get(pages.xhtmlTestPage);
415
WebElement element = driver.findElement(By.xpath("//a[text()='click me']"));
416
assertThat(element.getText()).isEqualTo("click me");
417
}
418
419
@Test
420
void testFindingALinkByXpathUsingContainsKeywordShouldWork() {
421
driver.get(pages.nestedPage);
422
WebElement element = driver.findElement(By.xpath("//a[contains(.,'hello world')]"));
423
assertThat(element.getText()).contains("hello world");
424
}
425
426
@Test
427
@Ignore(IE)
428
@NotYetImplemented(FIREFOX)
429
@NotYetImplemented(SAFARI)
430
public void testShouldBeAbleToFindElementByXPathWithNamespace() {
431
driver.get(pages.svgPage);
432
WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));
433
assertThat(element.getText()).isEqualTo("Test Chart");
434
}
435
436
// By.xpath negative
437
438
@Test
439
void testShouldThrowAnExceptionWhenThereIsNoLinkToClick() {
440
driver.get(pages.xhtmlTestPage);
441
assertThatExceptionOfType(NoSuchElementException.class)
442
.isThrownBy(() -> driver.findElement(By.xpath("//a[@id='Not here']")));
443
}
444
445
@Test
446
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
447
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
448
void testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInDriverFindElement() {
449
driver.get(pages.formPage);
450
assertThatExceptionOfType(InvalidSelectorException.class)
451
.isThrownBy(() -> driver.findElement(By.xpath("this][isnot][valid")));
452
}
453
454
@Test
455
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
456
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
457
void
458
testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInDriverFindElements() {
459
driver.get(pages.formPage);
460
assertThatExceptionOfType(InvalidSelectorException.class)
461
.isThrownBy(() -> driver.findElements(By.xpath("this][isnot][valid")));
462
}
463
464
@Test
465
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
466
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
467
void
468
testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInElementFindElement() {
469
driver.get(pages.formPage);
470
WebElement body = driver.findElement(By.tagName("body"));
471
assertThatExceptionOfType(InvalidSelectorException.class)
472
.isThrownBy(() -> body.findElement(By.xpath("this][isnot][valid")));
473
}
474
475
@Test
476
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
477
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
478
void
479
testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInElementFindElements() {
480
driver.get(pages.formPage);
481
WebElement body = driver.findElement(By.tagName("body"));
482
assertThatExceptionOfType(InvalidSelectorException.class)
483
.isThrownBy(() -> body.findElements(By.xpath("this][isnot][valid")));
484
}
485
486
@Test
487
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
488
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
489
void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInDriverFindElement() {
490
driver.get(pages.formPage);
491
assertThatExceptionOfType(InvalidSelectorException.class)
492
.isThrownBy(() -> driver.findElement(By.xpath("count(//input)")));
493
}
494
495
@Test
496
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
497
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
498
void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInDriverFindElements() {
499
driver.get(pages.formPage);
500
assertThatExceptionOfType(InvalidSelectorException.class)
501
.isThrownBy(() -> driver.findElements(By.xpath("count(//input)")));
502
}
503
504
@Test
505
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
506
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
507
void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInElementFindElement() {
508
driver.get(pages.formPage);
509
510
WebElement body = driver.findElement(By.tagName("body"));
511
assertThatExceptionOfType(InvalidSelectorException.class)
512
.isThrownBy(() -> body.findElement(By.xpath("count(//input)")));
513
}
514
515
@Test
516
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
517
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
518
void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInElementFindElements() {
519
driver.get(pages.formPage);
520
WebElement body = driver.findElement(By.tagName("body"));
521
assertThatExceptionOfType(InvalidSelectorException.class)
522
.isThrownBy(() -> body.findElements(By.xpath("count(//input)")));
523
}
524
525
// By.cssSelector positive
526
527
@Test
528
void testShouldBeAbleToFindASingleElementByCssSelector() {
529
driver.get(pages.xhtmlTestPage);
530
WebElement element = driver.findElement(By.cssSelector("div.content"));
531
assertThat(element.getTagName()).isEqualToIgnoringCase("div");
532
assertThat(element.getAttribute("class")).isEqualTo("content");
533
}
534
535
@Test
536
void testShouldBeAbleToFindMultipleElementsByCssSelector() {
537
driver.get(pages.xhtmlTestPage);
538
List<WebElement> elements = driver.findElements(By.cssSelector("p"));
539
assertThat(elements.size()).isGreaterThan(1);
540
}
541
542
@Test
543
void testShouldBeAbleToFindASingleElementByCompoundCssSelector() {
544
driver.get(pages.xhtmlTestPage);
545
WebElement element = driver.findElement(By.cssSelector("div.extraDiv, div.content"));
546
assertThat(element.getTagName()).isEqualToIgnoringCase("div");
547
assertThat(element.getAttribute("class")).isEqualTo("content");
548
}
549
550
@Test
551
void testShouldBeAbleToFindMultipleElementsByCompoundCssSelector() {
552
driver.get(pages.xhtmlTestPage);
553
List<WebElement> elements = driver.findElements(By.cssSelector("div.extraDiv, div.content"));
554
assertThat(elements.size()).isGreaterThan(1);
555
assertThat(elements.get(0).getAttribute("class")).isEqualTo("content");
556
assertThat(elements.get(1).getAttribute("class")).isEqualTo("extraDiv");
557
}
558
559
@Test
560
void testShouldBeAbleToFindAnElementByBooleanAttributeUsingCssSelector() {
561
driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected.html"));
562
WebElement element = driver.findElement(By.cssSelector("option[selected='selected']"));
563
assertThat(element.getAttribute("value")).isEqualTo("two");
564
}
565
566
@Test
567
void testShouldBeAbleToFindAnElementByBooleanAttributeUsingShortCssSelector() {
568
driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected.html"));
569
WebElement element = driver.findElement(By.cssSelector("option[selected]"));
570
assertThat(element.getAttribute("value")).isEqualTo("two");
571
}
572
573
@Test
574
void testShouldBeAbleToFindAnElementByBooleanAttributeUsingShortCssSelectorOnHtml4Page() {
575
driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected_html4.html"));
576
WebElement element = driver.findElement(By.cssSelector("option[selected]"));
577
assertThat(element.getAttribute("value")).isEqualTo("two");
578
}
579
580
// By.cssSelector negative
581
582
@Test
583
void testShouldNotFindElementByCssSelectorWhenThereIsNoSuchElement() {
584
driver.get(pages.xhtmlTestPage);
585
assertThatExceptionOfType(NoSuchElementException.class)
586
.isThrownBy(() -> driver.findElement(By.cssSelector(".there-is-no-such-class")));
587
}
588
589
@Test
590
void testShouldNotFindElementsByCssSelectorWhenThereIsNoSuchElement() {
591
driver.get(pages.xhtmlTestPage);
592
List<WebElement> elements = driver.findElements(By.cssSelector(".there-is-no-such-class"));
593
assertThat(elements).isEmpty();
594
}
595
596
@Test
597
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
598
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
599
void testFindingASingleElementByEmptyCssSelectorShouldThrow() {
600
driver.get(pages.xhtmlTestPage);
601
assertThatExceptionOfType(InvalidSelectorException.class)
602
.isThrownBy(() -> driver.findElement(By.cssSelector("")));
603
}
604
605
@Test
606
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
607
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
608
void testFindingMultipleElementsByEmptyCssSelectorShouldThrow() {
609
driver.get(pages.xhtmlTestPage);
610
assertThatExceptionOfType(InvalidSelectorException.class)
611
.isThrownBy(() -> driver.findElements(By.cssSelector("")));
612
}
613
614
@Test
615
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
616
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
617
void testFindingASingleElementByInvalidCssSelectorShouldThrow() {
618
driver.get(pages.xhtmlTestPage);
619
assertThatExceptionOfType(InvalidSelectorException.class)
620
.isThrownBy(() -> driver.findElement(By.cssSelector("//a/b/c[@id='1']")));
621
}
622
623
@Test
624
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
625
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
626
void testFindingMultipleElementsByInvalidCssSelectorShouldThrow() {
627
driver.get(pages.xhtmlTestPage);
628
assertThatExceptionOfType(InvalidSelectorException.class)
629
.isThrownBy(() -> driver.findElements(By.cssSelector("//a/b/c[@id='1']")));
630
}
631
632
// By.linkText positive
633
634
@Test
635
void testShouldBeAbleToFindALinkByText() {
636
driver.get(pages.xhtmlTestPage);
637
WebElement link = driver.findElement(By.linkText("click me"));
638
assertThat(link.getText()).isEqualTo("click me");
639
}
640
641
@Test
642
void testShouldBeAbleToFindMultipleLinksByText() {
643
driver.get(pages.xhtmlTestPage);
644
List<WebElement> elements = driver.findElements(By.linkText("click me"));
645
assertThat(elements).hasSize(2);
646
}
647
648
@Test
649
void testShouldFindElementByLinkTextContainingEqualsSign() {
650
driver.get(pages.xhtmlTestPage);
651
WebElement element = driver.findElement(By.linkText("Link=equalssign"));
652
assertThat(element.getAttribute("id")).isEqualTo("linkWithEqualsSign");
653
}
654
655
@Test
656
void testShouldFindMultipleElementsByLinkTextContainingEqualsSign() {
657
driver.get(pages.xhtmlTestPage);
658
List<WebElement> elements = driver.findElements(By.linkText("Link=equalssign"));
659
assertThat(elements).hasSize(1);
660
assertThat(elements.get(0).getAttribute("id")).isEqualTo("linkWithEqualsSign");
661
}
662
663
@Test
664
void findsByLinkTextOnXhtmlPage() {
665
driver.get(appServer.whereIs("actualXhtmlPage.xhtml"));
666
String linkText = "Foo";
667
WebElement element = driver.findElement(By.linkText(linkText));
668
assertThat(element.getText()).isEqualTo(linkText);
669
}
670
671
@Test
672
void testLinkWithFormattingTags() {
673
driver.get(pages.simpleTestPage);
674
WebElement elem = driver.findElement(By.id("links"));
675
676
WebElement res = elem.findElement(By.partialLinkText("link with formatting tags"));
677
assertThat(res.getText()).isEqualTo("link with formatting tags");
678
}
679
680
@Test
681
@NotYetImplemented(SAFARI)
682
public void testDriverCanGetLinkByLinkTestIgnoringTrailingWhitespace() {
683
driver.get(pages.simpleTestPage);
684
WebElement link = driver.findElement(By.linkText("link with trailing space"));
685
assertThat(link.getAttribute("id")).isEqualTo("linkWithTrailingSpace");
686
assertThat(link.getText()).isEqualTo("link with trailing space");
687
}
688
689
// By.linkText negative
690
691
@Test
692
void testShouldNotBeAbleToLocateByLinkTextASingleElementThatDoesNotExist() {
693
driver.get(pages.xhtmlTestPage);
694
assertThatExceptionOfType(NoSuchElementException.class)
695
.isThrownBy(() -> driver.findElement(By.linkText("Not here either")));
696
}
697
698
@Test
699
void testShouldNotBeAbleToLocateByLinkTextMultipleElementsThatDoNotExist() {
700
driver.get(pages.xhtmlTestPage);
701
List<WebElement> elements = driver.findElements(By.linkText("Not here either"));
702
assertThat(elements.size()).isZero();
703
}
704
705
// By.partialLinkText positive
706
707
@Test
708
void testShouldBeAbleToFindMultipleElementsByPartialLinkText() {
709
driver.get(pages.xhtmlTestPage);
710
List<WebElement> elements = driver.findElements(By.partialLinkText("ick me"));
711
assertThat(elements.size()).isEqualTo(2);
712
}
713
714
@Test
715
void testShouldBeAbleToFindASingleElementByPartialLinkText() {
716
driver.get(pages.xhtmlTestPage);
717
WebElement element = driver.findElement(By.partialLinkText("anon"));
718
assertThat(element.getText()).contains("anon");
719
}
720
721
@Test
722
void testShouldFindElementByPartialLinkTextContainingEqualsSign() {
723
driver.get(pages.xhtmlTestPage);
724
WebElement element = driver.findElement(By.partialLinkText("Link="));
725
assertThat(element.getAttribute("id")).isEqualTo("linkWithEqualsSign");
726
}
727
728
@Test
729
void testShouldFindMultipleElementsByPartialLinkTextContainingEqualsSign() {
730
driver.get(pages.xhtmlTestPage);
731
List<WebElement> elements = driver.findElements(By.partialLinkText("Link="));
732
assertThat(elements).hasSize(1);
733
assertThat(elements.get(0).getAttribute("id")).isEqualTo("linkWithEqualsSign");
734
}
735
736
// Misc tests
737
738
@Test
739
void testDriverShouldBeAbleToFindElementsAfterLoadingMoreThanOnePageAtATime() {
740
driver.get(pages.formPage);
741
driver.get(pages.xhtmlTestPage);
742
WebElement link = driver.findElement(By.linkText("click me"));
743
assertThat(link.getText()).isEqualTo("click me");
744
}
745
746
// You don't want to ask why this is here
747
@Test
748
void testWhenFindingByNameShouldNotReturnById() {
749
driver.get(pages.formPage);
750
751
WebElement element = driver.findElement(By.name("id-name1"));
752
assertThat(element.getAttribute("value")).isEqualTo("name");
753
754
element = driver.findElement(By.id("id-name1"));
755
assertThat(element.getAttribute("value")).isEqualTo("id");
756
757
element = driver.findElement(By.name("id-name2"));
758
assertThat(element.getAttribute("value")).isEqualTo("name");
759
760
element = driver.findElement(By.id("id-name2"));
761
assertThat(element.getAttribute("value")).isEqualTo("id");
762
}
763
764
@Test
765
void testShouldBeAbleToFindAHiddenElementsByName() {
766
driver.get(pages.formPage);
767
WebElement element = driver.findElement(By.name("hidden"));
768
assertThat(element.getAttribute("name")).isEqualTo("hidden");
769
}
770
771
@Test
772
void testShouldNotBeAbleToFindAnElementOnABlankPage() {
773
driver.get("about:blank");
774
assertThatExceptionOfType(NoSuchElementException.class)
775
.isThrownBy(() -> driver.findElement(By.tagName("a")));
776
}
777
778
@NeedsFreshDriver
779
@Test
780
@Ignore(SAFARI)
781
public void testShouldNotBeAbleToLocateASingleElementOnABlankPage() {
782
// Note we're on the default start page for the browser at this point.
783
assertThatExceptionOfType(NoSuchElementException.class)
784
.isThrownBy(() -> driver.findElement(By.id("nonExistentButton")));
785
}
786
787
@SwitchToTopAfterTest
788
@Test
789
public void testAnElementFoundInADifferentFrameIsNotFound() {
790
driver.get(pages.missedJsReferencePage);
791
driver.switchTo().frame("inner");
792
WebElement element = driver.findElement(By.id("oneline"));
793
driver.switchTo().defaultContent();
794
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(element::getText);
795
}
796
}
797
798