-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the remote server to handle relative locators
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
Showing
7 changed files
with
137 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java/src/org/openqa/selenium/support/locators/RelativeLocatorScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
java/src/org/openqa/selenium/support/locators/RelativeLocatorServerSide.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters