-
-
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.
[bidi][java] Listen to channel message
- Loading branch information
Showing
4 changed files
with
252 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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.bidi.script; | ||
|
||
import org.openqa.selenium.json.JsonInput; | ||
|
||
public class Message { | ||
private final String channel; | ||
private final RemoteValue data; | ||
private final Source source; | ||
|
||
public Message(String channel, RemoteValue data, Source source) { | ||
this.channel = channel; | ||
this.data = data; | ||
this.source = source; | ||
} | ||
|
||
public static Message fromJson(JsonInput input) { | ||
String channel = null; | ||
RemoteValue data = null; | ||
Source source = null; | ||
|
||
while (input.hasNext()) { | ||
switch (input.nextName()) { | ||
case "channel": | ||
channel = input.read(String.class); | ||
break; | ||
|
||
case "data": | ||
data = input.read(RemoteValue.class); | ||
break; | ||
|
||
case "source": | ||
source = input.read(Source.class); | ||
break; | ||
|
||
default: | ||
input.skipValue(); | ||
break; | ||
} | ||
} | ||
|
||
input.endObject(); | ||
|
||
return new Message(channel, data, source); | ||
} | ||
|
||
public String getChannel() { | ||
return channel; | ||
} | ||
|
||
public RemoteValue getData() { | ||
return data; | ||
} | ||
|
||
public Source getSource() { | ||
return source; | ||
} | ||
} |
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,64 @@ | ||
// 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.bidi.script; | ||
|
||
import java.util.Optional; | ||
import org.openqa.selenium.json.JsonInput; | ||
|
||
public class Source { | ||
private final String realm; | ||
private final Optional<String> browsingContext; | ||
|
||
private Source(String realm, Optional<String> browsingContext) { | ||
this.realm = realm; | ||
this.browsingContext = browsingContext; | ||
} | ||
|
||
public static Source fromJson(JsonInput input) { | ||
String realm = null; | ||
Optional<String> browsingContext = Optional.empty(); | ||
|
||
input.beginObject(); | ||
while (input.hasNext()) { | ||
switch (input.nextName()) { | ||
case "realm": | ||
realm = input.read(String.class); | ||
break; | ||
|
||
case "context": | ||
browsingContext = Optional.ofNullable(input.read(String.class)); | ||
break; | ||
|
||
default: | ||
input.skipValue(); | ||
break; | ||
} | ||
} | ||
|
||
input.endObject(); | ||
|
||
return new Source(realm, browsingContext); | ||
} | ||
|
||
public String getRealm() { | ||
return realm; | ||
} | ||
|
||
public Optional<String> getBrowsingContext() { | ||
return browsingContext; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.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,88 @@ | ||
// 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.bidi.script; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.openqa.selenium.testing.Safely.safelyCall; | ||
import static org.openqa.selenium.testing.drivers.Browser.CHROME; | ||
import static org.openqa.selenium.testing.drivers.Browser.EDGE; | ||
import static org.openqa.selenium.testing.drivers.Browser.IE; | ||
import static org.openqa.selenium.testing.drivers.Browser.SAFARI; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openqa.selenium.bidi.Script; | ||
import org.openqa.selenium.environment.webserver.AppServer; | ||
import org.openqa.selenium.environment.webserver.NettyAppServer; | ||
import org.openqa.selenium.testing.JupiterTestBase; | ||
import org.openqa.selenium.testing.NotYetImplemented; | ||
|
||
public class ScriptEventsTest extends JupiterTestBase { | ||
private AppServer server; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
server = new NettyAppServer(); | ||
server.start(); | ||
} | ||
|
||
@Test | ||
@NotYetImplemented(SAFARI) | ||
@NotYetImplemented(IE) | ||
@NotYetImplemented(EDGE) | ||
@NotYetImplemented(CHROME) | ||
void canAddPreloadScriptWithChannelOptions() | ||
throws ExecutionException, InterruptedException, TimeoutException { | ||
try (Script script = new Script(driver)) { | ||
CompletableFuture<Message> future = new CompletableFuture<>(); | ||
script.onMessage(future::complete); | ||
|
||
script.callFunctionInBrowsingContext( | ||
driver.getWindowHandle(), | ||
"(channel) => channel('foo')", | ||
false, | ||
Optional.of(List.of(LocalValue.channelValue("channel_name"))), | ||
Optional.empty(), | ||
Optional.empty()); | ||
|
||
Message message = future.get(5, TimeUnit.SECONDS); | ||
assertThat(message.getChannel()).isEqualTo("channel_name"); | ||
assertThat(message.getData().getType()).isEqualTo("string"); | ||
assertThat(message.getData().getValue().isPresent()).isTrue(); | ||
assertThat(message.getData().getValue().get()).isEqualTo("foo"); | ||
assertThat(message.getSource().getRealm()).isNotNull(); | ||
assertThat(message.getSource().getBrowsingContext().isPresent()).isTrue(); | ||
assertThat(message.getSource().getBrowsingContext().get()) | ||
.isEqualTo(driver.getWindowHandle()); | ||
} | ||
} | ||
|
||
@AfterEach | ||
public void quitDriver() { | ||
if (driver != null) { | ||
driver.quit(); | ||
} | ||
safelyCall(server::stop); | ||
} | ||
} |