How to use getActivePointer method of org.openqa.selenium.interactions.Actions class

Best Selenium code snippet using org.openqa.selenium.interactions.Actions.getActivePointer

Source:Actions.java Github

copy

Full Screen

...209 if (isBuildingActions()) {210 action.addAction(new ClickAndHoldAction(jsonMouse, (Locatable) target));211 }212 return moveInTicks(target, 0, 0)213 .tick(getActivePointer().createPointerDown(LEFT.asArg()));214 }215 /**216 * Clicks (without releasing) at the current mouse location.217 * @return A self reference.218 */219 public Actions clickAndHold() {220 if (isBuildingActions()) {221 action.addAction(new ClickAndHoldAction(jsonMouse, null));222 }223 return tick(getActivePointer().createPointerDown(LEFT.asArg()));224 }225 /**226 * Releases the depressed left mouse button, in the middle of the given element.227 * This is equivalent to:228 * <i>Actions.moveToElement(onElement).release()</i>229 *230 * Invoking this action without invoking {@link #clickAndHold()} first will result in231 * undefined behaviour.232 *233 * @param target Element to release the mouse button above.234 * @return A self reference.235 */236 public Actions release(WebElement target) {237 if (isBuildingActions()) {238 action.addAction(new ButtonReleaseAction(jsonMouse, (Locatable) target));239 }240 return moveInTicks(target, 0, 0).tick(getActivePointer().createPointerUp(LEFT.asArg()));241 }242 /**243 * Releases the depressed left mouse button at the current mouse location.244 * @see #release(org.openqa.selenium.WebElement)245 * @return A self reference.246 */247 public Actions release() {248 if (isBuildingActions()) {249 action.addAction(new ButtonReleaseAction(jsonMouse, null));250 }251 return tick(getActivePointer().createPointerUp(Button.LEFT.asArg()));252 }253 /**254 * Clicks in the middle of the given element. Equivalent to:255 * <i>Actions.moveToElement(onElement).click()</i>256 *257 * @param target Element to click.258 * @return A self reference.259 */260 public Actions click(WebElement target) {261 if (isBuildingActions()) {262 action.addAction(new ClickAction(jsonMouse, (Locatable) target));263 }264 return moveInTicks(target, 0, 0).clickInTicks(LEFT);265 }266 /**267 * Clicks at the current mouse location. Useful when combined with268 * {@link #moveToElement(org.openqa.selenium.WebElement, int, int)} or269 * {@link #moveByOffset(int, int)}.270 * @return A self reference.271 */272 public Actions click() {273 if (isBuildingActions()) {274 action.addAction(new ClickAction(jsonMouse, null));275 }276 return clickInTicks(LEFT);277 }278 private Actions clickInTicks(PointerInput.MouseButton button) {279 tick(getActivePointer().createPointerDown(button.asArg()));280 tick(getActivePointer().createPointerUp(button.asArg()));281 return this;282 }283 private Actions focusInTicks(WebElement target) {284 return moveInTicks(target, 0, 0).clickInTicks(LEFT);285 }286 /**287 * Performs a double-click at middle of the given element. Equivalent to:288 * <i>Actions.moveToElement(element).doubleClick()</i>289 *290 * @param target Element to move to.291 * @return A self reference.292 */293 public Actions doubleClick(WebElement target) {294 if (isBuildingActions()) {295 action.addAction(new DoubleClickAction(jsonMouse, (Locatable) target));296 }297 return moveInTicks(target, 0, 0)298 .clickInTicks(LEFT)299 .clickInTicks(LEFT);300 }301 /**302 * Performs a double-click at the current mouse location.303 * @return A self reference.304 */305 public Actions doubleClick() {306 if (isBuildingActions()) {307 action.addAction(new DoubleClickAction(jsonMouse, null));308 }309 return clickInTicks(LEFT).clickInTicks(LEFT);310 }311 /**312 * Moves the mouse to the middle of the element. The element is scrolled into view and its313 * location is calculated using getClientRects.314 * @param target element to move to.315 * @return A self reference.316 */317 public Actions moveToElement(WebElement target) {318 if (isBuildingActions()) {319 action.addAction(new MoveMouseAction(jsonMouse, (Locatable) target));320 }321 return moveInTicks(target, 0, 0);322 }323 /**324 * Moves the mouse to an offset from the element's in-view center point.325 * @param target element to move to.326 * @param xOffset Offset from the element's in-view center point. A negative value means327 * an offset left of the point.328 * @param yOffset Offset from the element's in-view center point. A negative value means329 * an offset above the point.330 * @return A self reference.331 */332 public Actions moveToElement(WebElement target, int xOffset, int yOffset) {333 if (isBuildingActions()) {334 action.addAction(new MoveToOffsetAction(jsonMouse, (Locatable) target, xOffset, yOffset));335 }336 // Of course, this is the offset from the centre of the element. We have no idea what the width337 // and height are once we execute this method.338 LOG.info("When using the W3C Action commands, offsets are from the element's in-view center point");339 return moveInTicks(target, xOffset, yOffset);340 }341 private Actions moveInTicks(WebElement target, int xOffset, int yOffset) {342 return tick(getActivePointer().createPointerMove(343 Duration.ofMillis(100),344 Origin.fromElement(target),345 xOffset,346 yOffset));347 }348 /**349 * Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates350 * provided are outside the viewport (the mouse will end up outside the browser window) then351 * the viewport is scrolled to match.352 * @param xOffset horizontal offset. A negative value means moving the mouse left.353 * @param yOffset vertical offset. A negative value means moving the mouse up.354 * @return A self reference.355 * @throws MoveTargetOutOfBoundsException if the provided offset is outside the document's356 * boundaries.357 */358 public Actions moveByOffset(int xOffset, int yOffset) {359 if (isBuildingActions()) {360 action.addAction(new MoveToOffsetAction(jsonMouse, null, xOffset, yOffset));361 }362 return tick(363 getActivePointer().createPointerMove(Duration.ofMillis(200), Origin.pointer(), xOffset, yOffset));364 }365 /**366 * Performs a context-click at middle of the given element. First performs a mouseMove367 * to the location of the element.368 *369 * @param target Element to move to.370 * @return A self reference.371 */372 public Actions contextClick(WebElement target) {373 if (isBuildingActions()) {374 action.addAction(new ContextClickAction(jsonMouse, (Locatable) target));375 }376 return moveInTicks(target, 0, 0).clickInTicks(RIGHT);377 }378 /**379 * Performs a context-click at the current mouse location.380 * @return A self reference.381 */382 public Actions contextClick() {383 if (isBuildingActions()) {384 action.addAction(new ContextClickAction(jsonMouse, null));385 }386 return clickInTicks(RIGHT);387 }388 /**389 * A convenience method that performs click-and-hold at the location of the source element,390 * moves to the location of the target element, then releases the mouse.391 *392 * @param source element to emulate button down at.393 * @param target element to move to and release the mouse at.394 * @return A self reference.395 */396 public Actions dragAndDrop(WebElement source, WebElement target) {397 if (isBuildingActions()) {398 action.addAction(new ClickAndHoldAction(jsonMouse, (Locatable) source));399 action.addAction(new MoveMouseAction(jsonMouse, (Locatable) target));400 action.addAction(new ButtonReleaseAction(jsonMouse, (Locatable) target));401 }402 return moveInTicks(source, 0, 0)403 .tick(getActivePointer().createPointerDown(LEFT.asArg()))404 .moveInTicks(target, 0, 0)405 .tick(getActivePointer().createPointerUp(LEFT.asArg()));406 }407 /**408 * A convenience method that performs click-and-hold at the location of the source element,409 * moves by a given offset, then releases the mouse.410 *411 * @param source element to emulate button down at.412 * @param xOffset horizontal move offset.413 * @param yOffset vertical move offset.414 * @return A self reference.415 */416 public Actions dragAndDropBy(WebElement source, int xOffset, int yOffset) {417 if (isBuildingActions()) {418 action.addAction(new ClickAndHoldAction(jsonMouse, (Locatable) source));419 action.addAction(new MoveToOffsetAction(jsonMouse, null, xOffset, yOffset));420 action.addAction(new ButtonReleaseAction(jsonMouse, null));421 }422 return moveInTicks(source, 0, 0)423 .tick(getActivePointer().createPointerDown(LEFT.asArg()))424 .tick(getActivePointer().createPointerMove(Duration.ofMillis(250), Origin.pointer(), xOffset, yOffset))425 .tick(getActivePointer().createPointerUp(LEFT.asArg()));426 }427 /**428 * Performs a pause.429 *430 * @param pause pause duration, in milliseconds.431 * @return A self reference.432 */433 public Actions pause(long pause) {434 if (isBuildingActions()) {435 action.addAction(new PauseAction(pause));436 }437 return tick(new Pause(getActivePointer(), Duration.ofMillis(pause)));438 }439 public Actions pause(Duration duration) {440 Require.nonNegative("Duration of pause", duration);441 if (isBuildingActions()) {442 action.addAction(new PauseAction(duration.toMillis()));443 }444 return tick(new Pause(getActivePointer(), duration));445 }446 public Actions tick(Interaction... actions) {447 // All actions must be for a unique source.448 Set<InputSource> seenSources = new HashSet<>();449 for (Interaction action : actions) {450 boolean freshlyAdded = seenSources.add(action.getSource());451 if (!freshlyAdded) {452 throw new IllegalStateException(String.format(453 "You may only add one action per input source per tick: %s",454 Arrays.asList(actions)));455 }456 }457 // Add all actions to sequences458 for (Interaction action : actions) {459 Sequence sequence = getSequence(action.getSource());460 sequence.addAction(action);461 }462 // And now pad the remaining sequences with a pause.463 Set<InputSource> unseen = new HashSet<>(sequences.keySet());464 unseen.removeAll(seenSources);465 for (InputSource source : unseen) {466 getSequence(source).addAction(new Pause(source, Duration.ZERO));467 }468 return this;469 }470 public Actions tick(Action action) {471 if (!(action instanceof IsInteraction)) {472 throw new IllegalStateException("Expected action to implement IsInteraction");473 }474 for (Interaction interaction :475 ((IsInteraction) action).asInteractions(getActivePointer(), getActiveKeyboard())) {476 tick(interaction);477 }478 if (isBuildingActions()) {479 this.action.addAction(action);480 }481 return this;482 }483 public Actions setActiveKeyboard(String name) {484 InputSource inputSource = sequences.keySet().stream().filter(input -> Objects.equals(input.getName(), name)).findFirst().orElse(null);485 if (inputSource == null) {486 this.activeKeyboard = new KeyInput(name);487 } else {488 this.activeKeyboard = (KeyInput) inputSource;489 }490 return this;491 }492 public Actions setActivePointer(PointerInput.Kind kind, String name) {493 InputSource inputSource = sequences.keySet().stream().filter(input -> Objects.equals(input.getName(), name)).findFirst().orElse(null);494 if (inputSource == null) {495 this.activePointer = new PointerInput(kind, name);496 } else {497 this.activePointer = (PointerInput) inputSource;498 }499 return this;500 }501 public KeyInput getActiveKeyboard() {502 if (this.activeKeyboard == null) {503 setActiveKeyboard("default keyboard");504 }505 return this.activeKeyboard;506 }507 public PointerInput getActivePointer() {508 if (this.activePointer == null) {509 setActivePointer(PointerInput.Kind.MOUSE, "default mouse");510 }511 return this.activePointer;512 }513 /**514 * Generates a composite action containing all actions so far, ready to be performed (and515 * resets the internal builder state, so subsequent calls to this method will contain fresh516 * sequences).517 * <p>518 * <b>Warning</b>: you may want to call {@link #perform()} instead to actually perform519 * the actions.520 *521 * @return the composite action...

Full Screen

Full Screen

getActivePointer

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.chrome.ChromeDriver; 5import org.openqa.selenium.interactions.Actions; 6import org.openqa.selenium.interactions.PointerInput; 7import org.openqa.selenium.interactions.Sequence; 8import org.openqa.selenium.support.ui.ExpectedConditions; 9import org.openqa.selenium.support.ui.WebDriverWait; 10import org.testng.annotations.AfterTest; 11import org.testng.annotations.BeforeTest; 12import org.testng.annotations.Test;13public class GetActivePointer { 14WebDriver driver; 15WebDriverWait wait;16public void setup() { 17System.setProperty(“webdriver.chrome.driver”, “C:\\\\chromedriver.exe”); 18driver = new ChromeDriver(); 19wait = new WebDriverWait(driver, 10); 20}21public void getActivePointerTest() { 22driver.manage().window().maximize(); 23driver.switchTo().frame(“iframeResult”); 24WebElement source = driver.findElement(By.id(“drag1”)); 25WebElement target = driver.findElement(By.id(“div1”)); 26Actions builder = new Actions(driver); 27builder.clickAndHold(source).moveToElement(target).release().perform(); 28PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, “finger”); 29Sequence dragAndDrop = new Sequence(finger, 1); 30dragAndDrop.addAction(finger.createPointerMove(Duration.ofMillis(0), 31PointerInput.Origin.viewport(), 0, 0)); 32dragAndDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())); 33dragAndDrop.addAction(finger.createPointerMove(Duration.ofMillis(1000), 34PointerInput.Origin.viewport(), 100, 100)); 35dragAndDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); 36driver.perform(Arrays.asList(dragAndDrop)); 37}38public void tearDown() { 39driver.quit(); 40} 41}

Full Screen

Full Screen

getActivePointer

Using AI Code Generation

copy

Full Screen

1Actions actions = new Actions(driver);2actions.moveToElement(element).build().perform();3Actions actions = new Actions(driver);4actions.moveToElement(element).click().build().perform();5Actions actions = new Actions(driver);6actions.moveToElement(element).contextClick().build().perform();7Actions actions = new Actions(driver);8actions.moveToElement(element).click().build().perform();9Actions actions = new Actions(driver);10actions.moveToElement(element).click().build().perform();11Actions actions = new Actions(driver);12actions.moveToElement(element).doubleClick().build().perform();13Actions actions = new Actions(driver);14actions.moveToElement(element).clickAndHold().build().perform();15Actions actions = new Actions(driver);16actions.moveToElement(element).release().build().perform();17Actions actions = new Actions(driver);18actions.moveToElement(element).release().build().perform();

Full Screen

Full Screen

getActivePointer

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.Point;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.interactions.Actions;5import org.openqa.selenium.interactions.PointerInput;6import org.openqa.selenium.interactions.Sequence;7import org.openqa.selenium.remote.RemoteWebDriver;8import org.openqa.selenium.support.ui.WebDriverWait;9import org.testng.annotations.BeforeClass;10import org.testng.annotations.Test;11import io.appium.java_client.MobileElement;12import io.appium.java_client.android.AndroidDriver;13import io.appium.java_client.android.AndroidElement;14import io.appium.java_client.android.nativekey.AndroidKey;15import io.appium.java_client.android.nativekey.KeyEvent;16import io.appium.java_client.remote.MobileCapabilityType;17import io.appium.java_client.remote.MobilePlatform;18import io.appium.java_client.touch.offset.PointOption;19import io.appium.java_client.touch.WaitOptions;20import io.appium.java_client.touch.offset.ElementOption;21import io.appium.java_client.touch.TapOptions;22import io.appium.java_client.touch.LongPressOptions;23import io.appium.java_client.touch.offset.PointOption;24import io.appium.java_client.touch.LongPressOptions;25import io.appium.java_client.touch.WaitOptions;26import java.net.MalformedURLException;27import java.net.URL;28import java.time.Duration;29import java.util.List;30import java.util.concurrent.TimeUnit;31public class getActivePointer {32 RemoteWebDriver driver;33 WebDriverWait wait;34 public void setup() throws MalformedURLException {35 DesiredCapabilities caps = new DesiredCapabilities();36 caps.setCapability("deviceName", "Pixel_4_Emulator");37 caps.setCapability("platformName", "Android");38 caps.setCapability("platformVersion", "11.0");39 caps.setCapability("skipUnlock", "true");40 caps.setCapability("appPackage", "com.android.chrome");41 caps.setCapability("appActivity", "com.google.android.apps.chrome.Main");42 caps.setCapability("noReset", "true");

Full Screen

Full Screen

getActivePointer

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.Keys;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.interactions.Actions;6import org.openqa.selenium.interactions.PointerInput;7import org.openqa.selenium.interactions.Sequence;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.WebDriverWait;10import org.testng.annotations.Test;11import java.time.Duration;12import java.util.ArrayList;13import java.util.List;14public class MultiPointerTouchActions extends BaseTest {15 public void testMultiPointerTouchActions() {16 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));17 PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");18 Sequence draw = new Sequence(finger, 0);19 draw.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(), 100, 100));20 draw.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));21 draw.addAction(finger.createPointerMove(Duration.ofMillis(1000), PointerInput.Origin.viewport(), 200, 200));22 draw.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));23 Sequence draw2 = new Sequence(finger, 1);24 draw2.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(), 200, 200));25 draw2.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));26 draw2.addAction(finger.createPointerMove(Duration.ofMillis(1000), PointerInput.Origin.viewport(), 300, 300));27 draw2.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));28 Actions multiTouch = new Actions(driver);

Full Screen

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful