Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void initializeListener(WebDriver webDriver) {
public static EventType<Void> domMutation(Consumer<@Nullable DomMutationEvent> handler) {
Require.nonNull("Handler", handler);

String script = Read.resourceAsString("/org/openqa/selenium/devtools/mutation-listener.js");
String script =
Read.resourceAsString(
CdpEventTypes.class, "/org/openqa/selenium/devtools/mutation-listener.js");

return new EventType<>() {
@Override
Expand Down
14 changes: 13 additions & 1 deletion java/src/org/openqa/selenium/io/Read.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.io;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -53,8 +54,19 @@ public static String toString(InputStream in) throws IOException {
return new String(toByteArray(in), UTF_8);
}

/**
* This method might not work in OSGI environment.
*
* @deprecated Use {@link #resourceAsString(Class, String)} instead
*/
@Deprecated
public static String resourceAsString(String resource) {
try (InputStream stream = Read.class.getResourceAsStream(resource)) {
return resourceAsString(Read.class, resource);
}

public static String resourceAsString(Class<?> resourceOwner, String resource) {
Class<?> clazz = requireNonNull(resourceOwner, "Class owning the resource");
try (InputStream stream = clazz.getResourceAsStream(resource)) {
if (stream == null) {
throw new IllegalArgumentException("Resource not found: " + resource);
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/remote/RemoteScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void removeJavaScriptErrorHandler(long id) {
@Override
public long addDomMutationHandler(Consumer<DomMutation> consumer) {
String scriptValue =
Read.resourceAsString("/org/openqa/selenium/remote/bidi-mutation-listener.js");
Read.resourceAsString(getClass(), "/org/openqa/selenium/remote/bidi-mutation-listener.js");

this.script.addPreloadScript(scriptValue, List.of(new ChannelValue("channel_name")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private List<String> stringToUtf8Array(String toConvert) {
atomFileName,
(fileName) -> {
String rawFunction =
resourceAsString("/org/openqa/selenium/remote/" + atomFileName);
resourceAsString(getClass(), "/org/openqa/selenium/remote/" + atomFileName);
String atomName = fileName.replace(".js", "");
return String.format(
"/* %s */return (%s).apply(null, arguments);", atomName, rawFunction);
Expand Down
2 changes: 1 addition & 1 deletion java/test/org/openqa/selenium/io/ReadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void canReadInputStreamToString() throws IOException {

@Test
void canReadResourceFromClasspath() {
String script = Read.resourceAsString("/org/openqa/selenium/remote/isDisplayed.js");
String script = Read.resourceAsString(Read.class, "/org/openqa/selenium/remote/isDisplayed.js");
assertThat(script).isNotBlank().contains("function(){");
}
}
Loading