Skip to content

Commit

Permalink
Allow the remote server to handle relative locators
Browse files Browse the repository at this point in the history
This avoids the need to send a large amount of JS across the wire, and
should make using the relative locators with the Selenium server more
efficient.
  • Loading branch information
shs96c committed Sep 27, 2021
1 parent 2835393 commit 6449881
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 21 deletions.
3 changes: 2 additions & 1 deletion java/src/org/openqa/selenium/support/locators/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ java_library(
"//java/test/org/openqa/selenium/support/locators:__pkg__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java:auto-service",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Map;

import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.support.locators.RelativeLocatorScript.FIND_ELEMENTS;

/**
* Used for finding elements by their location on a page, rather than their
Expand Down Expand Up @@ -71,23 +72,6 @@
public class RelativeLocator {

private static final Json JSON = new Json();
private static final String FIND_ELEMENTS;

static {
try {
String location = String.format(
"/%s/%s",
RelativeLocator.class.getPackage().getName().replace(".", "/"),
"findElements.js");

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);
}
}

private static final int CLOSE_IN_PIXELS = 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.support.locators;

import com.google.common.io.Resources;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

class RelativeLocatorScript {

static final String FIND_ELEMENTS;

static {
try {
String location = String.format(
"/%s/%s",
RelativeLocator.class.getPackage().getName().replace(".", "/"),
"findElements.js");

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);
}
}

private RelativeLocatorScript() {
// Utility class.
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.support.locators;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.By;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.JsonToWebElementConverter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.locators.CustomLocator;

import java.util.List;

import static org.openqa.selenium.support.locators.RelativeLocatorScript.FIND_ELEMENTS;

@AutoService(CustomLocator.class)
public class RelativeLocatorServerSide implements CustomLocator {
@Override
public String getLocatorName() {
return "relative";
}

@Override
public By createBy(Object usingParameter) {
Require.nonNull("Using", usingParameter);
return new RemoteRelative(usingParameter);
}

private static class RemoteRelative extends By {
private final Object using;

private RemoteRelative(Object usingParameter) {
using = usingParameter;
}

@Override
public List<WebElement> findElements(SearchContext context) {
JavascriptExecutor js = getJavascriptExecutor(context);

WebDriver driver = getWebDriver(context);

if (driver instanceof RemoteWebDriver) {
Object converted = new JsonToWebElementConverter((RemoteWebDriver) driver).apply(using);

@SuppressWarnings("unchecked")
List<WebElement> elements = (List<WebElement>) js.executeScript(FIND_ELEMENTS, ImmutableMap.of("relative", converted));
return elements;
}

throw new InvalidArgumentException("Unable to find element");
}
}
}
6 changes: 3 additions & 3 deletions java/test/org/openqa/selenium/chrome/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ java_selenium_test_suite(
artifact("org.mockito:mockito-core"),
],
javacopts = [
"--release",
"11",
],
"--release",
"11",
],
)
3 changes: 3 additions & 0 deletions java/test/org/openqa/selenium/support/locators/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ java_selenium_test_suite(
"--release",
"11",
],
tags = [
"selenium-remote",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/remote",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.environment.webserver.Page;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.testing.JUnit4TestBase;

import java.util.List;
Expand Down

0 comments on commit 6449881

Please sign in to comment.