Skip to content

Commit

Permalink
wip - provide context for search
Browse files Browse the repository at this point in the history
  • Loading branch information
lsoares committed Mar 4, 2024
1 parent 93cec0f commit 6f3d13e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/src/main/kotlin/seleniumtestinglib/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,17 @@ abstract class TLBy(private val by: String, private val textMatch: TextMatch) :
override fun findElements(context: SearchContext): List<WebElement> {
val jsExecutor = (getWebDriver(context) as JavascriptExecutor)
jsExecutor.ensureScript("testing-library.js", "window.__TL__?.screen?.queryAllByTestId")
val script = buildString {
return jsExecutor.executeScript(buildString {
append("return window.__TL__.screen.queryAllBy$by(")
(context as? WebElement)?.let {
append("arguments[0], ")
}
append(textMatch.escaped)
this@TLBy
.takeIf(TLBy::isNotEmpty)
takeIf(TLBy::isNotEmpty)
?.escaped
?.let<Any, StringBuilder?> { append(", $it") }
?.let { append(", $it") }
append(")")
}
return jsExecutor.executeScript(script) as List<WebElement>
}, context as? WebElement) as List<WebElement>
}

fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) }
Expand Down
30 changes: 30 additions & 0 deletions lib/src/test/kotlin/seleniumtestinglib/locators/ContextTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seleniumtestinglib.locators

import org.openqa.selenium.By
import org.openqa.selenium.support.pagefactory.ByChained
import seleniumtestinglib.Role
import seleniumtestinglib.TL.By.role
import seleniumtestinglib.driver
import seleniumtestinglib.render
import kotlin.test.Test
import kotlin.test.assertEquals


class ContextText {

@Test
fun `chaining test`() {
driver.render(
"""
<form aria-label='createUser'>
<input type='checkbox' />
</form>
""".repeat(2)
)
val checkboxes = driver.findElements(ByChained(By.tagName("form"), By.tagName("input")))

val checkboxesTL = driver.findElements(ByChained(role(Role.Form), role(Role.CheckBox)))

assertEquals(checkboxes.size, checkboxesTL.size)
}
}

0 comments on commit 6f3d13e

Please sign in to comment.