Skip to content

Commit aa0aeb0

Browse files
fufu930harsha509
andauthored
Adding Kotlin examples (SeleniumHQ#1163)[deploy site]
* Kotlin add examples and link docs * Link Kotlin pen tests Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
1 parent cff4e52 commit aa0aeb0

File tree

17 files changed

+212
-91
lines changed

17 files changed

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

website_and_docs/content/documentation/webdriver/actions_api/_index.en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ to wait a beat between actions for things to work correctly.
5050
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L20-L27" >}}
5151
{{< /tab >}}
5252
{{< tab header="Kotlin" >}}
53-
{{< badge-code >}}
53+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L22-L29" >}}
5454
{{< /tab >}}
5555
{{< /tabpane >}}
5656

@@ -81,6 +81,6 @@ it does not get executed with the perform method.
8181
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L44" >}}
8282
{{< /tab >}}
8383
{{< tab header="Kotlin" >}}
84-
{{< badge-code >}}
84+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L47" >}}
8585
{{< /tab >}}
8686
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/_index.ja.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ to wait a beat between actions for things to work correctly.
5050
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L20-L27" >}}
5151
{{< /tab >}}
5252
{{< tab header="Kotlin" >}}
53-
{{< badge-code >}}
53+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L22-L29" >}}
5454
{{< /tab >}}
5555
{{< /tabpane >}}
5656

@@ -81,6 +81,6 @@ it does not get executed with the perform method.
8181
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L44" >}}
8282
{{< /tab >}}
8383
{{< tab header="Kotlin" >}}
84-
{{< badge-code >}}
84+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L47" >}}
8585
{{< /tab >}}
8686
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/_index.pt-br.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ to wait a beat between actions for things to work correctly.
5050
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L20-L27" >}}
5151
{{< /tab >}}
5252
{{< tab header="Kotlin" >}}
53-
{{< badge-code >}}
53+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L22-L29" >}}
5454
{{< /tab >}}
5555
{{< /tabpane >}}
5656

@@ -81,6 +81,6 @@ it does not get executed with the perform method.
8181
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L44" >}}
8282
{{< /tab >}}
8383
{{< tab header="Kotlin" >}}
84-
{{< badge-code >}}
84+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L47" >}}
8585
{{< /tab >}}
8686
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/_index.zh-cn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Selenium允许您构建分配给特定输入的独立操作命令,
6363
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L20-L27" >}}
6464
{{< /tab >}}
6565
{{< tab header="Kotlin" >}}
66-
{{< badge-code >}}
66+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L22-L29" >}}
6767
{{< /tab >}}
6868
{{< /tabpane >}}
6969

@@ -96,6 +96,6 @@ Selenium允许您构建分配给特定输入的独立操作命令,
9696
{{< gh-codeblock path="examples/javascript/test/actionsApi/actionsTest.spec.js#L44" >}}
9797
{{< /tab >}}
9898
{{< tab header="Kotlin" >}}
99-
{{< badge-code >}}
99+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt#L47" >}}
100100
{{< /tab >}}
101101
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/keyboard.en.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
6262
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L19-L22" >}}
6363
{{< /tab >}}
6464
{{< tab header="Kotlin" >}}
65-
{{< badge-code >}}
65+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L19-L22" >}}
6666
{{< /tab >}}
6767
{{< /tabpane >}}
6868

@@ -85,7 +85,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
8585
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L34-L39" >}}
8686
{{< /tab >}}
8787
{{< tab header="Kotlin" >}}
88-
{{< badge-code >}}
88+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L32-L37" >}}
8989
{{< /tab >}}
9090
{{< /tabpane >}}
9191

@@ -114,7 +114,7 @@ primarily this gets used when needing to type multiple characters in the middle
114114
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L47-L48" >}}
115115
{{< /tab >}}
116116
{{< tab header="Kotlin" >}}
117-
{{< badge-code >}}
117+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L47-L49" >}}
118118
{{< /tab >}}
119119
{{< /tabpane >}}
120120

@@ -137,7 +137,7 @@ primarily this gets used when needing to type multiple characters in the middle
137137
{{< badge-code >}}
138138
{{< /tab >}}
139139
{{< tab header="Kotlin" >}}
140-
{{< badge-code >}}
140+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L60-L63" >}}
141141
{{< /tab >}}
142142
{{< /tabpane >}}
143143

@@ -164,6 +164,6 @@ This code will end up with the text: `SeleniumSelenium!`
164164
{{< badge-code >}}
165165
{{< /tab >}}
166166
{{< tab header="Kotlin" >}}
167-
{{< badge-code >}}
167+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L74-L86" >}}
168168
{{< /tab >}}
169169
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/keyboard.ja.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
6262
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L19-L22" >}}
6363
{{< /tab >}}
6464
{{< tab header="Kotlin" >}}
65-
{{< badge-code >}}
65+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L19-L22" >}}
6666
{{< /tab >}}
6767
{{< /tabpane >}}
6868

@@ -85,7 +85,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
8585
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L34-L39" >}}
8686
{{< /tab >}}
8787
{{< tab header="Kotlin" >}}
88-
{{< badge-code >}}
88+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L32-L37" >}}
8989
{{< /tab >}}
9090
{{< /tabpane >}}
9191

@@ -114,7 +114,7 @@ primarily this gets used when needing to type multiple characters in the middle
114114
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L47-L48" >}}
115115
{{< /tab >}}
116116
{{< tab header="Kotlin" >}}
117-
{{< badge-code >}}
117+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L47-L49" >}}
118118
{{< /tab >}}
119119
{{< /tabpane >}}
120120

@@ -137,7 +137,7 @@ primarily this gets used when needing to type multiple characters in the middle
137137
{{< badge-code >}}
138138
{{< /tab >}}
139139
{{< tab header="Kotlin" >}}
140-
{{< badge-code >}}
140+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L60-L63" >}}
141141
{{< /tab >}}
142142
{{< /tabpane >}}
143143

@@ -164,6 +164,6 @@ This code will end up with the text: `SeleniumSelenium!`
164164
{{< badge-code >}}
165165
{{< /tab >}}
166166
{{< tab header="Kotlin" >}}
167-
{{< badge-code >}}
167+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L74-L86" >}}
168168
{{< /tab >}}
169169
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/keyboard.pt-br.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
6262
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L19-L22" >}}
6363
{{< /tab >}}
6464
{{< tab header="Kotlin" >}}
65-
{{< badge-code >}}
65+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L19-L22" >}}
6666
{{< /tab >}}
6767
{{< /tabpane >}}
6868

@@ -85,7 +85,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
8585
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L34-L39" >}}
8686
{{< /tab >}}
8787
{{< tab header="Kotlin" >}}
88-
{{< badge-code >}}
88+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L32-L37" >}}
8989
{{< /tab >}}
9090
{{< /tabpane >}}
9191

@@ -114,7 +114,7 @@ primarily this gets used when needing to type multiple characters in the middle
114114
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L47-L48" >}}
115115
{{< /tab >}}
116116
{{< tab header="Kotlin" >}}
117-
{{< badge-code >}}
117+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L47-L49" >}}
118118
{{< /tab >}}
119119
{{< /tabpane >}}
120120

@@ -137,7 +137,7 @@ primarily this gets used when needing to type multiple characters in the middle
137137
{{< badge-code >}}
138138
{{< /tab >}}
139139
{{< tab header="Kotlin" >}}
140-
{{< badge-code >}}
140+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L60-L63" >}}
141141
{{< /tab >}}
142142
{{< /tabpane >}}
143143

@@ -164,6 +164,6 @@ This code will end up with the text: `SeleniumSelenium!`
164164
{{< badge-code >}}
165165
{{< /tab >}}
166166
{{< tab header="Kotlin" >}}
167-
{{< badge-code >}}
167+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L74-L86" >}}
168168
{{< /tab >}}
169169
{{< /tabpane >}}

website_and_docs/content/documentation/webdriver/actions_api/keyboard.zh-cn.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
6262
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L19-L22" >}}
6363
{{< /tab >}}
6464
{{< tab header="Kotlin" >}}
65-
{{< badge-code >}}
65+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L19-L22" >}}
6666
{{< /tab >}}
6767
{{< /tabpane >}}
6868

@@ -85,7 +85,7 @@ Use the [Java Keys enum](https://github.com/SeleniumHQ/selenium/blob/selenium-4.
8585
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L34-L39" >}}
8686
{{< /tab >}}
8787
{{< tab header="Kotlin" >}}
88-
{{< badge-code >}}
88+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L32-L37" >}}
8989
{{< /tab >}}
9090
{{< /tabpane >}}
9191

@@ -114,7 +114,7 @@ primarily this gets used when needing to type multiple characters in the middle
114114
{{< gh-codeblock path="examples/javascript/test/actionsApi/keysTest.spec.js#L47-L48" >}}
115115
{{< /tab >}}
116116
{{< tab header="Kotlin" >}}
117-
{{< badge-code >}}
117+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L47-L49" >}}
118118
{{< /tab >}}
119119
{{< /tabpane >}}
120120

@@ -137,7 +137,7 @@ primarily this gets used when needing to type multiple characters in the middle
137137
{{< badge-code >}}
138138
{{< /tab >}}
139139
{{< tab header="Kotlin" >}}
140-
{{< badge-code >}}
140+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L60-L63" >}}
141141
{{< /tab >}}
142142
{{< /tabpane >}}
143143

@@ -164,6 +164,6 @@ This code will end up with the text: `SeleniumSelenium!`
164164
{{< badge-code >}}
165165
{{< /tab >}}
166166
{{< tab header="Kotlin" >}}
167-
{{< badge-code >}}
167+
{{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt#L74-L86" >}}
168168
{{< /tab >}}
169169
{{< /tabpane >}}

0 commit comments

Comments
 (0)