Skip to content

Commit

Permalink
Rename "friendly" locators to "relative"
Browse files Browse the repository at this point in the history
Because the feedback I've had is that this is a better name.
  • Loading branch information
shs96c committed Sep 9, 2019
1 parent 1d45069 commit 221ec6a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

<html>
<head>
<title>Friendly Locators</title>
<title>Relative Locators</title>
<style>
table {
text-align: center;
Expand All @@ -12,7 +13,7 @@
</style>
</head>
<body>
<h1>Friendly Locator Tests</h1>
<h1>Relative Locator Tests</h1>
<p id="above">This text is above.
<p id="mid">This is a paragraph of text in the middle.
<p id="below">This text is below.
Expand Down
2 changes: 1 addition & 1 deletion java/client/src/org/openqa/selenium/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ java_export(
":page-factory",
"//java/client/src/org/openqa/selenium/support/devtools",
"//java/client/src/org/openqa/selenium/support/events",
"//java/client/src/org/openqa/selenium/support/friendly",
"//java/client/src/org/openqa/selenium/support/locators",
"//java/client/src/org/openqa/selenium/support/ui:clock",
"//java/client/src/org/openqa/selenium/support/ui:components",
"//java/client/src/org/openqa/selenium/support/ui:elements",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
java_library(
name = "friendly",
name = "locators",
srcs = glob(["*.java"]),
resources = [
":find-elements",
Expand All @@ -11,7 +11,7 @@ java_library(
],
visibility = [
"//java/client/src/org/openqa/selenium/support:__pkg__",
"//java/client/test/org/openqa/selenium/support/friendly:__pkg__",
"//java/client/test/org/openqa/selenium/support/locators:__pkg__",
]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openqa.selenium.support.friendly;
package org.openqa.selenium.support.locators;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand All @@ -23,40 +23,40 @@
import static com.google.common.base.Preconditions.checkArgument;
import static org.openqa.selenium.json.Json.MAP_TYPE;

public class ByFriendlyLocator {
public class RelativeLocator {

private static final Json JSON = new Json();
private static final String FIND_ELEMENTS;
static {
try {
String location = String.format(
"/%s/%s",
ByFriendlyLocator.class.getPackage().getName().replace(".", "/"),
RelativeLocator.class.getPackage().getName().replace(".", "/"),
"findElements.js");

URL url = ByFriendlyLocator.class.getResource(location);
URL url = RelativeLocator.class.getResource(location);

String rawFunction = Resources.toString(url, StandardCharsets.UTF_8);
FIND_ELEMENTS = String.format("return (%s).apply(null, arguments);", rawFunction);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static FriendlyBy withTagName(String tagName) {
public static RelativeBy withTagName(String tagName) {
Objects.requireNonNull(tagName, "Tag name to look for must be set");

return new FriendlyBy(By.tagName(tagName));
return new RelativeBy(By.tagName(tagName));
}

public static class FriendlyBy extends By {
public static class RelativeBy extends By {
private final Object root;
private final List<Map<String, Object>> filters;

private FriendlyBy(Object rootLocator) {
private RelativeBy(Object rootLocator) {
this(rootLocator, ImmutableList.of());
}

private FriendlyBy(Object rootLocator, List<Map<String, Object>> filters) {
private RelativeBy(Object rootLocator, List<Map<String, Object>> filters) {
if (rootLocator instanceof By) {
assertLocatorCanBeSerialized(rootLocator);
rootLocator = asAtomLocatorParameter((By) rootLocator);
Expand All @@ -73,79 +73,79 @@ private FriendlyBy(Object rootLocator, List<Map<String, Object>> filters) {
this.filters = ImmutableList.copyOf(Objects.requireNonNull(filters));
}

public FriendlyBy above(WebElement element) {
public RelativeBy above(WebElement element) {
Objects.requireNonNull(element, "Element to search for must be set.");
return simpleDirection("above", element);
}

public FriendlyBy above(By locator) {
public RelativeBy above(By locator) {
Objects.requireNonNull(locator, "Locator to use must be set.");
assertLocatorCanBeSerialized(locator);
return simpleDirection("above", locator);
}

public FriendlyBy below(WebElement element) {
public RelativeBy below(WebElement element) {
Objects.requireNonNull(element, "Element to search for must be set.");
return simpleDirection("below", element);
}

public FriendlyBy below(By locator) {
public RelativeBy below(By locator) {
Objects.requireNonNull(locator, "Locator to use must be set.");
assertLocatorCanBeSerialized(locator);
return simpleDirection("below", locator);
}

public FriendlyBy toLeftOf(WebElement element) {
public RelativeBy toLeftOf(WebElement element) {
Objects.requireNonNull(element, "Element to search for must be set.");
return simpleDirection("left", element);
}

public FriendlyBy toLeftOf(By locator) {
public RelativeBy toLeftOf(By locator) {
Objects.requireNonNull(locator, "Locator to use must be set.");
assertLocatorCanBeSerialized(locator);
return simpleDirection("left", locator);
}

public FriendlyBy toRightOf(WebElement element) {
public RelativeBy toRightOf(WebElement element) {
Objects.requireNonNull(element, "Element to search for must be set.");
return simpleDirection("right", element);
}

public FriendlyBy toRightOf(By locator) {
public RelativeBy toRightOf(By locator) {
Objects.requireNonNull(locator, "Locator to use must be set.");
assertLocatorCanBeSerialized(locator);
return simpleDirection("right", locator);
}

public FriendlyBy near(WebElement element) {
public RelativeBy near(WebElement element) {
Objects.requireNonNull(element, "Element to search for must be set.");
return near(element, 50);
}

public FriendlyBy near(WebElement element, int atMostDistanceInPixels) {
public RelativeBy near(WebElement element, int atMostDistanceInPixels) {
Objects.requireNonNull(element, "Element to search for must be set.");
checkArgument(atMostDistanceInPixels > 0, "Distance must be greater than 0.");

return near((Object) element, atMostDistanceInPixels);
}

public FriendlyBy near(By locator) {
public RelativeBy near(By locator) {
Objects.requireNonNull(locator, "Locator to use for must be set.");
return near((Object) locator, 50);
}

public FriendlyBy near(By locator, int atMostDistanceInPixels) {
public RelativeBy near(By locator, int atMostDistanceInPixels) {
Objects.requireNonNull(locator, "Locator to use for must be set.");
checkArgument(atMostDistanceInPixels > 0, "Distance must be greater than 0.");

return near((Object) locator, atMostDistanceInPixels);
}

private FriendlyBy near(Object locator, int atMostDistanceInPixels) {
private RelativeBy near(Object locator, int atMostDistanceInPixels) {
Objects.requireNonNull(locator, "Locator to use must be set.");
checkArgument(atMostDistanceInPixels > 0, "Distance must be greater than 0.");

return new FriendlyBy(
return new RelativeBy(
root,
amend(ImmutableMap.of(
"kind", "near",
Expand All @@ -162,11 +162,11 @@ public List<WebElement> findElements(SearchContext context) {
return elements;
}

private FriendlyBy simpleDirection(String direction, Object locator) {
private RelativeBy simpleDirection(String direction, Object locator) {
Objects.requireNonNull(direction, "Direction to search in must be set.");
Objects.requireNonNull(locator, "Locator to use must be set.");

return new FriendlyBy(
return new RelativeBy(
root,
amend(ImmutableMap.of(
"kind", direction,
Expand Down Expand Up @@ -200,7 +200,7 @@ private JavascriptExecutor extractJsExecutor(SearchContext context) {

private Map<String, Object> toJson() {
return ImmutableMap.of(
"friendly", ImmutableMap.of(
"relative", ImmutableMap.of(
"root", root,
"filters", filters));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ java_selenium_test_suite(
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/support/friendly",
"//java/client/src/org/openqa/selenium/support/locators",
"//java/client/test/org/openqa/selenium/testing:test-base",
"//third_party/java/assertj",
"//third_party/java/guava",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openqa.selenium.support.friendly;
package org.openqa.selenium.support.locators;

import com.google.common.collect.ImmutableList;
import org.junit.Test;
Expand All @@ -10,13 +10,13 @@
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.support.friendly.ByFriendlyLocator.withTagName;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

public class ByFriendlyLocatorTest extends JUnit4TestBase {
public class RelativeLocatorTest extends JUnit4TestBase {

@Test
public void shouldBeAbleToFindElementsAboveAnother() {
driver.get(appServer.whereIs("friendly_locators.html"));
driver.get(appServer.whereIs("relative_locators.html"));

WebElement lowest = driver.findElement(By.id("below"));

Expand All @@ -28,7 +28,7 @@ public void shouldBeAbleToFindElementsAboveAnother() {

@Test
public void shouldBeAbleToCombineFilters() {
driver.get(appServer.whereIs("friendly_locators.html"));
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(withTagName("td").above(By.id("center")).toRightOf(By.id("second")));

Expand Down
2 changes: 1 addition & 1 deletion javascript/atoms/fragments/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ closure_fragment(
"//javascript/atoms:locators",
],
visibility = [
"//java/client/src/org/openqa/selenium/support/friendly:__pkg__",
"//java/client/src/org/openqa/selenium/support/locators:__pkg__",
]
)

Expand Down
4 changes: 2 additions & 2 deletions javascript/atoms/locators/locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ goog.provide('bot.locators');
goog.require('bot');
goog.require('bot.locators.className');
goog.require('bot.locators.css');
goog.require('bot.locators.friendly');
goog.require('bot.locators.id');
goog.require('bot.locators.linkText');
goog.require('bot.locators.name');
goog.require('bot.locators.partialLinkText');
goog.require('bot.locators.relative');
goog.require('bot.locators.tagName');
goog.require('bot.locators.xpath');

Expand Down Expand Up @@ -59,7 +59,7 @@ bot.locators.STRATEGIES_ = {
'css': bot.locators.css,
'css selector': bot.locators.css,

'friendly': bot.locators.friendly,
'relative': bot.locators.relative,

'id': bot.locators.id,

Expand Down

0 comments on commit 221ec6a

Please sign in to comment.