-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PenTest.kt
121 lines (103 loc) · 5.65 KB
/
PenTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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.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 kotlin.collections.Map
import java.time.Duration
class PenTest : BaseTest() {
@Test
fun usePen() {
driver.get("https://www.selenium.dev/selenium/web/pointerActionsPage.html")
val pointerArea = driver.findElement(By.id("pointerArea"))
Actions(driver)
.setActivePointer(PointerInput.Kind.PEN, "default pen")
.moveToElement(pointerArea)
.clickAndHold()
.moveByOffset(2, 2)
.release()
.perform()
val moves = driver.findElements(By.className("pointermove"))
val moveTo = getPropertyInfo(moves.get(0))
val down = getPropertyInfo(driver.findElement(By.className("pointerdown")))
val moveBy = getPropertyInfo(moves.get(1))
val up = getPropertyInfo(driver.findElement(By.className("pointerup")))
val rect = pointerArea.getRect()
val centerX = Math.floor(rect.width.toDouble() / 2 + rect.getX()).toInt()
val centerY = Math.floor(rect.height.toDouble() / 2 + rect.getY()).toInt()
Assertions.assertEquals("-1", moveTo.get("button"))
Assertions.assertEquals("pen", moveTo.get("pointerType"))
Assertions.assertEquals(centerX.toString(), moveTo.get("pageX"))
Assertions.assertEquals(centerY.toString(), moveTo.get("pageY"))
Assertions.assertEquals("0", down.get("button"))
Assertions.assertEquals("pen", down.get("pointerType"))
Assertions.assertEquals(centerX.toString(), down.get("pageX"))
Assertions.assertEquals(centerY.toString(), down.get("pageY"))
Assertions.assertEquals("-1", moveBy.get("button"))
Assertions.assertEquals("pen", moveBy.get("pointerType"))
Assertions.assertEquals((centerX + 2).toString(), moveBy.get("pageX"))
Assertions.assertEquals((centerY + 2).toString(), moveBy.get("pageY"))
Assertions.assertEquals("0", up.get("button"))
Assertions.assertEquals("pen", up.get("pointerType"))
Assertions.assertEquals((centerX + 2).toString(), up.get("pageX"))
Assertions.assertEquals((centerY + 2).toString(), up.get("pageY"))
}
@Test
fun setPointerEventProperties() {
driver.get("https://selenium.dev/selenium/web/pointerActionsPage.html")
val pointerArea = driver.findElement(By.id("pointerArea"))
val pen = PointerInput(PointerInput.Kind.PEN, "default pen")
val eventProperties = PointerInput.eventProperties()
.setTiltX(-72)
.setTiltY(9)
.setTwist(86)
val origin = PointerInput.Origin.fromElement(pointerArea)
val actionListPen = Sequence(pen, 0)
.addAction(pen.createPointerMove(Duration.ZERO, origin, 0, 0))
.addAction(pen.createPointerDown(0))
.addAction(pen.createPointerMove(Duration.ZERO, origin, 2, 2, eventProperties))
.addAction(pen.createPointerUp(0))
(driver as RemoteWebDriver).perform(listOf(actionListPen))
val moves = driver.findElements(By.className("pointermove"))
val moveTo = getPropertyInfo(moves.get(0))
val down = getPropertyInfo(driver.findElement(By.className("pointerdown")))
val moveBy = getPropertyInfo(moves.get(1))
val up = getPropertyInfo(driver.findElement(By.className("pointerup")))
val rect = pointerArea.getRect()
val centerX = Math.floor(rect.width.toDouble() / 2 + rect.getX()).toInt()
val centerY = Math.floor(rect.height.toDouble() / 2 + rect.getY()).toInt()
Assertions.assertEquals("-1", moveTo.get("button"))
Assertions.assertEquals("pen", moveTo.get("pointerType"))
Assertions.assertEquals(centerX.toString(), moveTo.get("pageX"))
Assertions.assertEquals(centerY.toString(), moveTo.get("pageY"))
Assertions.assertEquals("0", down.get("button"))
Assertions.assertEquals("pen", down.get("pointerType"))
Assertions.assertEquals(centerX.toString(), down.get("pageX"))
Assertions.assertEquals(centerY.toString(), down.get("pageY"))
Assertions.assertEquals("-1", moveBy.get("button"))
Assertions.assertEquals("pen", moveBy.get("pointerType"))
Assertions.assertEquals((centerX + 2).toString(), moveBy.get("pageX"))
Assertions.assertEquals((centerY + 2).toString(), moveBy.get("pageY"))
Assertions.assertEquals("0", up.get("button"))
Assertions.assertEquals("pen", up.get("pointerType"))
Assertions.assertEquals((centerX + 2).toString(), up.get("pageX"))
Assertions.assertEquals((centerY + 2).toString(), up.get("pageY"))
Assertions.assertEquals("-72", moveBy.get("tiltX"))
Assertions.assertEquals("9", moveBy.get("tiltY"))
Assertions.assertEquals("86", moveBy.get("twist"))
}
fun getPropertyInfo(element: WebElement): Map<String, String> {
var text = element.getText()
text = text.substring(text.indexOf(" ")+1)
return text.split(", ")
.map { it.split(": ") }
.map { it.first() to it.last() }
.toMap()
}
}