Network
Commands
This section contains the APIs related to network commands.
Add network intercept
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const Network = require('selenium-webdriver/bidi/network')
const {until, By, Builder} = require('selenium-webdriver')
const {AddInterceptParameters} = require("selenium-webdriver/bidi/addInterceptParameters");
const {InterceptPhase} = require("selenium-webdriver/bidi/interceptPhase");
describe('Network commands', function () {
let driver
let network
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
network = await Network(driver)
})
afterEach(async function () {
await network.close()
await driver.quit()
})
xit('can add intercept', async function () {
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})
xit('can remove intercept', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
await network.removeIntercept(intercept)
})
xit('can continue with auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const successMessage = 'Congratulations! You must have the proper credentials.'
let elementMessage = await driver.findElement(By.tagName('p')).getText()
assert.equal(elementMessage, successMessage)
})
xit('can continue without auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const alert = await driver.wait(until.alertIsPresent())
await alert.dismiss()
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
xit('can cancel auth ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
})
Remove network intercept
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const Network = require('selenium-webdriver/bidi/network')
const {until, By, Builder} = require('selenium-webdriver')
const {AddInterceptParameters} = require("selenium-webdriver/bidi/addInterceptParameters");
const {InterceptPhase} = require("selenium-webdriver/bidi/interceptPhase");
describe('Network commands', function () {
let driver
let network
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
network = await Network(driver)
})
afterEach(async function () {
await network.close()
await driver.quit()
})
xit('can add intercept', async function () {
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})
xit('can remove intercept', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
await network.removeIntercept(intercept)
})
xit('can continue with auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const successMessage = 'Congratulations! You must have the proper credentials.'
let elementMessage = await driver.findElement(By.tagName('p')).getText()
assert.equal(elementMessage, successMessage)
})
xit('can continue without auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const alert = await driver.wait(until.alertIsPresent())
await alert.dismiss()
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
xit('can cancel auth ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
})
Continue request blocked at authRequired phase with credentials
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const Network = require('selenium-webdriver/bidi/network')
const {until, By, Builder} = require('selenium-webdriver')
const {AddInterceptParameters} = require("selenium-webdriver/bidi/addInterceptParameters");
const {InterceptPhase} = require("selenium-webdriver/bidi/interceptPhase");
describe('Network commands', function () {
let driver
let network
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
network = await Network(driver)
})
afterEach(async function () {
await network.close()
await driver.quit()
})
xit('can add intercept', async function () {
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})
xit('can remove intercept', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
await network.removeIntercept(intercept)
})
xit('can continue with auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const successMessage = 'Congratulations! You must have the proper credentials.'
let elementMessage = await driver.findElement(By.tagName('p')).getText()
assert.equal(elementMessage, successMessage)
})
xit('can continue without auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const alert = await driver.wait(until.alertIsPresent())
await alert.dismiss()
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
xit('can cancel auth ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
})
Continue request blocked at authRequired phase without credentials
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const Network = require('selenium-webdriver/bidi/network')
const {until, By, Builder} = require('selenium-webdriver')
const {AddInterceptParameters} = require("selenium-webdriver/bidi/addInterceptParameters");
const {InterceptPhase} = require("selenium-webdriver/bidi/interceptPhase");
describe('Network commands', function () {
let driver
let network
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
network = await Network(driver)
})
afterEach(async function () {
await network.close()
await driver.quit()
})
xit('can add intercept', async function () {
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})
xit('can remove intercept', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
await network.removeIntercept(intercept)
})
xit('can continue with auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const successMessage = 'Congratulations! You must have the proper credentials.'
let elementMessage = await driver.findElement(By.tagName('p')).getText()
assert.equal(elementMessage, successMessage)
})
xit('can continue without auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const alert = await driver.wait(until.alertIsPresent())
await alert.dismiss()
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
xit('can cancel auth ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
})
Cancel request blocked at authRequired phase
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const Network = require('selenium-webdriver/bidi/network')
const {until, By, Builder} = require('selenium-webdriver')
const {AddInterceptParameters} = require("selenium-webdriver/bidi/addInterceptParameters");
const {InterceptPhase} = require("selenium-webdriver/bidi/interceptPhase");
describe('Network commands', function () {
let driver
let network
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
network = await Network(driver)
})
afterEach(async function () {
await network.close()
await driver.quit()
})
xit('can add intercept', async function () {
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})
xit('can remove intercept', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
await network.removeIntercept(intercept)
})
xit('can continue with auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuth(event.request.request, 'admin','admin')
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const successMessage = 'Congratulations! You must have the proper credentials.'
let elementMessage = await driver.findElement(By.tagName('p')).getText()
assert.equal(elementMessage, successMessage)
})
xit('can continue without auth credentials ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.continueWithAuthNoCredentials(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
const alert = await driver.wait(until.alertIsPresent())
await alert.dismiss()
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
xit('can cancel auth ', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
await network.authRequired(async (event) => {
await network.cancelAuth(event.request.request)
})
await driver.get('https://the-internet.herokuapp.com/basic_auth')
let source = await driver.getPageSource()
assert.equal(source.includes('Not authorized'), true)
})
})
Fail request
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.InterceptPhase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class NetworkCommandsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@Test
@Disabled
void canAddIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
}
}
@Test
@Disabled
void canRemoveIntercept() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
Assertions.assertNotNull(intercept);
network.removeIntercept(intercept);
}
}
@Test
@Disabled
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "admin")));
driver.get("https://the-internet.herokuapp.com/basic_auth");
String successMessage = "Congratulations! You must have the proper credentials.";
WebElement elementMessage = driver.findElement(By.tagName("p"));
Assertions.assertEquals(successMessage, elementMessage.getText());
}
}
@Test
@Disabled
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
driver.get("https://the-internet.herokuapp.com/basic_auth");
Assertions.assertTrue(driver.getPageSource().contains("Not authorized"));
}
}
@Test
@Disabled
void canFailRequest() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
network.onBeforeRequestSent(
responseDetails -> network.failRequest(responseDetails.getRequest().getRequestId()));
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
Assertions.assertThrows(TimeoutException.class, () -> driver.get("https://the-internet.herokuapp.com/basic_auth"));
}
}
}
Events
This section contains the APIs related to network events.
Before Request Sent
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
class NetworkEventsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}
@Test
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
}
}
@Test
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
driver.get("https://www.selenium.dev/selenium/web/blankPage");
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
}
}
@Test
void canListenToOnAuthRequiredEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(401L, response.getResponseData().getStatus());
}
}
}
let beforeRequestEvent = null
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
Show full example
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const { Network } = require("selenium-webdriver/bidi/network");
const {until, Builder} = require("selenium-webdriver");
describe('Network events', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
it('can listen to event before request is sent', async function () {
let beforeRequestEvent = null
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
assert.equal(beforeRequestEvent.request.method, 'GET')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
})
it('can request cookies', async function () {
const network = await Network(driver)
let beforeRequestEvent = null
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
await driver.manage().addCookie({
name: 'north',
value: 'biryani',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.method, 'GET')
assert.equal(beforeRequestEvent.request.cookies[0].name, 'north')
assert.equal(beforeRequestEvent.request.cookies[0].value.value, 'biryani')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
await driver.manage().addCookie({
name: 'south',
value: 'dosa',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.cookies[1].name, 'south')
assert.equal(beforeRequestEvent.request.cookies[1].value.value, 'dosa')
})
it('can redirect http equiv', async function () {
let beforeRequestEvent = []
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent.push(event)
})
await driver.get('http://www.selenium.dev/selenium/web/bidi/redirected_http_equiv.html')
await driver.wait(until.urlContains('redirected.html'), 1000)
assert.equal(beforeRequestEvent[0].request.method, 'GET')
assert(beforeRequestEvent[0].request.url.includes('redirected_http_equiv.html'))
assert.equal(beforeRequestEvent[2].request.method, 'GET')
assert(beforeRequestEvent[3].request.url.includes('redirected.html'))
})
it('can subscribe to response started', async function () {
let onResponseStarted = []
const network = await Network(driver)
await network.responseStarted(function (event) {
onResponseStarted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseStarted[0].request.method, 'GET')
assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())
})
it('can subscribe to response completed', async function () {
let onResponseCompleted = []
const network = await Network(driver)
await network.responseCompleted(function (event) {
onResponseCompleted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseCompleted[0].request.method, 'GET')
assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseCompleted[0].response.fromCache, false)
assert.equal(onResponseCompleted[0].response.status, 200)
})
})
Response Started
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
class NetworkEventsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}
@Test
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
}
}
@Test
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
driver.get("https://www.selenium.dev/selenium/web/blankPage");
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
}
}
@Test
void canListenToOnAuthRequiredEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(401L, response.getResponseData().getStatus());
}
}
}
let onResponseStarted = []
const network = await Network(driver)
await network.responseStarted(function (event) {
onResponseStarted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
Show full example
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const { Network } = require("selenium-webdriver/bidi/network");
const {until, Builder} = require("selenium-webdriver");
describe('Network events', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
it('can listen to event before request is sent', async function () {
let beforeRequestEvent = null
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
assert.equal(beforeRequestEvent.request.method, 'GET')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
})
it('can request cookies', async function () {
const network = await Network(driver)
let beforeRequestEvent = null
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
await driver.manage().addCookie({
name: 'north',
value: 'biryani',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.method, 'GET')
assert.equal(beforeRequestEvent.request.cookies[0].name, 'north')
assert.equal(beforeRequestEvent.request.cookies[0].value.value, 'biryani')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
await driver.manage().addCookie({
name: 'south',
value: 'dosa',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.cookies[1].name, 'south')
assert.equal(beforeRequestEvent.request.cookies[1].value.value, 'dosa')
})
it('can redirect http equiv', async function () {
let beforeRequestEvent = []
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent.push(event)
})
await driver.get('http://www.selenium.dev/selenium/web/bidi/redirected_http_equiv.html')
await driver.wait(until.urlContains('redirected.html'), 1000)
assert.equal(beforeRequestEvent[0].request.method, 'GET')
assert(beforeRequestEvent[0].request.url.includes('redirected_http_equiv.html'))
assert.equal(beforeRequestEvent[2].request.method, 'GET')
assert(beforeRequestEvent[3].request.url.includes('redirected.html'))
})
it('can subscribe to response started', async function () {
let onResponseStarted = []
const network = await Network(driver)
await network.responseStarted(function (event) {
onResponseStarted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseStarted[0].request.method, 'GET')
assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())
})
it('can subscribe to response completed', async function () {
let onResponseCompleted = []
const network = await Network(driver)
await network.responseCompleted(function (event) {
onResponseCompleted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseCompleted[0].request.method, 'GET')
assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseCompleted[0].response.fromCache, false)
assert.equal(onResponseCompleted[0].response.status, 200)
})
})
Response Completed
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
class NetworkEventsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}
@Test
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
}
}
@Test
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
driver.get("https://www.selenium.dev/selenium/web/blankPage");
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
}
}
@Test
void canListenToOnAuthRequiredEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(401L, response.getResponseData().getStatus());
}
}
}
let onResponseCompleted = []
const network = await Network(driver)
await network.responseCompleted(function (event) {
onResponseCompleted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
Show full example
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const { Network } = require("selenium-webdriver/bidi/network");
const {until, Builder} = require("selenium-webdriver");
describe('Network events', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
it('can listen to event before request is sent', async function () {
let beforeRequestEvent = null
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
assert.equal(beforeRequestEvent.request.method, 'GET')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
})
it('can request cookies', async function () {
const network = await Network(driver)
let beforeRequestEvent = null
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
await driver.manage().addCookie({
name: 'north',
value: 'biryani',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.method, 'GET')
assert.equal(beforeRequestEvent.request.cookies[0].name, 'north')
assert.equal(beforeRequestEvent.request.cookies[0].value.value, 'biryani')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
await driver.manage().addCookie({
name: 'south',
value: 'dosa',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.cookies[1].name, 'south')
assert.equal(beforeRequestEvent.request.cookies[1].value.value, 'dosa')
})
it('can redirect http equiv', async function () {
let beforeRequestEvent = []
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent.push(event)
})
await driver.get('http://www.selenium.dev/selenium/web/bidi/redirected_http_equiv.html')
await driver.wait(until.urlContains('redirected.html'), 1000)
assert.equal(beforeRequestEvent[0].request.method, 'GET')
assert(beforeRequestEvent[0].request.url.includes('redirected_http_equiv.html'))
assert.equal(beforeRequestEvent[2].request.method, 'GET')
assert(beforeRequestEvent[3].request.url.includes('redirected.html'))
})
it('can subscribe to response started', async function () {
let onResponseStarted = []
const network = await Network(driver)
await network.responseStarted(function (event) {
onResponseStarted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseStarted[0].request.method, 'GET')
assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())
})
it('can subscribe to response completed', async function () {
let onResponseCompleted = []
const network = await Network(driver)
await network.responseCompleted(function (event) {
onResponseCompleted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseCompleted[0].request.method, 'GET')
assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseCompleted[0].response.fromCache, false)
assert.equal(onResponseCompleted[0].response.status, 200)
})
})
Auth Required
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
Show full example
package dev.selenium.bidirectional.webdriver_bidi;
import dev.selenium.BaseTest;
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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
class NetworkEventsTest extends BaseTest {
@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}
@Test
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
}
}
@Test
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}
@Test
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
driver.get("https://www.selenium.dev/selenium/web/blankPage");
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
}
}
@Test
void canListenToOnAuthRequiredEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(401L, response.getResponseData().getStatus());
}
}
}