Path: blob/trunk/java/test/org/openqa/selenium/ByTest.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.assertj.core.api.Assertions.assertThatNoException;22import static org.mockito.Mockito.mock;23import static org.mockito.Mockito.verify;24import static org.mockito.Mockito.verifyNoMoreInteractions;25import static org.openqa.selenium.By.ByClassName;26import static org.openqa.selenium.By.ByCssSelector;27import static org.openqa.selenium.By.ById;28import static org.openqa.selenium.By.ByLinkText;29import static org.openqa.selenium.By.ByName;30import static org.openqa.selenium.By.ByPartialLinkText;31import static org.openqa.selenium.By.ByTagName;32import static org.openqa.selenium.By.ByXPath;33import static org.openqa.selenium.json.Json.MAP_TYPE;3435import java.util.List;36import java.util.Map;37import org.junit.jupiter.api.Tag;38import org.junit.jupiter.api.Test;39import org.openqa.selenium.json.Json;4041@Tag("UnitTests")42class ByTest {4344@Test45void shouldUseFindsByNameToLocateElementsByName() {46final SearchContext driver = mock(SearchContext.class);4748By.cssSelector("cheese").findElement(driver);49By.cssSelector("peas").findElements(driver);5051verify(driver).findElement(By.cssSelector("cheese"));52verify(driver).findElements(By.cssSelector("peas"));53verifyNoMoreInteractions(driver);54}5556@Test57void shouldUseXpathLocateElementsByXpath() {58SearchContext driver = mock(SearchContext.class);5960By.xpath(".//*[@name = 'cheese']").findElement(driver);61By.xpath(".//*[@name = 'peas']").findElements(driver);6263verify(driver).findElement(By.xpath(".//*[@name = 'cheese']"));64verify(driver).findElements(By.xpath(".//*[@name = 'peas']"));65verifyNoMoreInteractions(driver);66}6768@Test69void searchesByTagNameIfSupported() {70SearchContext context = mock(SearchContext.class);7172By.tagName("foo").findElement(context);73By.tagName("bar").findElements(context);7475verify(context).findElement(By.tagName("foo"));76verify(context).findElements(By.tagName("bar"));77verifyNoMoreInteractions(context);78}7980@Test81void innerClassesArePublicSoThatTheyCanBeReusedElsewhere() {82assertThat(new ByXPath("a")).hasToString("By.xpath: a");83assertThat(new ById("a")).hasToString("By.id: a");84assertThat(new ByClassName("a")).hasToString("By.className: a");85assertThat(new ByLinkText("a")).hasToString("By.linkText: a");86assertThat(new ByName("a")).hasToString("By.name: a");87assertThat(new ByTagName("a")).hasToString("By.tagName: a");88assertThat(new ByCssSelector("a")).hasToString("By.cssSelector: a");89assertThat(new ByPartialLinkText("a")).hasToString("By.partialLinkText: a");90}9192// See https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/291793@Test94void testHashCodeDoesNotFallIntoEndlessRecursion() {95By locator =96new By() {97@Override98public List<WebElement> findElements(SearchContext context) {99return null;100}101};102assertThatNoException().isThrownBy(locator::hashCode);103}104105@Test106void ensureMultipleClassNamesAreNotAccepted() {107assertThatExceptionOfType(InvalidSelectorException.class)108.isThrownBy(() -> By.className("one two"));109}110111@Test112void ensureIdIsSerializedProperly() {113// Although it's not legal, make sure we handle the case where people use spaces.114By by = By.id("one two");115116Json json = new Json();117Map<String, Object> blob = json.toType(json.toJson(by), MAP_TYPE);118119assertThat(blob)120.hasSize(2)121.containsEntry("using", "css selector")122.containsEntry("value", "#one\\ two");123}124}125126127