Ações API Uma interface de baixo nível para fornecer ações de entrada de dispositivo virtualizadas para o navegador da web..
Além das interações de alto nível , a API de Ações oferece controle detalhado sobre o que dispositivos de entrada designados podem fazer. O Selenium fornece uma interface para 3 tipos de fontes de entrada: entrada de teclado para dispositivos de teclado, entrada de ponteiro para mouse, caneta ou dispositivos de toque, e entrada de roda para dispositivos de roda de rolagem (introduzida no Selenium 4.2). O Selenium permite que você construa comandos de ação individuais atribuídos a entradas específicas, encadeie-os e chame o método de execução associado para executá-los todos de uma vez.
Construtor de Ações Na transição do antigo Protocolo JSON Wire para o novo Protocolo W3C WebDriver, os componentes de construção de ações de baixo nível se tornaram especialmente detalhados. Isso é extremamente poderoso, mas cada dispositivo de entrada possui várias maneiras de ser utilizado e, se você precisa gerenciar mais de um dispositivo, é responsável por garantir a sincronização adequada entre eles.
Felizmente, provavelmente você não precisa aprender a usar os comandos de baixo nível diretamente, uma vez que quase tudo o que você pode querer fazer foi fornecido com um método de conveniência que combina os comandos de nível inferior para você. Todos esses métodos estão documentados nas páginas de teclado , mouse , caneta e roda .
Pausa Movimentos de ponteiro e rolagem da roda permitem que o usuário defina uma duração para a ação, mas às vezes você só precisa esperar um momento entre as ações para que as coisas funcionem corretamente.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/ActionsTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
public class ActionsTest extends BaseChromeTest {
@Test
public void pause () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
long start = System . currentTimeMillis ();
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ();
long duration = System . currentTimeMillis () - start ;
Assertions . assertTrue ( duration > 2000 );
Assertions . assertTrue ( duration < 3000 );
}
@Test
public void releasesAll () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
Actions actions = new Actions ( driver );
actions . clickAndHold ( clickable )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
(( RemoteWebDriver ) driver ). resetInputState ();
actions . sendKeys ( "a" ). perform ();
Assertions . assertEquals ( "A" , String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 0 )));
Assertions . assertEquals ( "a" , String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 1 )));
}
}
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. move_to_element ( clickable ) \
. pause ( 1 ) \
. click_and_hold () \
. pause ( 1 ) \
. send_keys ( "abc" ) \
. perform () /examples/python/tests/actions_api/test_actions.py
Copy
Close
from time import time
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
def test_pauses ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
start = time ()
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. move_to_element ( clickable ) \
. pause ( 1 ) \
. click_and_hold () \
. pause ( 1 ) \
. send_keys ( "abc" ) \
. perform ()
duration = time () - start
assert duration > 2
assert duration < 3
def test_releases_all ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. key_down ( Keys . SHIFT ) \
. key_down ( "a" ) \
. perform ()
ActionBuilder ( driver ) . clear_actions ()
ActionChains ( driver ) . key_down ( 'a' ) . perform ()
assert clickable . get_attribute ( 'value' )[ 0 ] == "A"
assert clickable . get_attribute ( 'value' )[ 1 ] == "a"
Selenium v4.2
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. MoveToElement ( clickable )
. Pause ( TimeSpan . FromSeconds ( 1 ))
. ClickAndHold ()
. Pause ( TimeSpan . FromSeconds ( 1 ))
. SendKeys ( "abc" )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/ActionsTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class ActionsTest : BaseChromeTest
{
[TestMethod]
public void Pause ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
DateTime start = DateTime . Now ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. MoveToElement ( clickable )
. Pause ( TimeSpan . FromSeconds ( 1 ))
. ClickAndHold ()
. Pause ( TimeSpan . FromSeconds ( 1 ))
. SendKeys ( "abc" )
. Perform ();
TimeSpan duration = DateTime . Now - start ;
Assert . IsTrue ( duration > TimeSpan . FromSeconds ( 2 ));
Assert . IsTrue ( duration < TimeSpan . FromSeconds ( 3 ));
}
[TestMethod]
public void ReleaseAll ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
var actions = new Actions ( driver );
actions . ClickAndHold ( clickable )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
(( WebDriver ) driver ). ResetInputState ();
actions . SendKeys ( "a" ). Perform ();
var value = clickable . GetAttribute ( "value" );
Assert . AreEqual ( "A" , value [.. 1 ]);
Assert . AreEqual ( "a" , value . Substring ( 1 , 1 ));
}
}
} Selenium v4.2
clickable = driver . find_element ( id : 'clickable' )
driver . action
. move_to ( clickable )
. pause ( duration : 1 )
. click_and_hold
. pause ( duration : 1 )
. send_keys ( 'abc' )
. perform /examples/ruby/spec/actions_api/actions_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Actions' do
let ( :driver ) { start_session }
it 'pauses' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
start = Time . now
clickable = driver . find_element ( id : 'clickable' )
driver . action
. move_to ( clickable )
. pause ( duration : 1 )
. click_and_hold
. pause ( duration : 1 )
. send_keys ( 'abc' )
. perform
duration = Time . now - start
expect ( duration ) . to be > 2
expect ( duration ) . to be < 3
end
it 'releases all' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
action = driver . action
. click_and_hold ( clickable )
. key_down ( :shift )
. key_down ( 'a' )
action . perform
driver . action . release_actions
action . key_down ( 'a' ) . perform
expect ( clickable . attribute ( 'value' ) [ 0 ] ) . to eq 'A'
expect ( clickable . attribute ( 'value' ) [- 1 ] ) . to eq 'a'
end
end
const clickable = await driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. move ({ origin : clickable })
. pause ( 1000 )
. press ()
. pause ( 1000 )
. sendKeys ( 'abc' )
. perform ()
/examples/javascript/test/actionsApi/actionsTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
describe ( 'Actions API - Pause and Release All Actions' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'Pause' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
const start = Date . now ()
const clickable = await driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. move ({ origin : clickable })
. pause ( 1000 )
. press ()
. pause ( 1000 )
. sendKeys ( 'abc' )
. perform ()
const end = Date . now () - start
assert . ok ( end > 2000 )
assert . ok ( end < 4000 )
})
it ( 'Clear' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
const clickable = driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. click ( clickable )
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
await driver . actions (). clear ()
await driver . actions (). sendKeys ( 'a' ). perform ()
const value = await clickable . getAttribute ( 'value' )
assert . deepStrictEqual ( 'A' , value . substring ( 0 , 1 ))
assert . deepStrictEqual ( 'a' , value . substring ( 1 , 2 ))
})
})
Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
class ActionsTest : BaseTest () {
@Test
fun pause () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val start = System . currentTimeMillis ()
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ()
val duration = System . currentTimeMillis () - start
Assertions . assertTrue ( duration > 2000 )
Assertions . assertTrue ( duration < 4000 )
}
@Test
fun releasesAll () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
val actions = Actions ( driver )
actions . clickAndHold ( clickable )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
( driver as RemoteWebDriver ). resetInputState ()
actions . sendKeys ( "a" ). perform ()
Assertions . assertEquals ( "A" , clickable . getAttribute ( "value" ) !! . get ( 0 ). toString ())
Assertions . assertEquals ( "a" , clickable . getAttribute ( "value" ) !! . get ( 1 ). toString ())
}
}
Liberar Todas as Ações Um ponto importante a ser observado é que o driver lembra o estado de todos os itens de entrada ao longo de uma sessão. Mesmo se você criar uma nova instância de uma classe de ações, as teclas pressionadas e a posição do ponteiro permanecerão no estado em que uma ação previamente executada os deixou.
Existe um método especial para liberar todas as teclas pressionadas e botões do ponteiro atualmente pressionados. Esse método é implementado de maneira diferente em cada uma das linguagens porque não é executado com o método de execução (perform).
Java
Python
CSharp
Ruby
JavaScript
Kotlin (( RemoteWebDriver ) driver ). resetInputState (); /examples/java/src/test/java/dev/selenium/actions_api/ActionsTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
public class ActionsTest extends BaseChromeTest {
@Test
public void pause () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
long start = System . currentTimeMillis ();
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ();
long duration = System . currentTimeMillis () - start ;
Assertions . assertTrue ( duration > 2000 );
Assertions . assertTrue ( duration < 3000 );
}
@Test
public void releasesAll () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
Actions actions = new Actions ( driver );
actions . clickAndHold ( clickable )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
(( RemoteWebDriver ) driver ). resetInputState ();
actions . sendKeys ( "a" ). perform ();
Assertions . assertEquals ( "A" , String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 0 )));
Assertions . assertEquals ( "a" , String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 1 )));
}
}
ActionBuilder ( driver ) . clear_actions () /examples/python/tests/actions_api/test_actions.py
Copy
Close
from time import time
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
def test_pauses ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
start = time ()
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. move_to_element ( clickable ) \
. pause ( 1 ) \
. click_and_hold () \
. pause ( 1 ) \
. send_keys ( "abc" ) \
. perform ()
duration = time () - start
assert duration > 2
assert duration < 3
def test_releases_all ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. key_down ( Keys . SHIFT ) \
. key_down ( "a" ) \
. perform ()
ActionBuilder ( driver ) . clear_actions ()
ActionChains ( driver ) . key_down ( 'a' ) . perform ()
assert clickable . get_attribute ( 'value' )[ 0 ] == "A"
assert clickable . get_attribute ( 'value' )[ 1 ] == "a"
(( WebDriver ) driver ). ResetInputState (); /examples/dotnet/SeleniumDocs/ActionsAPI/ActionsTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class ActionsTest : BaseChromeTest
{
[TestMethod]
public void Pause ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
DateTime start = DateTime . Now ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. MoveToElement ( clickable )
. Pause ( TimeSpan . FromSeconds ( 1 ))
. ClickAndHold ()
. Pause ( TimeSpan . FromSeconds ( 1 ))
. SendKeys ( "abc" )
. Perform ();
TimeSpan duration = DateTime . Now - start ;
Assert . IsTrue ( duration > TimeSpan . FromSeconds ( 2 ));
Assert . IsTrue ( duration < TimeSpan . FromSeconds ( 3 ));
}
[TestMethod]
public void ReleaseAll ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
var actions = new Actions ( driver );
actions . ClickAndHold ( clickable )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
(( WebDriver ) driver ). ResetInputState ();
actions . SendKeys ( "a" ). Perform ();
var value = clickable . GetAttribute ( "value" );
Assert . AreEqual ( "A" , value [.. 1 ]);
Assert . AreEqual ( "a" , value . Substring ( 1 , 1 ));
}
}
} driver . action . release_actions /examples/ruby/spec/actions_api/actions_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Actions' do
let ( :driver ) { start_session }
it 'pauses' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
start = Time . now
clickable = driver . find_element ( id : 'clickable' )
driver . action
. move_to ( clickable )
. pause ( duration : 1 )
. click_and_hold
. pause ( duration : 1 )
. send_keys ( 'abc' )
. perform
duration = Time . now - start
expect ( duration ) . to be > 2
expect ( duration ) . to be < 3
end
it 'releases all' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
action = driver . action
. click_and_hold ( clickable )
. key_down ( :shift )
. key_down ( 'a' )
action . perform
driver . action . release_actions
action . key_down ( 'a' ) . perform
expect ( clickable . attribute ( 'value' ) [ 0 ] ) . to eq 'A'
expect ( clickable . attribute ( 'value' ) [- 1 ] ) . to eq 'a'
end
end
await driver . actions (). clear ()
/examples/javascript/test/actionsApi/actionsTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
describe ( 'Actions API - Pause and Release All Actions' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'Pause' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
const start = Date . now ()
const clickable = await driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. move ({ origin : clickable })
. pause ( 1000 )
. press ()
. pause ( 1000 )
. sendKeys ( 'abc' )
. perform ()
const end = Date . now () - start
assert . ok ( end > 2000 )
assert . ok ( end < 4000 )
})
it ( 'Clear' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
const clickable = driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. click ( clickable )
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
await driver . actions (). clear ()
await driver . actions (). sendKeys ( 'a' ). perform ()
const value = await clickable . getAttribute ( 'value' )
assert . deepStrictEqual ( 'A' , value . substring ( 0 , 1 ))
assert . deepStrictEqual ( 'a' , value . substring ( 1 , 2 ))
})
})
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
class ActionsTest : BaseTest () {
@Test
fun pause () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val start = System . currentTimeMillis ()
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ()
val duration = System . currentTimeMillis () - start
Assertions . assertTrue ( duration > 2000 )
Assertions . assertTrue ( duration < 4000 )
}
@Test
fun releasesAll () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
val actions = Actions ( driver )
actions . clickAndHold ( clickable )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
( driver as RemoteWebDriver ). resetInputState ()
actions . sendKeys ( "a" ). perform ()
Assertions . assertEquals ( "A" , clickable . getAttribute ( "value" ) !! . get ( 0 ). toString ())
Assertions . assertEquals ( "a" , clickable . getAttribute ( "value" ) !! . get ( 1 ). toString ())
}
}
1 - Ações de Teclado Uma representação de qualquer dispositivo de entrada de teclado para interagir com uma página da web.
Existem apenas 2 ações que podem ser realizadas com um teclado: pressionar uma tecla e liberar uma tecla pressionada. Além de suportar caracteres ASCII, cada tecla do teclado possui uma representação que pode ser pressionada ou liberada em sequências designadas.
Chaves Além das teclas representadas pelo Unicode regular, valores Unicode foram atribuídos a outras teclas de teclado para uso com o Selenium. Cada linguagem tem sua própria maneira de fazer referência a essas teclas; a lista completa pode ser encontrada
aqui .
Java
Python
CSharp
Ruby
JavaScript
Kotlin Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/java/src/org/openqa/selenium/Keys.java#L28)
Use the [Python Keys class](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/py/selenium/webdriver/common/keys.py#L23)
Use the [.NET static Keys class](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/dotnet/src/webdriver/Keys.cs#L28)
Use the [Ruby KEYS constant](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/rb/lib/selenium/webdriver/common/keys.rb#L28)
Use the [JavaScript KEYS constant](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/javascript/node/selenium-webdriver/lib/input.js#L44)
Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.2.0/java/src/org/openqa/selenium/Keys.java#L28)
Pressione a tecla
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.Platform ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
public class KeysTest extends BaseChromeTest {
@Test
public void keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ));
}
@Test
public void keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
driver . findElement ( By . tagName ( "body" )). click ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ));
}
@Test
public void copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
}
}
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform () /examples/python/tests/actions_api/test_keys.py
Copy
Close
import sys
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.by import By
def test_key_down ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "ABC"
def test_key_up ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "Ab"
def test_send_keys_to_active_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_send_keys_to_designated_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
driver . find_element ( By . TAG_NAME , "body" ) . click ()
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_copy_and_paste ( firefox_driver ):
driver = firefox_driver
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "SeleniumSelenium!"
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
/examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseFirefoxTest
{
[TestMethod]
public void KeyDown ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "A" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void KeyUp ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "Ab" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToActiveElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. SendKeys ( "abc" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToDesignatedElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
. SendKeys ( textField , "abc" )
. Perform ();
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void CopyAndPaste ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
. KeyUp ( Keys . Shift )
. KeyDown ( cmdCtrl )
. SendKeys ( "xvv" )
. KeyUp ( cmdCtrl )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "SeleniumSelenium!" , textField . GetAttribute ( "value" ));
}
}
} driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform /examples/ruby/spec/actions_api/keys_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Keys' do
let ( :driver ) { start_session }
let ( :wait ) { Selenium :: WebDriver :: Wait . new ( timeout : 2 ) }
it 'key down' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'A'
end
it 'key up' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'Ab'
end
it 'sends keys to active element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. send_keys ( 'abc' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'abc'
end
it 'sends keys to designated element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
driver . find_element ( tag_name : 'body' ) . click
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
expect ( text_field . attribute ( 'value' )) . to eq 'Selenium!'
end
it 'copy and paste' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'SeleniumSelenium!'
end
end
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
/examples/javascript/test/actionsApi/keysTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
const { platform } = require ( 'node:process' )
describe ( 'Keyboard Action - Keys test' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'KeyDown' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
const textField = driver . findElement ( By . id ( 'textInput' ))
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'A' )
})
it ( 'KeyUp' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'Ab' )
})
it ( 'sendKeys' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Designated Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . findElement ( By . css ( 'body' )). click ()
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Copy and Paste' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = await driver . findElement ( By . id ( 'textInput' ))
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'SeleniumSelenium!' )
})
})
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.HasCapabilities
import org.openqa.selenium.Keys
import org.openqa.selenium.Platform
import org.openqa.selenium.interactions.Actions
class KeysTest : BaseTest () {
@Test
fun keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ))
}
@Test
fun keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
driver . findElement ( By . tagName ( "body" )). click ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ))
}
@Test
fun copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
val platformName = ( driver as HasCapabilities ). getCapabilities (). getPlatformName ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ))
}
}
Liberar a tecla
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.Platform ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
public class KeysTest extends BaseChromeTest {
@Test
public void keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ));
}
@Test
public void keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
driver . findElement ( By . tagName ( "body" )). click ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ));
}
@Test
public void copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
}
}
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform () /examples/python/tests/actions_api/test_keys.py
Copy
Close
import sys
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.by import By
def test_key_down ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "ABC"
def test_key_up ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "Ab"
def test_send_keys_to_active_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_send_keys_to_designated_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
driver . find_element ( By . TAG_NAME , "body" ) . click ()
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_copy_and_paste ( firefox_driver ):
driver = firefox_driver
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "SeleniumSelenium!"
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseFirefoxTest
{
[TestMethod]
public void KeyDown ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "A" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void KeyUp ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "Ab" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToActiveElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. SendKeys ( "abc" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToDesignatedElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
. SendKeys ( textField , "abc" )
. Perform ();
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void CopyAndPaste ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
. KeyUp ( Keys . Shift )
. KeyDown ( cmdCtrl )
. SendKeys ( "xvv" )
. KeyUp ( cmdCtrl )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "SeleniumSelenium!" , textField . GetAttribute ( "value" ));
}
}
} driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform /examples/ruby/spec/actions_api/keys_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Keys' do
let ( :driver ) { start_session }
let ( :wait ) { Selenium :: WebDriver :: Wait . new ( timeout : 2 ) }
it 'key down' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'A'
end
it 'key up' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'Ab'
end
it 'sends keys to active element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. send_keys ( 'abc' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'abc'
end
it 'sends keys to designated element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
driver . find_element ( tag_name : 'body' ) . click
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
expect ( text_field . attribute ( 'value' )) . to eq 'Selenium!'
end
it 'copy and paste' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'SeleniumSelenium!'
end
end
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
/examples/javascript/test/actionsApi/keysTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
const { platform } = require ( 'node:process' )
describe ( 'Keyboard Action - Keys test' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'KeyDown' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
const textField = driver . findElement ( By . id ( 'textInput' ))
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'A' )
})
it ( 'KeyUp' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'Ab' )
})
it ( 'sendKeys' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Designated Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . findElement ( By . css ( 'body' )). click ()
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Copy and Paste' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = await driver . findElement ( By . id ( 'textInput' ))
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'SeleniumSelenium!' )
})
})
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.HasCapabilities
import org.openqa.selenium.Keys
import org.openqa.selenium.Platform
import org.openqa.selenium.interactions.Actions
class KeysTest : BaseTest () {
@Test
fun keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ))
}
@Test
fun keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
driver . findElement ( By . tagName ( "body" )). click ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ))
}
@Test
fun copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
val platformName = ( driver as HasCapabilities ). getCapabilities (). getPlatformName ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ))
}
}
Enviar teclas This is a convenience method in the Actions API that combines keyDown and keyUp commands in one action.
Executing this command differs slightly from using the element method, but
primarily this gets used when needing to type multiple characters in the middle of other actions.
Elemento Ativo
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. sendKeys ( "abc" )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.Platform ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
public class KeysTest extends BaseChromeTest {
@Test
public void keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ));
}
@Test
public void keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
driver . findElement ( By . tagName ( "body" )). click ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ));
}
@Test
public void copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
}
}
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform () /examples/python/tests/actions_api/test_keys.py
Copy
Close
import sys
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.by import By
def test_key_down ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "ABC"
def test_key_up ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "Ab"
def test_send_keys_to_active_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_send_keys_to_designated_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
driver . find_element ( By . TAG_NAME , "body" ) . click ()
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_copy_and_paste ( firefox_driver ):
driver = firefox_driver
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "SeleniumSelenium!"
new Actions ( driver )
. SendKeys ( "abc" ) /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseFirefoxTest
{
[TestMethod]
public void KeyDown ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "A" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void KeyUp ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "Ab" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToActiveElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. SendKeys ( "abc" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToDesignatedElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
. SendKeys ( textField , "abc" )
. Perform ();
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void CopyAndPaste ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
. KeyUp ( Keys . Shift )
. KeyDown ( cmdCtrl )
. SendKeys ( "xvv" )
. KeyUp ( cmdCtrl )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "SeleniumSelenium!" , textField . GetAttribute ( "value" ));
}
}
} driver . action
. send_keys ( 'abc' )
. perform /examples/ruby/spec/actions_api/keys_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Keys' do
let ( :driver ) { start_session }
let ( :wait ) { Selenium :: WebDriver :: Wait . new ( timeout : 2 ) }
it 'key down' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'A'
end
it 'key up' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'Ab'
end
it 'sends keys to active element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. send_keys ( 'abc' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'abc'
end
it 'sends keys to designated element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
driver . find_element ( tag_name : 'body' ) . click
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
expect ( text_field . attribute ( 'value' )) . to eq 'Selenium!'
end
it 'copy and paste' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'SeleniumSelenium!'
end
end
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
/examples/javascript/test/actionsApi/keysTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
const { platform } = require ( 'node:process' )
describe ( 'Keyboard Action - Keys test' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'KeyDown' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
const textField = driver . findElement ( By . id ( 'textInput' ))
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'A' )
})
it ( 'KeyUp' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'Ab' )
})
it ( 'sendKeys' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Designated Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . findElement ( By . css ( 'body' )). click ()
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Copy and Paste' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = await driver . findElement ( By . id ( 'textInput' ))
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'SeleniumSelenium!' )
})
})
. sendKeys ( "abc" )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.HasCapabilities
import org.openqa.selenium.Keys
import org.openqa.selenium.Platform
import org.openqa.selenium.interactions.Actions
class KeysTest : BaseTest () {
@Test
fun keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ))
}
@Test
fun keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
driver . findElement ( By . tagName ( "body" )). click ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ))
}
@Test
fun copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
val platformName = ( driver as HasCapabilities ). getCapabilities (). getPlatformName ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ))
}
}
Elemento Designado
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
/examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.Platform ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
public class KeysTest extends BaseChromeTest {
@Test
public void keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ));
}
@Test
public void keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
driver . findElement ( By . tagName ( "body" )). click ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ));
}
@Test
public void copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
}
}
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform () /examples/python/tests/actions_api/test_keys.py
Copy
Close
import sys
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.by import By
def test_key_down ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "ABC"
def test_key_up ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "Ab"
def test_send_keys_to_active_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_send_keys_to_designated_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
driver . find_element ( By . TAG_NAME , "body" ) . click ()
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_copy_and_paste ( firefox_driver ):
driver = firefox_driver
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "SeleniumSelenium!"
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver ) /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseFirefoxTest
{
[TestMethod]
public void KeyDown ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "A" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void KeyUp ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "Ab" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToActiveElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. SendKeys ( "abc" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToDesignatedElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
. SendKeys ( textField , "abc" )
. Perform ();
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void CopyAndPaste ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
. KeyUp ( Keys . Shift )
. KeyDown ( cmdCtrl )
. SendKeys ( "xvv" )
. KeyUp ( cmdCtrl )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "SeleniumSelenium!" , textField . GetAttribute ( "value" ));
}
}
} text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform /examples/ruby/spec/actions_api/keys_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Keys' do
let ( :driver ) { start_session }
let ( :wait ) { Selenium :: WebDriver :: Wait . new ( timeout : 2 ) }
it 'key down' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'A'
end
it 'key up' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'Ab'
end
it 'sends keys to active element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. send_keys ( 'abc' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'abc'
end
it 'sends keys to designated element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
driver . find_element ( tag_name : 'body' ) . click
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
expect ( text_field . attribute ( 'value' )) . to eq 'Selenium!'
end
it 'copy and paste' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'SeleniumSelenium!'
end
end
Selenium v4.5.0
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
/examples/javascript/test/actionsApi/keysTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
const { platform } = require ( 'node:process' )
describe ( 'Keyboard Action - Keys test' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'KeyDown' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
const textField = driver . findElement ( By . id ( 'textInput' ))
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'A' )
})
it ( 'KeyUp' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'Ab' )
})
it ( 'sendKeys' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Designated Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . findElement ( By . css ( 'body' )). click ()
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Copy and Paste' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = await driver . findElement ( By . id ( 'textInput' ))
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'SeleniumSelenium!' )
})
})
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.HasCapabilities
import org.openqa.selenium.Keys
import org.openqa.selenium.Platform
import org.openqa.selenium.interactions.Actions
class KeysTest : BaseTest () {
@Test
fun keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ))
}
@Test
fun keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
driver . findElement ( By . tagName ( "body" )). click ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ))
}
@Test
fun copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
val platformName = ( driver as HasCapabilities ). getCapabilities (). getPlatformName ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ))
}
}
Copiar e Colar Aqui está um exemplo de uso de todos os métodos acima para realizar uma ação de copiar/colar. Note que a tecla a ser usada para essa operação será diferente, dependendo se for um sistema Mac OS ou não. Este código resultará no texto: SeleniumSelenium!
Java
Python
CSharp
Ruby
JavaScript
Kotlin Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" )); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Keys ;
import org.openqa.selenium.Platform ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
public class KeysTest extends BaseChromeTest {
@Test
public void keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ));
}
@Test
public void keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ));
}
@Test
public void sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
driver . findElement ( By . tagName ( "body" )). click ();
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ));
}
@Test
public void copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" );
Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
}
}
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform () /examples/python/tests/actions_api/test_keys.py
Copy
Close
import sys
from selenium.webdriver import Keys , ActionChains
from selenium.webdriver.common.by import By
def test_key_down ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "ABC"
def test_key_up ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "Ab"
def test_send_keys_to_active_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_send_keys_to_designated_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
driver . find_element ( By . TAG_NAME , "body" ) . click ()
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "abc"
def test_copy_and_paste ( firefox_driver ):
driver = firefox_driver
driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' )
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
assert driver . find_element ( By . ID , "textInput" ) . get_attribute ( 'value' ) == "SeleniumSelenium!"
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp ) /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Copy
Close
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseFirefoxTest
{
[TestMethod]
public void KeyDown ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "A" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void KeyUp ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "Ab" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToActiveElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
new Actions ( driver )
. SendKeys ( "abc" )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void SendKeysToDesignatedElement ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
. SendKeys ( textField , "abc" )
. Perform ();
Assert . AreEqual ( "abc" , textField . GetAttribute ( "value" ));
}
[TestMethod]
public void CopyAndPaste ()
{
driver . Url = "https://selenium.dev/selenium/web/single_text_input.html" ;
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
. KeyUp ( Keys . Shift )
. KeyDown ( cmdCtrl )
. SendKeys ( "xvv" )
. KeyUp ( cmdCtrl )
. Perform ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
Assert . AreEqual ( "SeleniumSelenium!" , textField . GetAttribute ( "value" ));
}
}
} driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
/examples/ruby/spec/actions_api/keys_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Keys' do
let ( :driver ) { start_session }
let ( :wait ) { Selenium :: WebDriver :: Wait . new ( timeout : 2 ) }
it 'key down' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'A'
end
it 'key up' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'Ab'
end
it 'sends keys to active element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
driver . action
. send_keys ( 'abc' )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'abc'
end
it 'sends keys to designated element' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
driver . find_element ( tag_name : 'body' ) . click
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
expect ( text_field . attribute ( 'value' )) . to eq 'Selenium!'
end
it 'copy and paste' do
driver . get 'https://www.selenium.dev/selenium/web/single_text_input.html'
wait . until { driver . find_element ( id : 'textInput' ) . attribute ( 'autofocus' ) }
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
expect ( driver . find_element ( id : 'textInput' ) . attribute ( 'value' )) . to eq 'SeleniumSelenium!'
end
end
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
/examples/javascript/test/actionsApi/keysTest.spec.js
Copy
Close
const { By , Key , Browser , Builder } = require ( 'selenium-webdriver' )
const assert = require ( 'assert' )
const { platform } = require ( 'node:process' )
describe ( 'Keyboard Action - Keys test' , function () {
let driver
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
})
after ( async () => await driver . quit ())
it ( 'KeyDown' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
const textField = driver . findElement ( By . id ( 'textInput' ))
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'A' )
})
it ( 'KeyUp' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'Ab' )
})
it ( 'sendKeys' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
await driver . actions ()
. sendKeys ( 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Designated Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
await driver . findElement ( By . css ( 'body' )). click ()
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'abc' )
})
it ( 'Copy and Paste' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' )
const textField = await driver . findElement ( By . id ( 'textInput' ))
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
assert . deepStrictEqual ( await textField . getAttribute ( 'value' ), 'SeleniumSelenium!' )
})
})
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.HasCapabilities
import org.openqa.selenium.Keys
import org.openqa.selenium.Platform
import org.openqa.selenium.interactions.Actions
class KeysTest : BaseTest () {
@Test
fun keyDown () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "A" , textField . getAttribute ( "value" ))
}
@Test
fun keyUp () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "Ab" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToActiveElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Assertions . assertEquals ( "abc" , textField . getAttribute ( "value" ))
}
@Test
fun sendKeysToDesignatedElement () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
driver . findElement ( By . tagName ( "body" )). click ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Assertions . assertEquals ( "Selenium!" , textField . getAttribute ( "value" ))
}
@Test
fun copyAndPaste () {
driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" )
val platformName = ( driver as HasCapabilities ). getCapabilities (). getPlatformName ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ))
}
}
2 - Ações do Mouse Uma representação de qualquer dispositivo de ponteiro para interagir com uma página da web.
Existem apenas 3 ações que podem ser realizadas com um mouse: pressionar um botão, liberar um botão pressionado e mover o mouse. O Selenium fornece métodos de conveniência que combinam essas ações da maneira mais comum.
Clicar e Manter Pressionado Este método combina mover o mouse para o centro de um elemento com a pressão do botão esquerdo do mouse. Isso é útil para focar em um elemento específico:
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
let clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : clickable }). press (). perform (); /examples/javascript/test/actionsApi/mouse/clickAndHold.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
describe ( 'Click and hold' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Mouse move and mouseDown on an element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
let clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : clickable }). press (). perform ();
});
});
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Clicar e Liberar Este método combina mover o mouse para o centro de um elemento com a pressão e liberação do botão esquerdo do mouse. Isso é conhecido como “clicar”:
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
let click = driver . findElement ( By . id ( "click" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : click }). click (). perform (); /examples/javascript/test/actionsApi/mouse/clickAndRelease.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
describe ( 'Click and release' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Mouse move and click on an element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
let click = driver . findElement ( By . id ( "click" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : click }). click (). perform ();
});
});
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Existem um total de 5 botões definidos para um mouse:
0 — Botão Esquerdo (o padrão) 1 — Botão do Meio (atualmente não suportado) 2 — Botão Direito 3 — Botão X1 (Voltar) 4 — Botão X2 (Avançar) Context Click Este método combina mover o mouse para o centro de um elemento com a pressão e liberação do botão direito do mouse (botão 2). Isso é conhecido como “clicar com o botão direito” ou “menu de contexto”
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . contextClick ( clickable ). perform (); /examples/javascript/test/actionsApi/mouse/rightClick.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Right click' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Mouse move and right click on an element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . contextClick ( clickable ). perform ();
await driver . sleep ( 500 );
const clicked = await driver . findElement ( By . id ( 'click-status' )). getText ();
assert . deepStrictEqual ( clicked , `context-clicked` )
});
});
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Click botão de voltar do mouse Este termo pode se referir a um clique com o botão X1 (botão de voltar) do mouse. No entanto, essa terminologia específica pode variar dependendo do contexto.
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
Selenium v4.2
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
Selenium v4.2
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} Selenium v4.2
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
Selenium v4.5.0
const actions = driver . actions ({ async : true });
await actions . press ( Button . BACK ). release ( Button . BACK ). perform () /examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js
Copy
Close
const { By , Button , Browser , Builder } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Should be able to perform BACK click and FORWARD click' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Back click' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
await driver . findElement ( By . id ( "click" )). click ();
assert . deepStrictEqual ( await driver . getTitle (), `We Arrive Here` )
const actions = driver . actions ({ async : true });
await actions . press ( Button . BACK ). release ( Button . BACK ). perform ()
assert . deepStrictEqual ( await driver . getTitle (), `BasicMouseInterfaceTest` )
});
it ( 'Forward click' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
await driver . findElement ( By . id ( "click" )). click ();
await driver . navigate (). back ();
assert . deepStrictEqual ( await driver . getTitle (), `BasicMouseInterfaceTest` )
const actions = driver . actions ({ async : true });
await actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform ()
assert . deepStrictEqual ( await driver . getTitle (), `We Arrive Here` )
});
});
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
botão de avançar) do mouse Este termo se refere a um clique com o botão X2 (botão de avançar) do mouse. Não existe um método de conveniência específico para essa ação, sendo apenas a pressão e liberação do botão do mouse de número 4.
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
Selenium v4.2
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
Selenium v4.2
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} Selenium v4.2
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
Selenium v4.5.0
const actions = driver . actions ({ async : true });
await actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform () /examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js
Copy
Close
const { By , Button , Browser , Builder } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Should be able to perform BACK click and FORWARD click' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Back click' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
await driver . findElement ( By . id ( "click" )). click ();
assert . deepStrictEqual ( await driver . getTitle (), `We Arrive Here` )
const actions = driver . actions ({ async : true });
await actions . press ( Button . BACK ). release ( Button . BACK ). perform ()
assert . deepStrictEqual ( await driver . getTitle (), `BasicMouseInterfaceTest` )
});
it ( 'Forward click' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
await driver . findElement ( By . id ( "click" )). click ();
await driver . navigate (). back ();
assert . deepStrictEqual ( await driver . getTitle (), `BasicMouseInterfaceTest` )
const actions = driver . actions ({ async : true });
await actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform ()
assert . deepStrictEqual ( await driver . getTitle (), `We Arrive Here` )
});
});
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Duplo click Este método combina mover o mouse para o centro de um elemento com a pressão e liberação do botão esquerdo do mouse duas vezes. Isso é conhecido como “duplo clique”.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . doubleClick ( clickable ). perform (); /examples/javascript/test/actionsApi/mouse/doubleClick.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'Double click' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Double-click on an element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . doubleClick ( clickable ). perform ();
await driver . sleep ( 500 );
const status = await driver . findElement ( By . id ( 'click-status' )). getText ();
assert . deepStrictEqual ( status , `double-clicked` )
});
});
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Mover para o Elemento Este método move o mouse para o ponto central do elemento que está visível na tela. Isso é conhecido como “hovering” ou “pairar”. É importante observar que o elemento deve estar no viewport (área visível na tela) ou então o comando resultará em erro.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const hoverable = driver . findElement ( By . id ( "hover" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : hoverable }). perform (); /examples/javascript/test/actionsApi/mouse/moveToElement.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'Move to element' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'Mouse move into an element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const hoverable = driver . findElement ( By . id ( "hover" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : hoverable }). perform ();
const status = await driver . findElement ( By . id ( 'move-status' )). getText ();
assert . deepStrictEqual ( status , `hovered` )
});
});
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Mover por Deslocamento Esses métodos primeiro movem o mouse para a origem designada e, em seguida, pelo número de pixels especificado no deslocamento fornecido. É importante observar que a posição do mouse deve estar dentro da janela de visualização (viewport) ou, caso contrário, o comando resultará em erro.
Deslocamento a partir do Elemento Este método move o mouse para o ponto central do elemento visível na tela e, em seguida, move o mouse pelo deslocamento fornecido.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const mouseTracker = driver . findElement ( By . id ( "mouse-tracker" ));
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 , origin : mouseTracker }). perform (); /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
const { By , Origin , Builder , until } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Mouse move by offset' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'From element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const mouseTracker = driver . findElement ( By . id ( "mouse-tracker" ));
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 , origin : mouseTracker }). perform ();
await driver . wait ( until . elementTextContains ( await driver . findElement ( By . id ( 'relative-location' )), "," ), 2000 );
let result = await driver . findElement ( By . id ( 'relative-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 100 - 8 ) < 2 ), true )
});
it ( 'From viewport origin' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 }). perform ();
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 8 ) < 2 ), true )
});
it ( 'From current pointer location' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 6 , y : 3 }). perform ()
await actions . move ({ x : 13 , y : 15 , origin : Origin . POINTER }). perform ()
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ]) - 6 - 13 ) < 2 , true )
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ]) - 3 - 15 ) < 2 , true )
});
});
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Deslocamento a partir da Janela de Visualização Este método move o mouse a partir do canto superior esquerdo da janela de visualização atual pelo deslocamento fornecido.
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} driver . action
. move_to_location ( 8 , 12 )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 }). perform (); /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
const { By , Origin , Builder , until } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Mouse move by offset' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'From element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const mouseTracker = driver . findElement ( By . id ( "mouse-tracker" ));
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 , origin : mouseTracker }). perform ();
await driver . wait ( until . elementTextContains ( await driver . findElement ( By . id ( 'relative-location' )), "," ), 2000 );
let result = await driver . findElement ( By . id ( 'relative-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 100 - 8 ) < 2 ), true )
});
it ( 'From viewport origin' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 }). perform ();
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 8 ) < 2 ), true )
});
it ( 'From current pointer location' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 6 , y : 3 }). perform ()
await actions . move ({ x : 13 , y : 15 , origin : Origin . POINTER }). perform ()
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ]) - 6 - 13 ) < 2 , true )
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ]) - 3 - 15 ) < 2 , true )
});
});
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Deslocamento a partir da Localização Atual do Ponteiro Este método move o mouse a partir de sua posição atual pelo deslocamento fornecido pelo usuário. Se o mouse não tiver sido movido anteriormente, a posição será no canto superior esquerdo da janela de visualização. É importante notar que a posição do ponteiro não muda quando a página é rolada.
Observe que o primeiro argumento, X, especifica o movimento para a direita quando positivo, enquanto o segundo argumento, Y, especifica o movimento para baixo quando positivo. Portanto, moveByOffset(30, -10) move o mouse 30 unidades para a direita e 10 unidades para cima a partir da posição atual do mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} driver . action
. move_by ( 13 , 15 )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
await actions . move ({ x : 13 , y : 15 , origin : Origin . POINTER }). perform () /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
const { By , Origin , Builder , until } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Mouse move by offset' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'From element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const mouseTracker = driver . findElement ( By . id ( "mouse-tracker" ));
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 , origin : mouseTracker }). perform ();
await driver . wait ( until . elementTextContains ( await driver . findElement ( By . id ( 'relative-location' )), "," ), 2000 );
let result = await driver . findElement ( By . id ( 'relative-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 100 - 8 ) < 2 ), true )
});
it ( 'From viewport origin' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 }). perform ();
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ]) - 8 ) < 2 ), true )
});
it ( 'From current pointer location' , async function () {
await driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' );
const actions = driver . actions ({ async : true });
await actions . move ({ x : 6 , y : 3 }). perform ()
await actions . move ({ x : 13 , y : 15 , origin : Origin . POINTER }). perform ()
let result = await driver . findElement ( By . id ( 'absolute-location' )). getText ();
result = result . split ( ', ' );
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ]) - 6 - 13 ) < 2 , true )
assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ]) - 3 - 15 ) < 2 , true )
});
});
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Arrastar e Soltar no Elemento Este método primeiro realiza um clique e mantém pressionado no elemento de origem, move para a localização do elemento de destino e, em seguida, libera o botão do mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform (); /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
using System ;
using System.Drawing ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Interactions ;
namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseChromeTest
{
[TestMethod]
public void ClickAndHold ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
Assert . AreEqual ( "focused" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void ClickAndRelease ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" ));
}
[TestMethod]
public void RightClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
Assert . AreEqual ( "context-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void BackClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
Assert . AreEqual ( "We Arrive Here" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
}
[TestMethod]
public void ForwardClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
driver . FindElement ( By . Id ( "click" )). Click ();
driver . Navigate (). Back ();
Assert . AreEqual ( "BasicMouseInterfaceTest" , driver . Title );
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Assert . AreEqual ( "We Arrive Here" , driver . Title );
}
[TestMethod]
public void DoubleClick ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
Assert . AreEqual ( "double-clicked" , driver . FindElement ( By . Id ( "click-status" )). Text );
}
[TestMethod]
public void Hovers ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
Assert . AreEqual ( "hovered" , driver . FindElement ( By . Id ( "move-status" )). Text );
}
[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCenterOfElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 100 - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromViewport ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 ) < 2 );
}
[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 12 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
string [] result = driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ]) - 8 - 13 ) < 2 );
Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ]) - 12 - 15 ) < 2 );
}
[TestMethod]
public void DragToElement ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
[TestMethod]
public void DragByOffset ()
{
driver . Url = "https://selenium.dev/selenium/web/mouse_interaction.html" ;
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
Assert . AreEqual ( "dropped" , driver . FindElement ( By . Id ( "drop-status" )). Text );
}
}
} draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform /examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
# frozen_string_literal: true
require 'spec_helper'
RSpec . describe 'Mouse' do
let ( :driver ) { start_session }
it 'click and hold' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'focused'
end
it 'click and release' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
expect ( driver . current_url ) . to include 'resultPage.html'
end
describe 'alternate button clicks' do
it 'right click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'context-clicked'
end
it 'back click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
expect ( driver . title ) . to eq ( 'We Arrive Here' )
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
end
it 'forward click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . find_element ( id : 'click' ) . click
driver . navigate . back
expect ( driver . title ) . to eq ( 'BasicMouseInterfaceTest' )
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
expect ( driver . title ) . to eq ( 'We Arrive Here' )
end
end
it 'double click' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
expect ( driver . find_element ( id : 'click-status' ) . text ) . to eq 'double-clicked'
end
it 'hovers' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
expect ( driver . find_element ( id : 'move-status' ) . text ) . to eq 'hovered'
end
describe 'move by offset' do
it 'offset from element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . scroll_to ( driver . find_element ( id : 'bottom' )) . perform
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
rect = mouse_tracker . rect
center_x = rect . width / 2
center_y = rect . height / 2
x_coord , y_coord = driver . find_element ( id : 'relative-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( center_x + 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( center_y + 11 )
end
it 'offset from viewport' , { platforn : :linux , reason : 'it only fails on the linux pipeline' } do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action
. move_to_location ( 8 , 12 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 12 )
end
it 'offset from current pointer location' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
driver . action . move_to_location ( 8 , 11 ) . perform
driver . action
. move_by ( 13 , 15 )
. perform
x_coord , y_coord = driver . find_element ( id : 'absolute-location' ) . text . split ( ',' ) . map ( & :to_i )
expect ( x_coord ) . to be_within ( 1 ) . of ( 8 + 13 )
expect ( y_coord ) . to be_within ( 1 ) . of ( 11 + 15 )
end
end
it 'drags to another element' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
it 'drags by an offset' do
driver . get 'https://www.selenium.dev/selenium/web/mouse_interaction.html'
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
expect ( driver . find_element ( id : 'drop-status' ) . text ) . to include ( 'dropped' )
end
end
const draggable = driver . findElement ( By . id ( "draggable" ));
const droppable = await driver . findElement ( By . id ( "droppable" ));
const actions = driver . actions ({ async : true });
await actions . dragAndDrop ( draggable , droppable ). perform (); /examples/javascript/test/actionsApi/mouse/dragAndDrop.spec.js
Copy
Close
const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( 'assert' );
describe ( 'Drag and Drop' , function () {
let driver ;
before ( async function () {
driver = new Builder (). forBrowser ( 'chrome' ). build ();
});
after ( async () => await driver . quit ());
it ( 'By Offset' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const draggable = driver . findElement ( By . id ( "draggable" ));
let start = await draggable . getRect ();
let finish = await driver . findElement ( By . id ( "droppable" )). getRect ();
const actions = driver . actions ({ async : true });
await actions . dragAndDrop ( draggable , { x : finish . x - start . x , y : finish . y - start . y }). perform ();
let result = await driver . findElement ( By . id ( "drop-status" )). getText ();
assert . deepStrictEqual ( 'dropped' , result )
});
it ( 'Onto Element' , async function () {
await driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' );
const draggable = driver . findElement ( By . id ( "draggable" ));
const droppable = await driver . findElement ( By . id ( "droppable" ));
const actions = driver . actions ({ async : true });
await actions . dragAndDrop ( draggable , droppable ). perform ();
let result = await driver . findElement ( By . id ( "drop-status" )). getText ();
assert . deepStrictEqual ( 'dropped' , result )
});
});
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ()) /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
package dev.selenium.actions_api
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import org.openqa.selenium.remote.RemoteWebDriver
import java.time.Duration
import java.util.Collections
class MouseTest : BaseTest () {
@Test
fun clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))
}
@Test
fun rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ())
}
@Test
fun forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . findElement ( By . id ( "click" )). click ()
driver . navigate (). back ()
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ())
}
@Test
fun doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ())
}
@Test
fun hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ())
}
@Test
fun moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
driver . manage (). window (). fullscreen ()
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
val result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 100 - 8 ) < 2 )
}
@Test
fun moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 12 ) < 2 )
}
@Test
fun moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
val result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ]) - 8 - 13 ) < 2 )
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ]) - 11 - 15 ) < 2 )
}
@Test
fun dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
@Test
fun dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" )
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ())
}
}
Arrastar e Soltar pelo Deslocamento Este método primeiro realiza um clique e mantém pressionado no elemento de origem, move para o deslocamento fornecido e, em seguida, libera o botão do mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
package dev.selenium.actions_api ;
import dev.selenium.BaseChromeTest ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.Rectangle ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.interactions.Actions ;
import org.openqa.selenium.interactions.PointerInput ;
import org.openqa.selenium.interactions.Sequence ;
import org.openqa.selenium.remote.RemoteWebDriver ;
import java.time.Duration ;
import java.util.Collections ;
public class MouseTest extends BaseChromeTest {
@Test
public void clickAndHold () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
Assertions . assertEquals ( "focused" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void clickAndRelease () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" ));
}
@Test
public void rightClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
Assertions . assertEquals ( "context-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void backClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
Assertions . assertEquals ( driver . getTitle (), "We Arrive Here" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "BasicMouseInterfaceTest" , driver . getTitle ());
}
@Test
public void forwardClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . findElement ( By . id ( "click" )). click ();
driver . navigate (). back ();
Assertions . assertEquals ( driver . getTitle (), "BasicMouseInterfaceTest" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Assertions . assertEquals ( "We Arrive Here" , driver . getTitle ());
}
@Test
public void doubleClick () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
Assertions . assertEquals ( "double-clicked" , driver . findElement ( By . id ( "click-status" )). getText ());
}
@Test
public void hovers () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
Assertions . assertEquals ( "hovered" , driver . findElement ( By . id ( "move-status" )). getText ());
}
@Test
public void moveByOffsetFromElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
driver . manage (). window (). fullscreen ();
WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
String [] result = driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 100 - 8 ) < 2 );
}
@Test
public void moveByOffsetFromViewport () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 12 ) < 2 );
}
@Test
public void moveByOffsetFromCurrentPointer () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 11 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
String [] result = driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] ) - 8 - 13 ) < 2 );
Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] ) - 11 - 15 ) < 2 );
}
@Test
public void dragsToElement () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
@Test
public void dragsByOffset () {
driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" );
WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
Assertions . assertEquals ( "dropped" , driver . findElement ( By . id ( "drop-status" )). getText ());
}
}
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform () /examples/python/tests/actions_api/test_mouse.py
Copy
Close
import pytest
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_click_and_hold ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "focused"
def test_click_and_release ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
assert "resultPage.html" in driver . current_url
def test_right_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
sleep ( 0.5 )
assert driver . find_element ( By . ID , "click-status" ) . text == "context-clicked"
def test_back_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
assert driver . title == "We Arrive Here"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
assert driver . title == "BasicMouseInterfaceTest"
def test_forward_click_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
driver . find_element ( By . ID , "click" ) . click ()
driver . back ()
assert driver . title == "BasicMouseInterfaceTest"
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
assert driver . title == "We Arrive Here"
def test_double_click ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
assert driver . find_element ( By . ID , "click-status" ) . text == "double-clicked"
def test_hover ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
assert driver . find_element ( By . ID , "move-status" ) . text == "hovered"
def test_move_by_offset_from_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "relative-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 100 - 8 ) < 2
def test_move_by_offset_from_viewport_origin_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
WebDriverWait ( driver , 10 ) . until ( EC . presence_of_element_located (( By . ID , "absolute-location" )))
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 8 ) < 2
def test_move_by_offset_from_current_pointer_ab ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 6 , 3 )
action . perform ()
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
coordinates = driver . find_element ( By . ID , "absolute-location" ) . text . split ( ", " )
assert abs ( int ( coordinates [ 0 ]) - 6 - 13 ) < 2
assert abs ( int ( coordinates [ 1 ]) - 3 - 15 ) < 2
def test_drag_and_drop_onto_element ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"
def test_drag_and_drop_by_offset ( driver ):
driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' )
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
assert driver . find_element ( By . ID , "drop-status" ) . text == "dropped"