-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByTextTest.kt
161 lines (122 loc) · 3.96 KB
/
ByTextTest.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package seleniumtestinglib.locators
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments.of
import org.junit.jupiter.params.provider.MethodSource
import org.openqa.selenium.NoSuchElementException
import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.text
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ByTextTest {
@Test
fun `by text`() {
driver.render("<article><span>I accept</span><div></div></article>")
val result = driver.findElement(text("I accept"))
assertEquals("span", result.tagName)
}
@Test
fun `ensure quotes are escaped`() {
driver.render(""""<article><span>with quotes"'`</span><div></div></article>""")
val result = driver.findElement(text("""with quotes"'`"""))
assertEquals("span", result.tagName)
}
@Test
fun `with a selector`() {
driver.render("<article><span>Username</span><div>Username</div></article>")
val result = driver.findElement(text("Username", selector = "div"))
assertEquals("div", result.tagName)
}
@Test
fun `not found`() {
driver.render("<span></span>")
val result = runCatching {
driver.findElement(text("abc"))
}
assertTrue(result.exceptionOrNull() is NoSuchElementException)
}
@Test
fun `not found with selector`() {
driver.render("<span>Username</span><div>Username</div>")
val result = runCatching {
driver.findElement(text("Username", selector = "x"))
}
assertTrue(result.exceptionOrNull() is NoSuchElementException)
}
@Test
fun `not exact with selector`() {
driver.render(
"""
<div>
<span>I accept</span>
<p>I accept</p>
</div>
<p>I accept</p>
"""
)
val result = driver.findElement(text("accept", exact = false, selector = "div p"))
assertEquals("p", result.tagName)
}
@Test
fun regex() {
driver.render("<p>I accept</p>")
val result = driver.findElement(text(Pattern.compile("ACCEPT", Pattern.CASE_INSENSITIVE)))
assertEquals("p", result.tagName)
}
@Test
fun function() {
driver.render("<p>Hello World</p>")
val result = driver.findElement(
text(JsFunction("(content, element) => content.startsWith('Hello') && element.tagName == 'P'"))
)
assertEquals("p", result.tagName)
}
@Test
fun `case insensitive`() {
driver.render("<p>I accept</p>")
val result = driver.findElement(text("ACCEPT", exact = false))
assertEquals("p", result.tagName)
}
@ParameterizedTest
@MethodSource("ignore values")
fun ignore(ignore: String, expectedFound: Int) {
driver.render(
"""
<p>I accept</p>
<script>I accept</script>
<style>I accept</style>
"""
)
val result = driver.findElements(text("I accept", ignore = ignore))
assertEquals(expectedFound, result.size)
}
private fun `ignore values`() = setOf(
of("", 3),
of("style", 2),
of("style,script", 1),
of("style,script,p", 0),
)
@Test
fun `ignore with boolean`() {
driver.render(
"""
<p>I accept</p>
<script>I accept</script>
<style>I accept</style>
"""
)
val result = driver.findElements(text("I accept").ignore(false))
assertEquals(3, result.size)
}
@Test
fun normalizer() {
driver.render("<p>I accept</p>")
val result = driver.findElement(
text("I accept123", normalizer = JsFunction( "str => str + '123'"))
)
assertEquals("p", result.tagName)
}
}