Browsing Context

Page being translated from English to Portuguese. Do you speak Portuguese? Help us to translate it by sending us pull requests!

This section contains the APIs related to input commands.

Perform Actions

Selenium v4.17

        Actions selectThreeOptions =
                actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3)).keyUp(Keys.SHIFT);

        input.perform(windowHandle, selectThreeOptions.getSequences());
Show full example
package dev.selenium.bidirectional.webdriver_bidi;

import dev.selenium.BaseTest;
import java.util.List;
import java.util.Map;

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.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Input;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.interactions.Actions;

class ActionsTest extends BaseTest {
    private Input input;

    private String windowHandle;

    @BeforeEach
    public void setup() {
        FirefoxOptions options = new FirefoxOptions();
        options.setCapability("webSocketUrl", true);
        driver = new FirefoxDriver(options);
        windowHandle = driver.getWindowHandle();
        input = new Input(driver);
    }

    @Test
    void canPerformInputActions() {
        driver.get("https://www.selenium.dev/selenium/web/formSelectionPage.html");

        List<WebElement> options = driver.findElements(By.tagName("option"));

        Actions actions = new Actions(driver);
        Actions selectThreeOptions =
                actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3)).keyUp(Keys.SHIFT);

        input.perform(windowHandle, selectThreeOptions.getSequences());

        WebElement showButton = driver.findElement(By.name("showselected"));
        showButton.click();

        WebElement resultElement = driver.findElement(By.id("result"));
        Assertions.assertTrue(resultElement.getText().contains("roquefort parmigiano cheddar"));
        }

    @Test
    void canPerformReleaseAction() {
        driver.get("https://www.selenium.dev/selenium/web/bidi/release_action.html");

        WebElement inputTextBox = driver.findElement(By.id("keys"));

        Actions sendLowercase =
                new Actions(driver).keyDown(inputTextBox, "a").keyDown(inputTextBox, "b");

        input.perform(windowHandle, sendLowercase.getSequences());
        ((JavascriptExecutor) driver).executeScript("resetEvents()");

        input.release(windowHandle);

        List<Map<String, Object>> events =
                (List<Map<String, Object>>)
                        ((JavascriptExecutor) driver).executeScript("return allEvents.events");
        Assertions.assertEquals("KeyB", events.get(0).get("code"));
        Assertions.assertEquals("KeyA", events.get(1).get("code"));
        }
    }

Selenium v4.17

    const actions = driver.actions().click(options[1]).keyDown(Key.SHIFT).click(options[3]).keyUp(Key.SHIFT).getSequences()

    await input.perform(browsingContextId, actions)
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const {By, Key, Builder} = require("selenium-webdriver")
const Input = require('selenium-webdriver/bidi/input')

describe('Input module', 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 perform input action', async function () {
    const browsingContextId = await driver.getWindowHandle()
    const input = await Input(driver)
    await driver.get('https://www.selenium.dev/selenium/web/formSelectionPage.html')

    let options = await driver.findElements(By.tagName('option'))

    const actions = driver.actions().click(options[1]).keyDown(Key.SHIFT).click(options[3]).keyUp(Key.SHIFT).getSequences()

    await input.perform(browsingContextId, actions)

    let showButton = await driver.findElement(By.name('showselected'))
    showButton.click()

    let resultElement = await driver.findElement(By.id('result'))
    await resultElement.getText().then(function (text) {
      assert(text.includes('oquefort parmigiano cheddar'))
    })
  })

  it('can execute release in browsing context', async function () {
    const browsingContextId = await driver.getWindowHandle()
    const input = await Input(driver)
    await driver.get('https://www.selenium.dev/selenium/web/bidi/release_action.html')

    let inputTextBox = await driver.findElement(By.id('keys'))

    await driver.executeScript('arguments[0].focus()', inputTextBox)

    const actions = driver.actions().keyDown('a').keyDown('b').getSequences()

    await input.perform(browsingContextId, actions)

    await driver.executeScript('resetEvents()')

    await input.release(browsingContextId)

    const events = await driver.executeScript('return allEvents.events')

    assert.strictEqual(events[0].code, 'KeyB')
    assert.strictEqual(events[1].code, 'KeyA')
  })
})

Release Actions

Selenium v4.17

        Actions sendLowercase =
                new Actions(driver).keyDown(inputTextBox, "a").keyDown(inputTextBox, "b");

        input.perform(windowHandle, sendLowercase.getSequences());
        ((JavascriptExecutor) driver).executeScript("resetEvents()");

        input.release(windowHandle);
Show full example
package dev.selenium.bidirectional.webdriver_bidi;

import dev.selenium.BaseTest;
import java.util.List;
import java.util.Map;

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.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Input;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.interactions.Actions;

class ActionsTest extends BaseTest {
    private Input input;

    private String windowHandle;

    @BeforeEach
    public void setup() {
        FirefoxOptions options = new FirefoxOptions();
        options.setCapability("webSocketUrl", true);
        driver = new FirefoxDriver(options);
        windowHandle = driver.getWindowHandle();
        input = new Input(driver);
    }

    @Test
    void canPerformInputActions() {
        driver.get("https://www.selenium.dev/selenium/web/formSelectionPage.html");

        List<WebElement> options = driver.findElements(By.tagName("option"));

        Actions actions = new Actions(driver);
        Actions selectThreeOptions =
                actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3)).keyUp(Keys.SHIFT);

        input.perform(windowHandle, selectThreeOptions.getSequences());

        WebElement showButton = driver.findElement(By.name("showselected"));
        showButton.click();

        WebElement resultElement = driver.findElement(By.id("result"));
        Assertions.assertTrue(resultElement.getText().contains("roquefort parmigiano cheddar"));
        }

    @Test
    void canPerformReleaseAction() {
        driver.get("https://www.selenium.dev/selenium/web/bidi/release_action.html");

        WebElement inputTextBox = driver.findElement(By.id("keys"));

        Actions sendLowercase =
                new Actions(driver).keyDown(inputTextBox, "a").keyDown(inputTextBox, "b");

        input.perform(windowHandle, sendLowercase.getSequences());
        ((JavascriptExecutor) driver).executeScript("resetEvents()");

        input.release(windowHandle);

        List<Map<String, Object>> events =
                (List<Map<String, Object>>)
                        ((JavascriptExecutor) driver).executeScript("return allEvents.events");
        Assertions.assertEquals("KeyB", events.get(0).get("code"));
        Assertions.assertEquals("KeyA", events.get(1).get("code"));
        }
    }

Selenium v4.17

    await input.release(browsingContextId)
Show full example
const assert = require("assert")
const firefox = require('selenium-webdriver/firefox')
const {By, Key, Builder} = require("selenium-webdriver")
const Input = require('selenium-webdriver/bidi/input')

describe('Input module', 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 perform input action', async function () {
    const browsingContextId = await driver.getWindowHandle()
    const input = await Input(driver)
    await driver.get('https://www.selenium.dev/selenium/web/formSelectionPage.html')

    let options = await driver.findElements(By.tagName('option'))

    const actions = driver.actions().click(options[1]).keyDown(Key.SHIFT).click(options[3]).keyUp(Key.SHIFT).getSequences()

    await input.perform(browsingContextId, actions)

    let showButton = await driver.findElement(By.name('showselected'))
    showButton.click()

    let resultElement = await driver.findElement(By.id('result'))
    await resultElement.getText().then(function (text) {
      assert(text.includes('oquefort parmigiano cheddar'))
    })
  })

  it('can execute release in browsing context', async function () {
    const browsingContextId = await driver.getWindowHandle()
    const input = await Input(driver)
    await driver.get('https://www.selenium.dev/selenium/web/bidi/release_action.html')

    let inputTextBox = await driver.findElement(By.id('keys'))

    await driver.executeScript('arguments[0].focus()', inputTextBox)

    const actions = driver.actions().keyDown('a').keyDown('b').getSequences()

    await input.perform(browsingContextId, actions)

    await driver.executeScript('resetEvents()')

    await input.release(browsingContextId)

    const events = await driver.executeScript('return allEvents.events')

    assert.strictEqual(events[0].code, 'KeyB')
    assert.strictEqual(events[1].code, 'KeyA')
  })
})