Path: blob/trunk/java/test/org/openqa/selenium/ElementFindingTest.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.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.testing.drivers.Browser.CHROME;22import static org.openqa.selenium.testing.drivers.Browser.EDGE;23import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;24import static org.openqa.selenium.testing.drivers.Browser.IE;25import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2627import java.util.List;28import org.junit.jupiter.api.Test;29import org.openqa.selenium.testing.Ignore;30import org.openqa.selenium.testing.JupiterTestBase;31import org.openqa.selenium.testing.NeedsFreshDriver;32import org.openqa.selenium.testing.NotYetImplemented;33import org.openqa.selenium.testing.SwitchToTopAfterTest;3435class ElementFindingTest extends JupiterTestBase {3637// By.id positive3839@Test40void testShouldBeAbleToFindASingleElementById() {41driver.get(pages.xhtmlTestPage);42WebElement element = driver.findElement(By.id("linkId"));43assertThat(element.getAttribute("id")).isEqualTo("linkId");44}4546@Test47void testShouldBeAbleToFindASingleElementByNumericId() {48driver.get(pages.nestedPage);49WebElement element = driver.findElement(By.id("2"));50assertThat(element.getAttribute("id")).isEqualTo("2");51}5253@Test54void testShouldBeAbleToFindASingleElementByIdWithNonAlphanumericCharacters() {55driver.get(pages.nestedPage);56WebElement element = driver.findElement(By.id("white space"));57assertThat(element.getText()).isEqualTo("space");58WebElement element2 = driver.findElement(By.id("css#.chars"));59assertThat(element2.getText()).isEqualTo("css escapes");60}6162@Test63void testShouldBeAbleToFindMultipleElementsById() {64driver.get(pages.nestedPage);65List<WebElement> elements = driver.findElements(By.id("test_id"));66assertThat(elements).hasSize(2);67}6869@Test70void testShouldBeAbleToFindMultipleElementsByNumericId() {71driver.get(pages.nestedPage);72List<WebElement> elements = driver.findElements(By.id("2"));73assertThat(elements).hasSize(8);74}7576@Test77void testShouldBeAbleToFindMultipleElementsByIdWithNonAlphanumericCharacters() {78driver.get(pages.nestedPage);79List<WebElement> elements = driver.findElements(By.id("white space"));80assertThat(elements).hasSize(2);81List<WebElement> elements2 = driver.findElements(By.id("css#.chars"));82assertThat(elements2).hasSize(2);83}8485// By.id negative8687@Test88void testShouldNotBeAbleToLocateByIdASingleElementThatDoesNotExist() {89driver.get(pages.formPage);90assertThatExceptionOfType(NoSuchElementException.class)91.isThrownBy(() -> driver.findElement(By.id("nonExistentButton")));92}9394@Test95void testShouldNotBeAbleToLocateByIdMultipleElementsThatDoNotExist() {96driver.get(pages.formPage);97List<WebElement> elements = driver.findElements(By.id("nonExistentButton"));98assertThat(elements.size()).isZero();99}100101@Test102@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")103@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")104void testFindingASingleElementByEmptyIdShouldThrow() {105driver.get(pages.formPage);106assertThatExceptionOfType(InvalidSelectorException.class)107.isThrownBy(() -> driver.findElement(By.id("")));108}109110@Test111@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")112@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")113public void testFindingMultipleElementsByEmptyIdShouldThrow() {114driver.get(pages.formPage);115assertThatExceptionOfType(InvalidSelectorException.class)116.isThrownBy(() -> driver.findElements(By.id("")));117}118119@Test120void testFindingASingleElementByIdWithSpaceShouldThrow() {121driver.get(pages.formPage);122assertThatExceptionOfType(NoSuchElementException.class)123.isThrownBy(() -> driver.findElement(By.id("nonexistent button")));124}125126@Test127void testFindingMultipleElementsByIdWithSpaceShouldReturnEmptyList() {128driver.get(pages.formPage);129List<WebElement> elements = driver.findElements(By.id("nonexistent button"));130assertThat(elements.size()).isZero();131}132133// By.name positive134135@Test136void testShouldBeAbleToFindASingleElementByName() {137driver.get(pages.formPage);138WebElement element = driver.findElement(By.name("checky"));139assertThat(element.getAttribute("value")).isEqualTo("furrfu");140}141142@Test143void testShouldBeAbleToFindMultipleElementsByName() {144driver.get(pages.nestedPage);145List<WebElement> elements = driver.findElements(By.name("checky"));146assertThat(elements.size()).isGreaterThan(1);147}148149@Test150void testShouldBeAbleToFindAnElementThatDoesNotSupportTheNameProperty() {151driver.get(pages.nestedPage);152WebElement element = driver.findElement(By.name("div1"));153assertThat(element.getAttribute("name")).isEqualTo("div1");154}155156// By.name negative157158@Test159void testShouldNotBeAbleToLocateByNameASingleElementThatDoesNotExist() {160driver.get(pages.formPage);161assertThatExceptionOfType(NoSuchElementException.class)162.isThrownBy(() -> driver.findElement(By.name("nonExistentButton")));163}164165@Test166void testShouldNotBeAbleToLocateByNameMultipleElementsThatDoNotExist() {167driver.get(pages.formPage);168List<WebElement> elements = driver.findElements(By.name("nonExistentButton"));169assertThat(elements).isEmpty();170}171172@Test173void testFindingASingleElementByEmptyNameShouldThrow() {174driver.get(pages.formPage);175assertThatExceptionOfType(NoSuchElementException.class)176.isThrownBy(() -> driver.findElement(By.name("")));177}178179@Test180void testFindingMultipleElementsByEmptyNameShouldReturnEmptyList() {181driver.get(pages.formPage);182List<WebElement> elements = driver.findElements(By.name(""));183assertThat(elements).isEmpty();184}185186@Test187void testFindingASingleElementByNameWithSpaceShouldThrow() {188driver.get(pages.formPage);189assertThatExceptionOfType(NoSuchElementException.class)190.isThrownBy(() -> driver.findElement(By.name("nonexistent button")));191}192193@Test194void testFindingMultipleElementsByNameWithSpaceShouldReturnEmptyList() {195driver.get(pages.formPage);196List<WebElement> elements = driver.findElements(By.name("nonexistent button"));197assertThat(elements).isEmpty();198}199200// By.tagName positive201202@Test203void testShouldBeAbleToFindASingleElementByTagName() {204driver.get(pages.formPage);205WebElement element = driver.findElement(By.tagName("input"));206assertThat(element.getTagName().toLowerCase()).isEqualTo("input");207}208209@Test210void testShouldBeAbleToFindMultipleElementsByTagName() {211driver.get(pages.formPage);212List<WebElement> elements = driver.findElements(By.tagName("input"));213assertThat(elements.size()).isGreaterThan(1);214}215216// By.tagName negative217218@Test219void testShouldNotBeAbleToLocateByTagNameASingleElementThatDoesNotExist() {220driver.get(pages.formPage);221assertThatExceptionOfType(NoSuchElementException.class)222.isThrownBy(() -> driver.findElement(By.tagName("nonExistentButton")));223}224225@Test226void testShouldNotBeAbleToLocateByTagNameMultipleElementsThatDoNotExist() {227driver.get(pages.formPage);228List<WebElement> elements = driver.findElements(By.tagName("nonExistentButton"));229assertThat(elements).isEmpty();230}231232@Test233void testFindingASingleElementByEmptyTagNameShouldThrow() {234driver.get(pages.formPage);235assertThatExceptionOfType(InvalidSelectorException.class)236.isThrownBy(() -> driver.findElement(By.tagName("")));237}238239@Test240void testFindingMultipleElementsByEmptyTagNameShouldThrow() {241driver.get(pages.formPage);242assertThatExceptionOfType(InvalidSelectorException.class)243.isThrownBy(() -> driver.findElements(By.tagName("")));244}245246@Test247void testFindingASingleElementByTagNameWithSpaceShouldThrow() {248driver.get(pages.formPage);249assertThatExceptionOfType(NoSuchElementException.class)250.isThrownBy(() -> driver.findElement(By.tagName("nonexistent button")));251}252253@Test254void testFindingMultipleElementsByTagNameWithSpaceShouldReturnEmptyList() {255driver.get(pages.formPage);256List<WebElement> elements = driver.findElements(By.tagName("nonexistent button"));257assertThat(elements).isEmpty();258}259260// By.className positive261262@Test263void testShouldBeAbleToFindASingleElementByClass() {264driver.get(pages.xhtmlTestPage);265WebElement element = driver.findElement(By.className("extraDiv"));266assertThat(element.getText()).startsWith("Another div starts here.");267}268269@Test270void testShouldBeAbleToFindMultipleElementsByClassName() {271driver.get(pages.xhtmlTestPage);272List<WebElement> elements = driver.findElements(By.className("nameC"));273assertThat(elements.size()).isGreaterThan(1);274}275276@Test277void testShouldFindElementByClassWhenItIsTheFirstNameAmongMany() {278driver.get(pages.xhtmlTestPage);279WebElement element = driver.findElement(By.className("nameA"));280assertThat(element.getText()).isEqualTo("An H2 title");281}282283@Test284void testShouldFindElementByClassWhenItIsTheLastNameAmongMany() {285driver.get(pages.xhtmlTestPage);286WebElement element = driver.findElement(By.className("nameC"));287assertThat(element.getText()).isEqualTo("An H2 title");288}289290@Test291void testShouldFindElementByClassWhenItIsInTheMiddleAmongMany() {292driver.get(pages.xhtmlTestPage);293WebElement element = driver.findElement(By.className("nameBnoise"));294assertThat(element.getText()).isEqualTo("An H2 title");295}296297@Test298void testShouldFindElementByClassWhenItsNameIsSurroundedByWhitespace() {299driver.get(pages.xhtmlTestPage);300WebElement element = driver.findElement(By.className("spaceAround"));301assertThat(element.getText()).isEqualTo("Spaced out");302}303304@Test305void testShouldFindElementsByClassWhenItsNameIsSurroundedByWhitespace() {306driver.get(pages.xhtmlTestPage);307List<WebElement> elements = driver.findElements(By.className("spaceAround"));308assertThat(elements).hasSize(1);309assertThat(elements.get(0).getText()).isEqualTo("Spaced out");310}311312// By.className negative313314@Test315void testShouldNotFindElementByClassWhenTheNameQueriedIsShorterThanCandidateName() {316driver.get(pages.xhtmlTestPage);317assertThatExceptionOfType(NoSuchElementException.class)318.isThrownBy(() -> driver.findElement(By.className("nameB")));319}320321@Test322@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")323@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")324void testFindingASingleElementByEmptyClassNameShouldThrow() {325driver.get(pages.xhtmlTestPage);326assertThatExceptionOfType(InvalidSelectorException.class)327.isThrownBy(() -> driver.findElement(By.className("")));328}329330@Test331@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")332@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")333void testFindingMultipleElementsByEmptyClassNameShouldThrow() {334driver.get(pages.xhtmlTestPage);335assertThatExceptionOfType(InvalidSelectorException.class)336.isThrownBy(() -> driver.findElements(By.className("")));337}338339@Test340void testFindingASingleElementByCompoundClassNameShouldThrow() {341driver.get(pages.xhtmlTestPage);342assertThatExceptionOfType(InvalidSelectorException.class)343.isThrownBy(() -> driver.findElement(By.className("a b")));344}345346@Test347void testFindingMultipleElementsByCompoundClassNameShouldThrow() {348driver.get(pages.xhtmlTestPage);349assertThatExceptionOfType(InvalidSelectorException.class)350.isThrownBy(() -> driver.findElements(By.className("a b")));351}352353@Test354public void testShouldBeAbleToFindASingleElementByAWeirdLookingClassName() {355driver.get(pages.xhtmlTestPage);356WebElement element = driver.findElement(By.className("cls-!@#$%^&*"));357assertThat(element.getAttribute("class")).isEqualTo("cls-!@#$%^&*");358}359360@Test361public void testShouldBeAbleToFindMultipleElementsByAWeirdLookingClassName() {362driver.get(pages.xhtmlTestPage);363List<WebElement> elements = driver.findElements(By.className("cls-!@#$%^&*"));364assertThat(elements).hasSize(1);365assertThat(elements.get(0).getAttribute("class")).isEqualTo("cls-!@#$%^&*");366}367368// By.xpath positive369370@Test371void testShouldBeAbleToFindASingleElementByXPath() {372driver.get(pages.xhtmlTestPage);373WebElement element = driver.findElement(By.xpath("//h1"));374assertThat(element.getText()).isEqualTo("XHTML Might Be The Future");375}376377@Test378void testShouldBeAbleToFindMultipleElementsByXPath() {379driver.get(pages.xhtmlTestPage);380List<WebElement> elements = driver.findElements(By.xpath("//div"));381assertThat(elements).hasSize(13);382}383384@Test385void testShouldBeAbleToFindManyElementsRepeatedlyByXPath() {386driver.get(pages.xhtmlTestPage);387String xpathString = "//node()[contains(@id,'id')]";388assertThat(driver.findElements(By.xpath(xpathString))).hasSize(3);389390xpathString = "//node()[contains(@id,'nope')]";391assertThat(driver.findElements(By.xpath(xpathString))).isEmpty();392}393394@Test395void testShouldBeAbleToIdentifyElementsByClass() {396driver.get(pages.xhtmlTestPage);397WebElement header = driver.findElement(By.xpath("//h1[@class='header']"));398assertThat(header.getText()).isEqualTo("XHTML Might Be The Future");399}400401@Test402void testShouldBeAbleToFindAnElementByXPathWithMultipleAttributes() {403driver.get(pages.formPage);404WebElement element =405driver.findElement(406By.xpath("//form[@name='optional']/input[@type='submit' and @value='Click!']"));407assertThat(element.getTagName()).isEqualToIgnoringCase("input");408assertThat(element.getAttribute("value")).isEqualTo("Click!");409}410411@Test412void testFindingALinkByXpathShouldLocateAnElementWithTheGivenText() {413driver.get(pages.xhtmlTestPage);414WebElement element = driver.findElement(By.xpath("//a[text()='click me']"));415assertThat(element.getText()).isEqualTo("click me");416}417418@Test419void testFindingALinkByXpathUsingContainsKeywordShouldWork() {420driver.get(pages.nestedPage);421WebElement element = driver.findElement(By.xpath("//a[contains(.,'hello world')]"));422assertThat(element.getText()).contains("hello world");423}424425@Test426@Ignore(IE)427@NotYetImplemented(FIREFOX)428@NotYetImplemented(SAFARI)429public void testShouldBeAbleToFindElementByXPathWithNamespace() {430driver.get(pages.svgPage);431WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));432assertThat(element.getText()).isEqualTo("Test Chart");433}434435// By.xpath negative436437@Test438void testShouldThrowAnExceptionWhenThereIsNoLinkToClick() {439driver.get(pages.xhtmlTestPage);440assertThatExceptionOfType(NoSuchElementException.class)441.isThrownBy(() -> driver.findElement(By.xpath("//a[@id='Not here']")));442}443444@Test445@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")446@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")447void testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInDriverFindElement() {448driver.get(pages.formPage);449assertThatExceptionOfType(InvalidSelectorException.class)450.isThrownBy(() -> driver.findElement(By.xpath("this][isnot][valid")));451}452453@Test454@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")455@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")456void457testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInDriverFindElements() {458driver.get(pages.formPage);459assertThatExceptionOfType(InvalidSelectorException.class)460.isThrownBy(() -> driver.findElements(By.xpath("this][isnot][valid")));461}462463@Test464@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")465@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")466void467testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInElementFindElement() {468driver.get(pages.formPage);469WebElement body = driver.findElement(By.tagName("body"));470assertThatExceptionOfType(InvalidSelectorException.class)471.isThrownBy(() -> body.findElement(By.xpath("this][isnot][valid")));472}473474@Test475@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")476@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")477void478testShouldThrowInvalidSelectorExceptionWhenXPathIsSyntacticallyInvalidInElementFindElements() {479driver.get(pages.formPage);480WebElement body = driver.findElement(By.tagName("body"));481assertThatExceptionOfType(InvalidSelectorException.class)482.isThrownBy(() -> body.findElements(By.xpath("this][isnot][valid")));483}484485@Test486@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")487@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")488void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInDriverFindElement() {489driver.get(pages.formPage);490assertThatExceptionOfType(InvalidSelectorException.class)491.isThrownBy(() -> driver.findElement(By.xpath("count(//input)")));492}493494@Test495@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")496@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")497void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInDriverFindElements() {498driver.get(pages.formPage);499assertThatExceptionOfType(InvalidSelectorException.class)500.isThrownBy(() -> driver.findElements(By.xpath("count(//input)")));501}502503@Test504@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")505@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")506void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInElementFindElement() {507driver.get(pages.formPage);508509WebElement body = driver.findElement(By.tagName("body"));510assertThatExceptionOfType(InvalidSelectorException.class)511.isThrownBy(() -> body.findElement(By.xpath("count(//input)")));512}513514@Test515@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")516@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")517void testShouldThrowInvalidSelectorExceptionWhenXPathReturnsWrongTypeInElementFindElements() {518driver.get(pages.formPage);519WebElement body = driver.findElement(By.tagName("body"));520assertThatExceptionOfType(InvalidSelectorException.class)521.isThrownBy(() -> body.findElements(By.xpath("count(//input)")));522}523524// By.cssSelector positive525526@Test527void testShouldBeAbleToFindASingleElementByCssSelector() {528driver.get(pages.xhtmlTestPage);529WebElement element = driver.findElement(By.cssSelector("div.content"));530assertThat(element.getTagName()).isEqualToIgnoringCase("div");531assertThat(element.getAttribute("class")).isEqualTo("content");532}533534@Test535void testShouldBeAbleToFindMultipleElementsByCssSelector() {536driver.get(pages.xhtmlTestPage);537List<WebElement> elements = driver.findElements(By.cssSelector("p"));538assertThat(elements.size()).isGreaterThan(1);539}540541@Test542void testShouldBeAbleToFindASingleElementByCompoundCssSelector() {543driver.get(pages.xhtmlTestPage);544WebElement element = driver.findElement(By.cssSelector("div.extraDiv, div.content"));545assertThat(element.getTagName()).isEqualToIgnoringCase("div");546assertThat(element.getAttribute("class")).isEqualTo("content");547}548549@Test550void testShouldBeAbleToFindMultipleElementsByCompoundCssSelector() {551driver.get(pages.xhtmlTestPage);552List<WebElement> elements = driver.findElements(By.cssSelector("div.extraDiv, div.content"));553assertThat(elements.size()).isGreaterThan(1);554assertThat(elements.get(0).getAttribute("class")).isEqualTo("content");555assertThat(elements.get(1).getAttribute("class")).isEqualTo("extraDiv");556}557558@Test559void testShouldBeAbleToFindAnElementByBooleanAttributeUsingCssSelector() {560driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected.html"));561WebElement element = driver.findElement(By.cssSelector("option[selected='selected']"));562assertThat(element.getAttribute("value")).isEqualTo("two");563}564565@Test566void testShouldBeAbleToFindAnElementByBooleanAttributeUsingShortCssSelector() {567driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected.html"));568WebElement element = driver.findElement(By.cssSelector("option[selected]"));569assertThat(element.getAttribute("value")).isEqualTo("two");570}571572@Test573void testShouldBeAbleToFindAnElementByBooleanAttributeUsingShortCssSelectorOnHtml4Page() {574driver.get(appServer.whereIs("locators_tests/boolean_attribute_selected_html4.html"));575WebElement element = driver.findElement(By.cssSelector("option[selected]"));576assertThat(element.getAttribute("value")).isEqualTo("two");577}578579// By.cssSelector negative580581@Test582void testShouldNotFindElementByCssSelectorWhenThereIsNoSuchElement() {583driver.get(pages.xhtmlTestPage);584assertThatExceptionOfType(NoSuchElementException.class)585.isThrownBy(() -> driver.findElement(By.cssSelector(".there-is-no-such-class")));586}587588@Test589void testShouldNotFindElementsByCssSelectorWhenThereIsNoSuchElement() {590driver.get(pages.xhtmlTestPage);591List<WebElement> elements = driver.findElements(By.cssSelector(".there-is-no-such-class"));592assertThat(elements).isEmpty();593}594595@Test596@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")597@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")598void testFindingASingleElementByEmptyCssSelectorShouldThrow() {599driver.get(pages.xhtmlTestPage);600assertThatExceptionOfType(InvalidSelectorException.class)601.isThrownBy(() -> driver.findElement(By.cssSelector("")));602}603604@Test605@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")606@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")607void testFindingMultipleElementsByEmptyCssSelectorShouldThrow() {608driver.get(pages.xhtmlTestPage);609assertThatExceptionOfType(InvalidSelectorException.class)610.isThrownBy(() -> driver.findElements(By.cssSelector("")));611}612613@Test614@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")615@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")616void testFindingASingleElementByInvalidCssSelectorShouldThrow() {617driver.get(pages.xhtmlTestPage);618assertThatExceptionOfType(InvalidSelectorException.class)619.isThrownBy(() -> driver.findElement(By.cssSelector("//a/b/c[@id='1']")));620}621622@Test623@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")624@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")625void testFindingMultipleElementsByInvalidCssSelectorShouldThrow() {626driver.get(pages.xhtmlTestPage);627assertThatExceptionOfType(InvalidSelectorException.class)628.isThrownBy(() -> driver.findElements(By.cssSelector("//a/b/c[@id='1']")));629}630631// By.linkText positive632633@Test634void testShouldBeAbleToFindALinkByText() {635driver.get(pages.xhtmlTestPage);636WebElement link = driver.findElement(By.linkText("click me"));637assertThat(link.getText()).isEqualTo("click me");638}639640@Test641void testShouldBeAbleToFindMultipleLinksByText() {642driver.get(pages.xhtmlTestPage);643List<WebElement> elements = driver.findElements(By.linkText("click me"));644assertThat(elements).hasSize(2);645}646647@Test648void testShouldFindElementByLinkTextContainingEqualsSign() {649driver.get(pages.xhtmlTestPage);650WebElement element = driver.findElement(By.linkText("Link=equalssign"));651assertThat(element.getAttribute("id")).isEqualTo("linkWithEqualsSign");652}653654@Test655void testShouldFindMultipleElementsByLinkTextContainingEqualsSign() {656driver.get(pages.xhtmlTestPage);657List<WebElement> elements = driver.findElements(By.linkText("Link=equalssign"));658assertThat(elements).hasSize(1);659assertThat(elements.get(0).getAttribute("id")).isEqualTo("linkWithEqualsSign");660}661662@Test663void findsByLinkTextOnXhtmlPage() {664driver.get(appServer.whereIs("actualXhtmlPage.xhtml"));665String linkText = "Foo";666WebElement element = driver.findElement(By.linkText(linkText));667assertThat(element.getText()).isEqualTo(linkText);668}669670@Test671void testLinkWithFormattingTags() {672driver.get(pages.simpleTestPage);673WebElement elem = driver.findElement(By.id("links"));674675WebElement res = elem.findElement(By.partialLinkText("link with formatting tags"));676assertThat(res.getText()).isEqualTo("link with formatting tags");677}678679@Test680@NotYetImplemented(SAFARI)681public void testDriverCanGetLinkByLinkTestIgnoringTrailingWhitespace() {682driver.get(pages.simpleTestPage);683WebElement link = driver.findElement(By.linkText("link with trailing space"));684assertThat(link.getAttribute("id")).isEqualTo("linkWithTrailingSpace");685assertThat(link.getText()).isEqualTo("link with trailing space");686}687688// By.linkText negative689690@Test691void testShouldNotBeAbleToLocateByLinkTextASingleElementThatDoesNotExist() {692driver.get(pages.xhtmlTestPage);693assertThatExceptionOfType(NoSuchElementException.class)694.isThrownBy(() -> driver.findElement(By.linkText("Not here either")));695}696697@Test698void testShouldNotBeAbleToLocateByLinkTextMultipleElementsThatDoNotExist() {699driver.get(pages.xhtmlTestPage);700List<WebElement> elements = driver.findElements(By.linkText("Not here either"));701assertThat(elements.size()).isZero();702}703704// By.partialLinkText positive705706@Test707void testShouldBeAbleToFindMultipleElementsByPartialLinkText() {708driver.get(pages.xhtmlTestPage);709List<WebElement> elements = driver.findElements(By.partialLinkText("ick me"));710assertThat(elements.size()).isEqualTo(2);711}712713@Test714void testShouldBeAbleToFindASingleElementByPartialLinkText() {715driver.get(pages.xhtmlTestPage);716WebElement element = driver.findElement(By.partialLinkText("anon"));717assertThat(element.getText()).contains("anon");718}719720@Test721void testShouldFindElementByPartialLinkTextContainingEqualsSign() {722driver.get(pages.xhtmlTestPage);723WebElement element = driver.findElement(By.partialLinkText("Link="));724assertThat(element.getAttribute("id")).isEqualTo("linkWithEqualsSign");725}726727@Test728void testShouldFindMultipleElementsByPartialLinkTextContainingEqualsSign() {729driver.get(pages.xhtmlTestPage);730List<WebElement> elements = driver.findElements(By.partialLinkText("Link="));731assertThat(elements).hasSize(1);732assertThat(elements.get(0).getAttribute("id")).isEqualTo("linkWithEqualsSign");733}734735// Misc tests736737@Test738void testDriverShouldBeAbleToFindElementsAfterLoadingMoreThanOnePageAtATime() {739driver.get(pages.formPage);740driver.get(pages.xhtmlTestPage);741WebElement link = driver.findElement(By.linkText("click me"));742assertThat(link.getText()).isEqualTo("click me");743}744745// You don't want to ask why this is here746@Test747void testWhenFindingByNameShouldNotReturnById() {748driver.get(pages.formPage);749750WebElement element = driver.findElement(By.name("id-name1"));751assertThat(element.getAttribute("value")).isEqualTo("name");752753element = driver.findElement(By.id("id-name1"));754assertThat(element.getAttribute("value")).isEqualTo("id");755756element = driver.findElement(By.name("id-name2"));757assertThat(element.getAttribute("value")).isEqualTo("name");758759element = driver.findElement(By.id("id-name2"));760assertThat(element.getAttribute("value")).isEqualTo("id");761}762763@Test764void testShouldBeAbleToFindAHiddenElementsByName() {765driver.get(pages.formPage);766WebElement element = driver.findElement(By.name("hidden"));767assertThat(element.getAttribute("name")).isEqualTo("hidden");768}769770@Test771void testShouldNotBeAbleToFindAnElementOnABlankPage() {772driver.get("about:blank");773assertThatExceptionOfType(NoSuchElementException.class)774.isThrownBy(() -> driver.findElement(By.tagName("a")));775}776777@NeedsFreshDriver778@Test779@Ignore(SAFARI)780public void testShouldNotBeAbleToLocateASingleElementOnABlankPage() {781// Note we're on the default start page for the browser at this point.782assertThatExceptionOfType(NoSuchElementException.class)783.isThrownBy(() -> driver.findElement(By.id("nonExistentButton")));784}785786@SwitchToTopAfterTest787@Test788public void testAnElementFoundInADifferentFrameIsNotFound() {789driver.get(pages.missedJsReferencePage);790driver.switchTo().frame("inner");791WebElement element = driver.findElement(By.id("oneline"));792driver.switchTo().defaultContent();793assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(element::getText);794}795}796797798