Skip to content

Commit

Permalink
prevent exposing TextMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
lsoares committed Mar 6, 2024
1 parent 935ddde commit 544fab7
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 93 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ The [core API](https://testing-library.com/docs) contains the selectors which ar

```kotlin
driver.findElements(altText("first name"))
driver.findElement(displayValue("c => c.startsWith('selen')".asJsExpression()))
driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')")))
driver.findElements(labelText("active"))
driver.findElements(placeholderText("first name", exact = false))
driver.findElements(role(Heading, nameAsFunction = "c => c.startsWith('something')".asJsExpression()))
driver.findElements(role(Heading, nameAsFunction = JsFuntion("c => c.startsWith('something')")))
driver.findElements(role(Button, nameAsRegex = Pattern.compile("confirm")))
driver.findElements(testId("test-id"))
driver.findElements(text("present", exact = false, selector = "span"))
Expand Down
123 changes: 66 additions & 57 deletions lib/src/main/kotlin/seleniumtestinglib/Core.kt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/src/test/java/JavaApiTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import org.junit.jupiter.api.Test;
import seleniumtestinglib.JsFunction;

import java.util.regex.Pattern;

import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static seleniumtestinglib.CoreKt.asJsExpression;
import static seleniumtestinglib.DriverKt.getDriver;
import static seleniumtestinglib.DriverKt.render;
import static seleniumtestinglib.Role.Heading;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void byLabelText() {
public void byPlaceholderText() {
render(getDriver(), "<input placeholder='Username' />");

var result = getDriver().findElement(placeholderText(asJsExpression("c => c.startsWith('User')")));
var result = getDriver().findElement(placeholderText(new JsFunction("c => c.startsWith('User')")));

assertEquals("input", result.getTagName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package seleniumtestinglib.locators
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.openqa.selenium.NoSuchElementException
import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.altText
import seleniumtestinglib.TextMatch.JsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -47,7 +47,7 @@ class ByAltTextTest {
driver.render("<div alt='Incredibles 2 Poster' src='/incredibles-2.png' />")

val result = runCatching {
driver.findElement(altText(JsExpression("c => c.startsWith('inc')")))
driver.findElement(altText(JsFunction("c => c.startsWith('inc')")))
}

assertTrue(result.exceptionOrNull() is NoSuchElementException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package seleniumtestinglib.locators

import org.openqa.selenium.By.tagName
import org.openqa.selenium.support.ui.Select
import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.displayValue
import seleniumtestinglib.TL.By.placeholderText
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -49,7 +49,7 @@ class ByDisplayValueTest {
driver.render("<input placeholder='username' />")
driver.findElement(placeholderText("username")).sendKeys("selenium")

val result = driver.findElement(displayValue("c => c.startsWith('selen')".asJsExpression()))
val result = driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')")))

assertEquals("input", result.tagName)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seleniumtestinglib.locators

import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.labelText
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -91,7 +91,7 @@ class ByLabelTextTest {
fun function() {
driver.render("<input aria-label='Username' />")

val result = driver.findElement(labelText("c => c.startsWith('User')".asJsExpression()))
val result = driver.findElement(labelText(JsFunction("c => c.startsWith('User')")))

assertEquals("input", result.tagName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package seleniumtestinglib.locators
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.openqa.selenium.NoSuchElementException
import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.placeholderText
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -48,7 +48,7 @@ class ByPlaceholderTextTest {
fun function() {
driver.render("<input placeholder='Username' />")

val result = driver.findElement(placeholderText("c => c.startsWith('User')".asJsExpression()))
val result = driver.findElement(placeholderText(JsFunction("c => c.startsWith('User')")))

assertEquals("input", result.tagName)
}
Expand Down
7 changes: 3 additions & 4 deletions lib/src/test/kotlin/seleniumtestinglib/locators/ByRoleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.openqa.selenium.WebElement
import seleniumtestinglib.*
import seleniumtestinglib.Role.*
import seleniumtestinglib.TL.By.role
import seleniumtestinglib.TextMatch.JsExpression
import java.util.regex.Pattern
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -51,7 +50,7 @@ class ByRoleTest {
driver.render("""<h1>something as a user something</h1>""")

val result =
driver.findElements(role(Heading, nameAsFunction = "c => c.startsWith('something')".asJsExpression()))
driver.findElements(role(Heading, nameAsFunction = JsFunction("c => c.startsWith('something')")))

assertEquals("something as a user something", result.single().accessibleName)
}
Expand Down Expand Up @@ -175,7 +174,7 @@ class ByRoleTest {
)

val result = driver.findElements(
role(AlertDialog, descriptionAsFunction = JsExpression("content => content.endsWith('!')"))
role(AlertDialog, descriptionAsFunction = JsFunction("content => content.endsWith('!')"))
)

assertEquals("Your session is about to expire!", result.single().text.substringAfter("Close\n"))
Expand Down Expand Up @@ -322,7 +321,7 @@ class ByRoleTest {
of(Value(max = 10), listOf("Volume", "Pitch")),
of(Value(text = "medium"), listOf("Volume", "Pitch")),
of(Value(textAsRegex = Pattern.compile("med")), listOf("Volume", "Pitch")),
of(Value(textAsFunction = "t => t.startsWith('med')".asJsExpression()), listOf("Volume", "Pitch")),
of(Value(textAsFunction = JsFunction("t => t.startsWith('med')")), listOf("Volume", "Pitch")),
)

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seleniumtestinglib.locators

import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.testId
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -32,7 +32,7 @@ class ByTestIdTest {
fun function() {
driver.render(""" <div data-testid="custom-element" /> """)

val result = driver.findElement(testId("c => c.startsWith('custom')".asJsExpression()))
val result = driver.findElement(testId(JsFunction("c => c.startsWith('custom')")))

assertEquals("div", result.tagName)
}
Expand Down
28 changes: 14 additions & 14 deletions lib/src/test/kotlin/seleniumtestinglib/locators/ByTextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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.TL
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.text
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand All @@ -19,7 +19,7 @@ class ByTextTest {
fun `by text`() {
driver.render("<article><span>I accept</span><div></div></article>")

val result = driver.findElement(TL.text("I accept"))
val result = driver.findElement(text("I accept"))

assertEquals("span", result.tagName)
}
Expand All @@ -28,7 +28,7 @@ class ByTextTest {
fun `ensure quotes are escaped`() {
driver.render(""""<article><span>with quotes"'`</span><div></div></article>""")

val result = driver.findElement(TL.By.text("""with quotes"'`"""))
val result = driver.findElement(text("""with quotes"'`"""))

assertEquals("span", result.tagName)
}
Expand All @@ -37,7 +37,7 @@ class ByTextTest {
fun `with a selector`() {
driver.render("<article><span>Username</span><div>Username</div></article>")

val result = driver.findElement(TL.text("Username", selector = "div"))
val result = driver.findElement(text("Username", selector = "div"))

assertEquals("div", result.tagName)
}
Expand All @@ -47,7 +47,7 @@ class ByTextTest {
driver.render("<span></span>")

val result = runCatching {
driver.findElement(TL.text("abc"))
driver.findElement(text("abc"))
}

assertTrue(result.exceptionOrNull() is NoSuchElementException)
Expand All @@ -58,7 +58,7 @@ class ByTextTest {
driver.render("<span>Username</span><div>Username</div>")

val result = runCatching {
driver.findElement(TL.text("Username", selector = "x"))
driver.findElement(text("Username", selector = "x"))
}

assertTrue(result.exceptionOrNull() is NoSuchElementException)
Expand All @@ -76,7 +76,7 @@ class ByTextTest {
"""
)

val result = driver.findElement(TL.text("accept", exact = false, selector = "div p"))
val result = driver.findElement(text("accept", exact = false, selector = "div p"))

assertEquals("p", result.tagName)
}
Expand All @@ -85,7 +85,7 @@ class ByTextTest {
fun regex() {
driver.render("<p>I accept</p>")

val result = driver.findElement(TL.text(Pattern.compile("ACCEPT", Pattern.CASE_INSENSITIVE)))
val result = driver.findElement(text(Pattern.compile("ACCEPT", Pattern.CASE_INSENSITIVE)))

assertEquals("p", result.tagName)
}
Expand All @@ -95,7 +95,7 @@ class ByTextTest {
driver.render("<p>Hello World</p>")

val result = driver.findElement(
TL.text("(content, element) => content.startsWith('Hello') && element.tagName == 'P'".asJsExpression())
text(JsFunction("(content, element) => content.startsWith('Hello') && element.tagName == 'P'"))
)

assertEquals("p", result.tagName)
Expand All @@ -105,7 +105,7 @@ class ByTextTest {
fun `case insensitive`() {
driver.render("<p>I accept</p>")

val result = driver.findElement(TL.text("ACCEPT", exact = false))
val result = driver.findElement(text("ACCEPT", exact = false))

assertEquals("p", result.tagName)
}
Expand All @@ -121,7 +121,7 @@ class ByTextTest {
"""
)

val result = driver.findElements(TL.text("I accept", ignore = ignore))
val result = driver.findElements(text("I accept", ignore = ignore))

assertEquals(expectedFound, result.size)
}
Expand All @@ -143,7 +143,7 @@ class ByTextTest {
"""
)

val result = driver.findElements(TL.text("I accept").ignore(false))
val result = driver.findElements(text("I accept").ignore(false))

assertEquals(3, result.size)
}
Expand All @@ -153,7 +153,7 @@ class ByTextTest {
driver.render("<p>I accept</p>")

val result = driver.findElement(
TL.text("I accept123", normalizer = "str => str + '123'".asJsExpression())
text("I accept123", normalizer = JsFunction( "str => str + '123'"))
)

assertEquals("p", result.tagName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seleniumtestinglib.locators

import seleniumtestinglib.JsFunction
import seleniumtestinglib.TL.By.title
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern
Expand Down Expand Up @@ -56,7 +56,7 @@ class ByTitleTest {
fun function() {
driver.render("<div title='foobar'>Hello World!</div>")

val result = driver.findElement(title("c => c.startsWith('foo')".asJsExpression()))
val result = driver.findElement(title(JsFunction("c => c.startsWith('foo')")))

assertEquals("Hello World!", result.text)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import org.junit.jupiter.params.provider.MethodSource
import org.openqa.selenium.By
import org.openqa.selenium.NoSuchElementException
import seleniumtestinglib.Current.Page
import seleniumtestinglib.JsFunction
import seleniumtestinglib.Role.SpinButton
import seleniumtestinglib.TL.By.altText
import seleniumtestinglib.TL.By.placeholderText
import seleniumtestinglib.TL.By.role
import seleniumtestinglib.TL.By.text
import seleniumtestinglib.Value
import seleniumtestinglib.asJsExpression
import seleniumtestinglib.driver
import seleniumtestinglib.render
import java.util.regex.Pattern.CASE_INSENSITIVE
Expand All @@ -29,7 +29,7 @@ class NotFoundTest {
"ByText(/a/i)"
),
of(
placeholderText("el => true".asJsExpression()),
placeholderText(JsFunction("el => true")),
"ByPlaceholderText(el => true)"
),
of(
Expand Down

0 comments on commit 544fab7

Please sign in to comment.