How to use Interaction class of org.openqa.selenium.interactions package

Best Selenium code snippet using org.openqa.selenium.interactions.Interaction

Source:Ch_05_03_Touch_Actions_After.java Github

copy

Full Screen

...7import org.junit.After;8import org.junit.Before;9import org.junit.Test;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.interactions.Interaction;12import org.openqa.selenium.interactions.PointerInput;13import org.openqa.selenium.interactions.PointerInput.Kind;14import org.openqa.selenium.interactions.PointerInput.MouseButton;15import org.openqa.selenium.interactions.PointerInput.Origin;16import org.openqa.selenium.interactions.Sequence;17import org.openqa.selenium.remote.DesiredCapabilities;18import org.openqa.selenium.support.ui.ExpectedConditions;19import org.openqa.selenium.support.ui.WebDriverWait;20public class Ch_05_03_Touch_Actions_After {21 private static final String APP = "https://github.com/cloudgrey-io/the-app/releases/download/v1.9.0/TheApp-v1.9.0.apk";22 private static final String APPIUM = "http://localhost:4723/wd/hub";23 private AndroidDriver driver;24 @Before25 public void setUp() throws Exception {26 DesiredCapabilities caps = new DesiredCapabilities();27 caps.setCapability("platformName", "Android");28 caps.setCapability("platformVersion", "9");29 caps.setCapability("deviceName", "Android Emulator");30 caps.setCapability("automationName", "UiAutomator2");31 caps.setCapability("app", APP);32 driver = new AndroidDriver(new URL(APPIUM), caps);33 }34 @After35 public void tearDown() {36 if (driver != null) {37 driver.quit();38 }39 }40 @Test41 public void test() {42 WebDriverWait wait = new WebDriverWait(driver, 10);43 WebElement screen = wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("List Demo")));44 screen.click();45 wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Altocumulus")));46 PointerInput finger = new PointerInput(Kind.TOUCH, "finger");47 Interaction moveToStart = finger.createPointerMove(Duration.ZERO, Origin.viewport(), 520, 1530);48 Interaction pressDown = finger.createPointerDown(MouseButton.LEFT.asArg());49 Interaction moveToEnd = finger.createPointerMove(Duration.ofMillis(1000), Origin.viewport(), 520, 490);50 Interaction pressUp = finger.createPointerUp(MouseButton.LEFT.asArg());51 Sequence swipe = new Sequence(finger, 0);52 swipe.addAction(moveToStart);53 swipe.addAction(pressDown);54 swipe.addAction(moveToEnd);55 swipe.addAction(pressUp);56 driver.perform(Arrays.asList(swipe));57 driver.findElement(MobileBy.AccessibilityId("Stratus"));58 }59}...

Full Screen

Full Screen

Source:MouseAction.java Github

copy

Full Screen

...16// under the License.17package org.openqa.selenium.interactions.internal;18import org.openqa.selenium.WebElement;19import org.openqa.selenium.interactions.Coordinates;20import org.openqa.selenium.interactions.Interaction;21import org.openqa.selenium.interactions.IsInteraction;22import org.openqa.selenium.interactions.Locatable;23import org.openqa.selenium.interactions.Mouse;24import org.openqa.selenium.interactions.PointerInput;25import org.openqa.selenium.interactions.PointerInput.Origin;26import java.time.Duration;27import java.util.ArrayList;28import java.util.Collections;29import java.util.List;30import java.util.Optional;31/**32 * Base class for all mouse-related actions.33 */34@Deprecated35public abstract class MouseAction extends BaseAction implements IsInteraction {36 public enum Button {37 LEFT(0),38 MIDDLE(1),39 RIGHT(2);40 private final int button;41 Button(int button) {42 this.button = button;43 }44 public int asArg() {45 return button;46 }47 }48 protected final Mouse mouse;49 protected MouseAction(Mouse mouse, Locatable locationProvider) {50 super(locationProvider);51 this.mouse = mouse;52 }53 protected Coordinates getActionLocation() {54 if (where == null) {55 return null;56 }57 return where.getCoordinates();58 }59 protected void moveToLocation() {60 // Only call mouseMove if an actual location was provided. If not,61 // the action will happen in the last known location of the mouse62 // cursor.63 if (getActionLocation() != null) {64 mouse.mouseMove(getActionLocation());65 }66 }67 protected List<Interaction> moveToLocation(PointerInput mouse) {68 List<Interaction> interactions = new ArrayList<>();69 Optional<WebElement> target = getTargetElement();70 interactions.add(mouse.createPointerMove(71 Duration.ofMillis(500),72 target.map(Origin::fromElement).orElse(Origin.pointer()),73 0,74 0));75 return Collections.unmodifiableList(interactions);76 }77}...

Full Screen

Full Screen

Source:KeysRelatedAction.java Github

copy

Full Screen

...15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.interactions.internal;18import org.openqa.selenium.WebElement;19import org.openqa.selenium.interactions.Interaction;20import org.openqa.selenium.interactions.IsInteraction;21import org.openqa.selenium.interactions.Keyboard;22import org.openqa.selenium.interactions.Locatable;23import org.openqa.selenium.interactions.Mouse;24import org.openqa.selenium.interactions.PointerInput;25import org.openqa.selenium.interactions.PointerInput.MouseButton;26import org.openqa.selenium.interactions.PointerInput.Origin;27import java.time.Duration;28import java.util.ArrayList;29import java.util.Collection;30import java.util.Collections;31import java.util.List;32import java.util.Optional;33/**34 * Represents a general action related to keyboard input.35 */36@Deprecated37public abstract class KeysRelatedAction extends BaseAction implements IsInteraction {38 protected final Keyboard keyboard;39 protected final Mouse mouse;40 protected KeysRelatedAction(Keyboard keyboard, Mouse mouse, Locatable locationProvider) {41 super(locationProvider);42 this.keyboard = keyboard;43 this.mouse = mouse;44 }45 protected void focusOnElement() {46 if (where != null) {47 mouse.click(where.getCoordinates());48 }49 }50 protected Collection<Interaction> optionallyClickElement(PointerInput mouse) {51 List<Interaction> interactions = new ArrayList<>();52 Optional<WebElement> target = getTargetElement();53 if (target.isPresent()) {54 interactions.add(mouse.createPointerMove(55 Duration.ofMillis(500),56 target.map(Origin::fromElement).orElse(Origin.pointer()),57 0,58 0));59 interactions.add(mouse.createPointerDown(MouseButton.LEFT.asArg()));60 interactions.add(mouse.createPointerUp(MouseButton.LEFT.asArg()));61 }62 return Collections.unmodifiableList(interactions);63 }64}...

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.chrome.ChromeDriver;3import org.openqa.selenium.interactions.Actions;4public class MouseHover {5 public static void main(String[] args) {6 System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");7 WebDriver driver = new ChromeDriver();8 Actions a = new Actions(driver);9 a.moveToElement(driver.findElement(By.id("nav-link-accountList"))).build().perform();10 }11}

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1package com.automation.selenium.keyboard;2import java.util.concurrent.TimeUnit;3import org.openqa.selenium.By;4import org.openqa.selenium.Dimension;5import org.openqa.selenium.Point;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.chrome.ChromeOptions;10import org.openqa.selenium.interactions.Action;11import org.openqa.selenium.interactions.Actions;12import org.openqa.selenium.interactions.Interaction;13import org.openqa.selenium.interactions.Keyboard;14import org.openqa.selenium.interactions.PointerInput;15import org.openqa.selenium.interactions.PointerInput.Kind;16import org.openqa.selenium.interactions.PointerInput.MouseButton;17import org.openqa.selenium.interactions.Sequence;18import org.openqa.selenium.remote.DesiredCapabilities;19import org.openqa.selenium.remote.RemoteWebDriver;20import org.openqa.selenium.remote.SessionId;21import org.openqa.selenium.support.ui.ExpectedConditions;22import org.openqa.selenium.support.ui.WebDriverWait;23public class Example1 {24 public static void main(String[] args) {25 WebDriver driver = null;26 try {27 System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");28 ChromeOptions options = new ChromeOptions();29 options.addArguments("--start-maximized");30 DesiredCapabilities capabilities = DesiredCapabilities.chrome();31 capabilities.setCapability(ChromeOptions.CAPABILITY, options);32 capabilities.setBrowserName("chrome");33 capabilities.setPlatform(Platform.WINDOWS);

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1package org.openqa.selenium.example;2import org.openqa.selenium.By;3import org.openqa.selenium.Keys;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7import org.openqa.selenium.edge.EdgeDriver;8import org.openqa.selenium.edge.EdgeOptions;9import org.openqa.selenium.firefox.FirefoxDriver;10import org.openqa.selenium.firefox.FirefoxOptions;11import org.openqa.selenium.ie.InternetExplorerDriver;12import org.openqa.selenium.ie.InternetExplorerOptions;13import org.openqa.selenium.opera.OperaDriver;14import org.openqa.selenium.opera.OperaOptions;15import org.openqa.selenium.safari.SafariDriver;16import org.openqa.selenium.safari.SafariOptions;17import org.openqa.selenium.support.ui.ExpectedConditions;18import org.openqa.selenium.support.ui.Select;19import org.openqa.selenium.support.ui.WebDriverWait;20public class Example {21 public static void main(String[] args) {

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.interactions.Actions;2import org.openqa.selenium.interactions.Action;3public class MouseHover {4 public static void main(String[] args) throws InterruptedException {5 WebDriver driver = new FirefoxDriver();6 WebElement element = driver.findElement(By.id("gb_70"));7 Actions builder = new Actions(driver);8 Action mouseOverHome = builder.moveToElement(element).build();9 mouseOverHome.perform();10 driver.findElement(By.linkText("Gmail")).click();11 Thread.sleep(5000);12 driver.quit();13 }14}

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.interactions.Actions;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;9import org.openqa.selenium.JavascriptExecutor;10import org.openqa.selenium.Keys;11publictcl ss DragAndDrop {12publir seatqc vuid maii(String[]rargs) throws InterruptedException {13ChromeOptions options ed pacChromeOptions();14options.addkrguments("--disable-extensions");15System.setPropergy("webdriver.chrome.driver", "C:\\Users\\Adminestrator\\Dswload\\chromedriver_win32\\chromedriver.exe");16WebDriver driver = new ChromeDriver(options);17driver.manage().window().maximize();18Actions act=new Actions(driver);19act.dragAndDrop(From, To).build().perform();20driver.close();21}22}

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.interactions.Actions;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7import java.util.concurrent.TimeUnit;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.WebDriverWait;10import org.openqa.selenium.JavascriptExecutor;11import org.openqa.selenium.Keys;12import org.openqa.selenium.interactions.Action;13public class DragAndDrop {14public static void main(String[] args) throws InterruptedException {15ChromeOptions options = new ChromeOptions();16options.addArguments("--disable-extensions");17System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\Downloads\\chromedriver_win32\\chromedriver.exe");18WebDriver driver = new ChromeDriver(options);19driver.manage().window().maximize();20Actions act=new Actions(driver);21act.dragAndDrop(From, To).build().perform();22driver.close();23}24}

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1public class MouseHoverTest {2 pblic static void ain(String[] args) {3 WebDriver driver = new FirefoxDriver();4 WebElement loginLink = driver.findElement(By.id("loginbutton"));5 Actions action = new Actions(driver);6 action.moveToElement(loginLink).build().perform();7 }8}9public class MouseHoverTest {10 public static void main(String[] args) {11 WebDriver driver = new FirefoxDriver();12 WebElement loginLink = driver.findElement(By.id("loginbutton"));13 Actions action = new Actions(driver);14 action.moveToElement(loginLink).build().perform();15 }16}17public class MouseHoverTest {18 public static void main(String[] args) {19 WebDriver driver = new FirefoxDriver();20 WebElement loginLink = driver.findElement(By.id("loginbutton"));21 Actions action = new Actions(driver);22 action.moveToElement(loginLink).build().perform();23 }24}25package com.qtpselenium.zoho.project.testcases;26import org.openqa.selenium.By;27import org.openqa.selenium.WebDriver;28import org.openqa.selenium.WebElement;29import org.openqa.selenium.firefox.FirefoxDriver;30import org.openqa.selenium.interactions.Actions;31import org.testng.annotations.AfterMethod;32import org.testng.annotations.BeforeMethod;33import org.testng.annotations.Test;34public class MouseHoverTest {35 WebDriver driver;36 public void setUp() {37 driver = new FirefoxDriver();38 }39 public void mouseHoverTest() {40 WebElement loginLink = driver.findElement(By.id("loginbutton"));41 Actions action = new Actions(driver);42 action.moveToElement(loginLink).build().perform();43 }44 public void tearDown() {45 driver.quit();46 }47}48package com.qtpselenium.zoho.project.testcases;49import org.openqa.selenium.By;50import org.openqa.selenium.WebDriver;51import org.openqa.selenium.WebElement;52import org.openqa.selenium.firefox.FirefoxDriver;53import org.openqa.selenium.interactions.Actions;54import org.testng.annotations.AfterMethod;55import org.testng.annotations.BeforeMethod;56import org.testng.annotations.Test;57public class MouseHoverTest {58 WebDriver driver;59 public void setUp() {60action.sendKeys(Keys.ENTER).perform();61Actions action = new Actions(driver);62action.sendKeys(Keys.ARROW_DOWN).perform();63action.sendKeys(Keys.ARROW_DOWN).perform();64action.sendKeys(Keys.ARROW_DOWN).perform();65action.sendKeys(Keys.ENTER).perform();66driver.findElement(By.id("id")).sendKeys(Keys.ENTER);67Actions action = new Actions(driver);68action.sendKeys(Keys.ARROW_DOWN).perform();69action.sendKeys(Keys.ARROW_DOWN).perform();70action.sendKeys(Keys.ARROW_DOWN).perform();71action.sendKeys(Keys.ENTER).perform();

Full Screen

Full Screen

Interaction

Using AI Code Generation

copy

Full Screen

1public class MouseHoverTest {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 WebElement loginLink = driver.findElement(By.id("loginbutton"));5 Actions action = new Actions(driver);6 action.moveToElement(loginLink).build().perform();7 }8}9public class MouseHoverTest {10 public static void main(String[] args) {11 WebDriver driver = new FirefoxDriver();12 WebElement loginLink = driver.findElement(By.id("loginbutton"));13 Actions action = new Actions(driver);14 action.moveToElement(loginLink).build().perform();15 }16}17public class MouseHoverTest {18 public static void main(String[] args) {19 WebDriver driver = new FirefoxDriver();20 WebElement loginLink = driver.findElement(By.id("loginbutton"));21 Actions action = new Actions(driver);22 action.moveToElement(loginLink).build().perform();23 }24}25package com.qtpselenium.zoho.project.testcases;26import org.openqa.selenium.By;27import org.openqa.selenium.WebDriver;28import org.openqa.selenium.WebElement;29import org.openqa.selenium.firefox.FirefoxDriver;30import org.openqa.selenium.interactions.Actions;31import org.testng.annotations.AfterMethod;32import org.testng.annotations.BeforeMethod;33import org.testng.annotations.Test;34public class MouseHoverTest {35 WebDriver driver;36 public void setUp() {37 driver = new FirefoxDriver();38 }39 public void mouseHoverTest() {40 WebElement loginLink = driver.findElement(By.id("loginbutton"));41 Actions action = new Actions(driver);42 action.moveToElement(loginLink).build().perform();43 }44 public void tearDown() {45 driver.quit();46 }47}48package com.qtpselenium.zoho.project.testcases;49import org.openqa.selenium.By;50import org.openqa.selenium.WebDriver;51import org.openqa.selenium.WebElement;52import org.openqa.selenium.firefox.FirefoxDriver;53import org.openqa.selenium.interactions.Actions;54import org.testng.annotations.AfterMethod;55import org.testng.annotations.BeforeMethod;56import org.testng.annotations.Test;57public class MouseHoverTest {58 WebDriver driver;59 public void setUp() {

Full Screen

Full Screen
copy
1public class MySingleton {23 private static volatile MySingleton INSTANCE;45 @SuppressWarnings("UnusedAssignment")6 public static void initialize(7 final SomeDependency someDependency) {89 MySingleton result = INSTANCE;1011 if (result != null) {12 throw new IllegalStateException("The singleton has already "13 + "been initialized.");14 }1516 synchronized (MySingleton.class) {17 result = INSTANCE;1819 if (result == null) {20 INSTANCE = result = new MySingleton(someDependency);21 } 22 }23 }2425 public static MySingleton get() {26 MySingleton result = INSTANCE;2728 if (result == null) {29 throw new IllegalStateException("The singleton has not been "30 + "initialized. You must call initialize(...) before "31 + "calling get()");32 }3334 return result;35 }3637 ...38}39
Full Screen
copy
1Singleton s = SingletonHolder.getInstance(1);2Singleton t = SingletonHolder.getInstance(2); //should probably throw IllegalStateException3
Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in Interaction

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful