Skip to content

Commit c507192

Browse files
committed
[grid] Improving fallback locators code
1 parent bd1110c commit c507192

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

java/server/src/org/openqa/selenium/grid/node/CustomLocatorHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
157157
.orElseThrow(() -> new IllegalArgumentException("Cannot locate session ID from " + req.getUri())));
158158

159159
SearchContext context = null;
160-
RemoteWebElement element = null;
160+
RemoteWebElement element;
161161
boolean findMultiple = false;
162162
UrlTemplate.Match match = FIND_ELEMENT.match(req.getUri());
163163
if (match != null) {
@@ -194,7 +194,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
194194
throw new IllegalStateException("Unable to determine locator context: " + req);
195195
}
196196

197-
Object toReturn = null;
197+
Object toReturn;
198198
By by = customLocator.apply(value);
199199
if (findMultiple) {
200200
toReturn = context.findElements(by);

java/server/src/org/openqa/selenium/grid/node/locators/FallbackLocators.java renamed to java/server/src/org/openqa/selenium/grid/node/locators/ById.java

+18-31
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,29 @@
2222
import com.google.auto.service.AutoService;
2323

2424
import org.openqa.selenium.By;
25+
import org.openqa.selenium.internal.Require;
2526
import org.openqa.selenium.remote.locators.CustomLocator;
2627

2728
import java.util.stream.Stream;
2829

29-
public class FallbackLocators {
30-
31-
@AutoService(CustomLocator.class)
32-
public static class ById extends CustomLocator {
33-
@Override
34-
public String getLocatorName() {
35-
return "id";
36-
}
37-
38-
@Override
39-
public By createBy(Object usingParameter) {
40-
String id = Stream.of(
41-
String.valueOf(usingParameter)
42-
.split("\\s+")).map(str -> "#" + str).collect(joining(" "));
43-
return By.cssSelector(id);
44-
}
45-
}
30+
/**
31+
* A class implementing {link @CustomLocator} and to be used as a fallback locator on the server
32+
* side.
33+
*/
4634

47-
@AutoService(CustomLocator.class)
48-
public static class ByName extends CustomLocator {
49-
@Override
50-
public String getLocatorName() {
51-
return "name";
52-
}
53-
54-
@Override
55-
public By createBy(Object usingParameter) {
56-
String name = String.valueOf(usingParameter).replace("'", "\\'");
57-
return By.cssSelector(String.format("*[name='%s']", name));
58-
}
35+
@AutoService(CustomLocator.class)
36+
public class ById extends CustomLocator {
37+
@Override
38+
public String getLocatorName() {
39+
return "id";
5940
}
6041

61-
62-
// asJson.put("value", String.format("*[name='%s']", name.replace("'", "\\'")));
42+
@Override
43+
public By createBy(Object usingParameter) {
44+
Require.argument("Locator value", usingParameter).instanceOf(String.class);
45+
String id = Stream.of(
46+
String.valueOf(usingParameter)
47+
.split("\\s+")).map(str -> "#" + str).collect(joining(" "));
48+
return By.cssSelector(id);
49+
}
6350
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.grid.node.locators;
19+
20+
import com.google.auto.service.AutoService;
21+
22+
import org.openqa.selenium.By;
23+
import org.openqa.selenium.internal.Require;
24+
import org.openqa.selenium.remote.locators.CustomLocator;
25+
26+
/**
27+
* A class implementing {link @CustomLocator} and to be used as a fallback locator on the server
28+
* side.
29+
*/
30+
31+
@AutoService(CustomLocator.class)
32+
public class ByName extends CustomLocator {
33+
@Override
34+
public String getLocatorName() {
35+
return "name";
36+
}
37+
38+
@Override
39+
public By createBy(Object usingParameter) {
40+
Require.argument("Locator value", usingParameter).instanceOf(String.class);
41+
String name = String.valueOf(usingParameter).replace("'", "\\'");
42+
return By.cssSelector(String.format("*[name='%s']", name));
43+
}
44+
}

java/server/test/org/openqa/selenium/grid/node/CustomLocatorHandlerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.openqa.selenium.events.local.GuavaEventBus;
3333
import org.openqa.selenium.grid.data.Session;
3434
import org.openqa.selenium.grid.node.local.LocalNode;
35-
import org.openqa.selenium.grid.node.locators.FallbackLocators;
35+
import org.openqa.selenium.grid.node.locators.ById;
3636
import org.openqa.selenium.grid.security.Secret;
3737
import org.openqa.selenium.grid.testing.TestSessionFactory;
3838
import org.openqa.selenium.grid.web.ErrorFilter;
@@ -281,7 +281,7 @@ public void shouldFallbackToUseById() {
281281
HttpHandler handler = new CustomLocatorHandler(
282282
node,
283283
registrationSecret,
284-
singleton(new FallbackLocators.ById()));
284+
singleton(new ById()));
285285

286286
HttpResponse res = handler.execute(
287287
new HttpRequest(POST, "/session/1234/elements")

0 commit comments

Comments
 (0)