Skip to content

Commit 5d807aa

Browse files
webdriver bidi tests
1 parent 68c0705 commit 5d807aa

File tree

4 files changed

+268
-1
lines changed

4 files changed

+268
-1
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<maven.compiler.source>17</maven.compiler.source>
1313
<maven.compiler.target>17</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<selenium.version>4.16.1</selenium.version>
15+
<selenium.version>4.19.1</selenium.version>
1616
<restassured.version>5.4.0</restassured.version>
1717
<appium.version>9.0.0</appium.version>
1818
<testng.version>7.8.0</testng.version>
@@ -29,6 +29,12 @@
2929
<artifactId>selenium-java</artifactId>
3030
<version>${selenium.version}</version>
3131
</dependency>
32+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
33+
<dependency>
34+
<groupId>org.junit.jupiter</groupId>
35+
<artifactId>junit-jupiter</artifactId>
36+
<version>5.10.2</version>
37+
</dependency>
3238
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
3339
<dependency>
3440
<groupId>io.rest-assured</groupId>

src/test/java/demo/customassert/ProductTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package demo.customassert;
22

3+
import org.testng.Assert;
34
import org.testng.annotations.Test;
45

6+
import java.util.Objects;
7+
import java.util.stream.Stream;
8+
59
public class ProductTest extends BaseTest{
610

711
@Test(description = "Test Assertions using AssertJ")
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package demo.webdriver.bidi;
2+
3+
import java.time.Duration;
4+
import java.util.concurrent.*;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.openqa.selenium.By;
10+
import org.openqa.selenium.WebDriver;
11+
import org.openqa.selenium.bidi.module.LogInspector;
12+
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
13+
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
14+
import org.openqa.selenium.bidi.log.LogLevel;
15+
import org.openqa.selenium.bidi.log.StackTrace;
16+
import org.openqa.selenium.firefox.FirefoxDriver;
17+
import org.openqa.selenium.firefox.FirefoxOptions;
18+
import org.openqa.selenium.support.ui.WebDriverWait;
19+
20+
class LogTest {
21+
22+
WebDriver driver;
23+
24+
@BeforeEach
25+
public void setup() {
26+
FirefoxOptions options = new FirefoxOptions();
27+
options.setCapability("webSocketUrl", true);
28+
driver = new FirefoxDriver(options);
29+
}
30+
31+
@Test
32+
public void jsErrors() {
33+
CopyOnWriteArrayList<ConsoleLogEntry> logs = new CopyOnWriteArrayList<>();
34+
35+
try (LogInspector logInspector = new LogInspector(driver)) {
36+
logInspector.onConsoleEntry(logs::add);
37+
}
38+
39+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
40+
driver.findElement(By.id("consoleLog")).click();
41+
42+
new WebDriverWait(driver, Duration.ofSeconds(5)).until(_d -> !logs.isEmpty());
43+
Assertions.assertEquals("Hello, world!", logs.get(0).getText());
44+
}
45+
46+
@Test
47+
void testListenToConsoleLog() throws ExecutionException, InterruptedException, TimeoutException {
48+
try (LogInspector logInspector = new LogInspector(driver)) {
49+
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
50+
logInspector.onConsoleEntry(future::complete);
51+
52+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
53+
driver.findElement(By.id("consoleLog")).click();
54+
55+
ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
56+
57+
Assertions.assertEquals("Hello, world!", logEntry.getText());
58+
Assertions.assertNull(logEntry.getRealm());
59+
Assertions.assertEquals(1, logEntry.getArgs().size());
60+
Assertions.assertEquals("console", logEntry.getType());
61+
Assertions.assertEquals("log", logEntry.getMethod());
62+
Assertions.assertNull(logEntry.getStackTrace());
63+
}
64+
}
65+
66+
@Test
67+
void testListenToJavascriptLog()
68+
throws ExecutionException, InterruptedException, TimeoutException {
69+
try (LogInspector logInspector = new LogInspector(driver)) {
70+
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
71+
logInspector.onJavaScriptLog(future::complete);
72+
73+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
74+
driver.findElement(By.id("jsException")).click();
75+
76+
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
77+
78+
Assertions.assertEquals("Error: Not working", logEntry.getText());
79+
Assertions.assertEquals("javascript", logEntry.getType());
80+
Assertions.assertEquals(LogLevel.ERROR, logEntry.getLevel());
81+
}
82+
}
83+
84+
@Test
85+
void testListenToJavascriptErrorLog()
86+
throws ExecutionException, InterruptedException, TimeoutException {
87+
try (LogInspector logInspector = new LogInspector(driver)) {
88+
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
89+
logInspector.onJavaScriptException(future::complete);
90+
91+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
92+
driver.findElement(By.id("jsException")).click();
93+
94+
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
95+
96+
Assertions.assertEquals("Error: Not working", logEntry.getText());
97+
Assertions.assertEquals("javascript", logEntry.getType());
98+
}
99+
}
100+
101+
@Test
102+
void testRetrieveStacktraceForALog()
103+
throws ExecutionException, InterruptedException, TimeoutException {
104+
try (LogInspector logInspector = new LogInspector(driver)) {
105+
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
106+
logInspector.onJavaScriptException(future::complete);
107+
108+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
109+
driver.findElement(By.id("logWithStacktrace")).click();
110+
111+
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
112+
113+
StackTrace stackTrace = logEntry.getStackTrace();
114+
Assertions.assertNotNull(stackTrace);
115+
Assertions.assertEquals(4, stackTrace.getCallFrames().size());
116+
}
117+
}
118+
119+
@Test
120+
void testListenToLogsWithMultipleConsumers()
121+
throws ExecutionException, InterruptedException, TimeoutException {
122+
try (LogInspector logInspector = new LogInspector(driver)) {
123+
CompletableFuture<JavascriptLogEntry> completableFuture1 = new CompletableFuture<>();
124+
logInspector.onJavaScriptLog(completableFuture1::complete);
125+
126+
CompletableFuture<JavascriptLogEntry> completableFuture2 = new CompletableFuture<>();
127+
logInspector.onJavaScriptLog(completableFuture2::complete);
128+
129+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
130+
driver.findElement(By.id("jsException")).click();
131+
132+
JavascriptLogEntry logEntry = completableFuture1.get(5, TimeUnit.SECONDS);
133+
134+
Assertions.assertEquals("Error: Not working", logEntry.getText());
135+
Assertions.assertEquals("javascript", logEntry.getType());
136+
137+
logEntry = completableFuture2.get(5, TimeUnit.SECONDS);
138+
139+
Assertions.assertEquals("Error: Not working", logEntry.getText());
140+
Assertions.assertEquals("javascript", logEntry.getType());
141+
}
142+
}
143+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package demo.webdriver.bidi;
2+
import java.util.concurrent.CompletableFuture;
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.TimeUnit;
5+
import java.util.concurrent.TimeoutException;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.openqa.selenium.Cookie;
10+
import org.openqa.selenium.WebDriver;
11+
import org.openqa.selenium.bidi.module.Network;
12+
import org.openqa.selenium.bidi.network.BeforeRequestSent;
13+
import org.openqa.selenium.bidi.network.ResponseDetails;
14+
import org.openqa.selenium.firefox.FirefoxDriver;
15+
import org.openqa.selenium.firefox.FirefoxOptions;
16+
17+
class NetworkEventsTest {
18+
19+
WebDriver driver;
20+
21+
@BeforeEach
22+
public void setup() {
23+
FirefoxOptions options = new FirefoxOptions();
24+
options.setCapability("webSocketUrl", true);
25+
driver = new FirefoxDriver(options);
26+
}
27+
28+
@Test
29+
void canListenToBeforeRequestSentEvent()
30+
throws ExecutionException, InterruptedException, TimeoutException {
31+
try (Network network = new Network(driver)) {
32+
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
33+
network.onBeforeRequestSent(future::complete);
34+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
35+
36+
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
37+
String windowHandle = driver.getWindowHandle();
38+
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
39+
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
40+
}
41+
}
42+
43+
@Test
44+
void canListenToResponseStartedEvent()
45+
throws ExecutionException, InterruptedException, TimeoutException {
46+
try (Network network = new Network(driver)) {
47+
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
48+
network.onResponseStarted(future::complete);
49+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
50+
51+
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
52+
String windowHandle = driver.getWindowHandle();
53+
54+
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
55+
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
56+
Assertions.assertEquals(200L, response.getResponseData().getStatus());
57+
}
58+
}
59+
60+
@Test
61+
void canListenToResponseCompletedEvent()
62+
throws ExecutionException, InterruptedException, TimeoutException {
63+
try (Network network = new Network(driver)) {
64+
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
65+
network.onResponseCompleted(future::complete);
66+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
67+
68+
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
69+
String windowHandle = driver.getWindowHandle();
70+
71+
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
72+
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
73+
Assertions.assertEquals(200L, response.getResponseData().getStatus());
74+
}
75+
}
76+
77+
@Test
78+
void canListenToResponseCompletedEventWithCookie()
79+
throws ExecutionException, InterruptedException, TimeoutException {
80+
try (Network network = new Network(driver)) {
81+
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
82+
83+
driver.get("https://www.selenium.dev/selenium/web/blankPage");
84+
driver.manage().addCookie(new Cookie("foo", "bar"));
85+
network.onBeforeRequestSent(future::complete);
86+
driver.navigate().refresh();
87+
88+
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
89+
String windowHandle = driver.getWindowHandle();
90+
91+
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
92+
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
93+
94+
Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
95+
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
96+
}
97+
}
98+
99+
@Test
100+
void canListenToOnAuthRequiredEvent()
101+
throws ExecutionException, InterruptedException, TimeoutException {
102+
try (Network network = new Network(driver)) {
103+
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
104+
network.onAuthRequired(future::complete);
105+
driver.get("https://the-internet.herokuapp.com/basic_auth");
106+
107+
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
108+
String windowHandle = driver.getWindowHandle();
109+
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
110+
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
111+
Assertions.assertEquals(401L, response.getResponseData().getStatus());
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)