Skip to content

Commit d4c68d7

Browse files
authored
Issue148 (#149)
* closes #146
1 parent b99acf3 commit d4c68d7

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

.github/badges/jacoco.svg

Lines changed: 1 addition & 1 deletion
Loading

src/main/kotlin/org/xpathqs/core/selector/base/BaseSelector.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ abstract class BaseSelector(
5858
* Build selector's xpath
5959
*/
6060
override fun toXpath(): String {
61-
val b = base.toXpath()
62-
val p = props.toXpath()
63-
if(b.isNotEmpty() && !p.startsWith("/")) {
64-
return "$b//$p"
65-
}
66-
return b + p
61+
return mergeXpath(
62+
base.toXpath(), props.toXpath()
63+
)
6764
}
6865

6966
/**
@@ -80,4 +77,16 @@ abstract class BaseSelector(
8077
xpath
8178
}
8279
}
80+
81+
companion object {
82+
fun mergeXpath(xp1: String, xp2: String): String {
83+
if(xp1.isNotEmpty()
84+
&& !xp2.startsWith("/")
85+
&& !xp2.startsWith("[")
86+
) {
87+
return "$xp1//$xp2"
88+
}
89+
return xp1 + xp2
90+
}
91+
}
8392
}

src/main/kotlin/org/xpathqs/core/selector/group/GroupSelector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ open class GroupSelector(
7272
}
7373
val props = props.toXpath()
7474
if (props.isNotEmpty()) {
75-
"(${base.toXpath() + res})$props"
75+
mergeXpath("(${base.toXpath() + res})", props)
7676
} else {
77-
base.toXpath() + res
77+
mergeXpath(base.toXpath(), res)
7878
}
7979
}
8080
}

src/test/kotlin/org/xpathqs/core/reflection/SelectorReflectionFieldsTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ internal class SelectorReflectionFieldsTest {
3737
BaseSelector::name.name,
3838
BaseSelector::annotations.name,
3939
BaseSelector::field.name,
40+
"Companion"
4041
)
4142

4243
@Test
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2021 XPATH-QS
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.selector.block.extensions
24+
25+
import org.junit.jupiter.api.Test
26+
import org.xpathqs.core.reflection.scanPackage
27+
import org.xpathqs.core.selector.block.Block
28+
import org.xpathqs.core.selector.extensions.plus
29+
import org.xpathqs.core.selector.selector.followingSibling
30+
import org.xpathqs.core.util.SelectorFactory.tagSelector
31+
import org.xpathqs.xpathShouldBe
32+
33+
object Page1 : Block(tagSelector("base")) {
34+
val s1 = tagSelector("base").followingSibling()
35+
val s2 = tagSelector("base").followingSibling() + tagSelector("div")
36+
}
37+
38+
class AxeExtensionsTest {
39+
init {
40+
Page1::class.java.packageName.scanPackage()
41+
}
42+
43+
@Test
44+
fun followingWithBase() {
45+
Page1.s1
46+
.xpathShouldBe("//base//following-sibling::base")
47+
}
48+
49+
@Test
50+
fun followingWithBaseAndGroup() {
51+
Page1.s2
52+
.xpathShouldBe("//base//following-sibling::base//div")
53+
}
54+
}

0 commit comments

Comments
 (0)