Skip to content

Commit 4c58c0e

Browse files
committed
tests/qg-253: Link переведён на матчинг урлов через UriTemplate
Для повышения надёжности. В частности для матчинга урлов с вложенными uri с query-параметрами в качестве значений query-параметров url
1 parent 386e6f2 commit 4c58c0e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

app/src/testFixtures/kotlin/pro/qyoga/tests/assertions/ElementMatchers.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.kotest.matchers.compose.all
66
import io.kotest.matchers.should
77
import io.kotest.matchers.shouldNot
88
import org.jsoup.nodes.Element
9+
import org.springframework.web.util.UriTemplate
910
import pro.qyoga.tests.platform.html.Component
1011
import pro.qyoga.tests.platform.html.HtmlPage
1112
import pro.qyoga.tests.platform.html.Input
@@ -104,6 +105,14 @@ fun haveAttributeValueMatching(attr: String, valueRegex: Regex) = Matcher { elem
104105
)
105106
}
106107

108+
fun haveAttributeValueMatching(attr: String, uriTemplate: UriTemplate) = Matcher { element: Element ->
109+
MatcherResult.invoke(
110+
uriTemplate.matches(element.attr(attr)),
111+
{ "Element ${element.descr} has $attr=\"${element.attr(attr)}\" but `$attr` value matching \"${uriTemplate}}\" is expected" },
112+
{ "Element ${element.descr} should not have attribute `$attr` value matching \"${uriTemplate}}\"" },
113+
)
114+
}
115+
107116
fun haveAttributeValue(attr: String, value: String, ignoreCase: Boolean = false) = Matcher { element: Element ->
108117
MatcherResult.invoke(
109118
element.attr(attr).equals(value, ignoreCase = ignoreCase),
@@ -277,4 +286,4 @@ data class SelectorOnlyComponent(val selector: String) : Component {
277286

278287
override fun matcher(): Matcher<Element> = alwaysSuccess()
279288

280-
}
289+
}

app/src/testFixtures/kotlin/pro/qyoga/tests/platform/html/Link.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package pro.qyoga.tests.platform.html
22

33
import io.kotest.matchers.Matcher
44
import org.jsoup.nodes.Element
5+
import org.springframework.web.util.UriTemplate
56
import pro.qyoga.tests.assertions.haveAttribute
67
import pro.qyoga.tests.assertions.haveAttributeValueMatching
78
import pro.qyoga.tests.assertions.haveText
89
import pro.qyoga.tests.assertions.isTag
910
import pro.qyoga.tests.platform.kotest.all
10-
import pro.qyoga.tests.platform.pathToRegex
1111

1212
class Link(
1313
val id: String,
@@ -16,7 +16,7 @@ class Link(
1616
val targetAttr: String = "href"
1717
) : Component {
1818

19-
val urlRegex = urlPattern.pathToRegex().toRegex()
19+
val urlRegex = UriTemplate(urlPattern)
2020

2121
constructor(id: String, page: HtmlPageCompat, text: String) : this(id, page.path, text)
2222

@@ -36,12 +36,8 @@ class Link(
3636

3737
fun pathParam(element: Element, paramName: String): String? {
3838
val actualUrl = element.select(selector()).attr(targetAttr)
39-
val paramIdx = "\\{.*?}".toRegex().findAll(urlPattern)
40-
.indexOfFirst { it.value == "{$paramName}" }
41-
.takeIf { it >= 0 }
42-
?: return null
43-
44-
return urlRegex.matchEntire(actualUrl)!!.groupValues[paramIdx + 1]
39+
val vars = urlRegex.match(actualUrl)
40+
return vars[paramName]
4541
}
4642

4743
companion object {
@@ -52,4 +48,4 @@ class Link(
5248

5349
}
5450

55-
}
51+
}

0 commit comments

Comments
 (0)