• Selenium Video Tutorials

Selenium WebDriver - Pen Events



Selenium Webdriver can be used to perform a pointer input similar to a pen movements to communicate with web elements on a page. This is performed using the Actions Class. The pen events are only available in the Chromium browsers.

What is a Pen Action?

A pen is similar to a pointer input which has the similar characteristics of a mouse. It can also have the event properties which is distinct to a stylus. While a mouse consists of five buttons, a pen is only capable of three buttons namely, 0 for touch contact which is similar to a left click, 2 for barrel button which is similar to a right click, and 5 for eraser button (this is still unsupported by drivers).

Syntax to Perform a Pen Event

WebElement a = driver.findElement(By.xpath("xpath of pointer area"));
new Actions(driver)
   .setActivePointer(PointerInput.Kind.PEN, "default pen")
   .moveToElement(a)
   .clickAndHold()
   .moveByOffset(5, 5)
   .release()
   .perform();

Syntax to Perform Pointer Event Attributes

WebElement a = driver.findElement(By.xpath("xpath of pointer area"));
PointerInput p = 
new PointerInput(PointerInput.Kind.PEN, "default pen");
PointerInput.PointerEventProperties eventProperties = 
PointerInput.eventProperties()
   .setTiltX(-82)
   .setTiltY(10)
   .setTwist(98);
PointerInput.Origin o = PointerInput.Origin.fromElement(a);

Sequence act = new Sequence(p, 0)
   .addAction(p.createPointerMove(Duration.ZERO, origin, 0, 5))
   .addAction(p.createPointerDown(0))
   .addAction(p.createPointerMove(Duration.ZERO, origin, 2, 7,  eventProperties))
   .addAction(p.createPointerUp(0));

((RemoteWebDriver) driver).perform(Collections.singletonList(act));

Thus, in this tutorial, we had discussed Pen Events using the Selenium Webdriver.

Advertisements