From 3250ab616e86643229b81c15ab56a91af765c948 Mon Sep 17 00:00:00 2001 From: Ian Clarke Date: Thu, 30 Mar 2023 20:41:37 -0500 Subject: [PATCH] fixes #454 --- src/test/kotlin/kweb/HistoryTest.kt | 4 +- src/test/kotlin/kweb/HrefTest.kt | 4 + src/test/kotlin/kweb/InputCheckedTest.kt | 2 +- .../kotlin/kweb/SelectInitialValueTest.kt | 2 +- src/test/kotlin/kweb/SelectValueTest.kt | 2 +- src/test/kotlin/kweb/StringDiffTest.kt | 2 +- .../kweb/state/render/RenderCleanupTest.kt | 115 ------------------ .../kweb/state/render/RenderEachTest.kt | 36 +++--- 8 files changed, 31 insertions(+), 136 deletions(-) delete mode 100755 src/test/kotlin/kweb/state/render/RenderCleanupTest.kt diff --git a/src/test/kotlin/kweb/HistoryTest.kt b/src/test/kotlin/kweb/HistoryTest.kt index a9cad29948..4351ed1c18 100644 --- a/src/test/kotlin/kweb/HistoryTest.kt +++ b/src/test/kotlin/kweb/HistoryTest.kt @@ -51,9 +51,7 @@ class HistoryTest { } @Test - fun testBackButton(@Arguments("--headless") driver : WebDriver) { - WebDriverManager.chromedriver().setup() - + fun testBackButton(driver : ChromeDriver) { historyTestApp.reloadCount.value shouldBe 0 driver.get("http://localhost:7665/0") diff --git a/src/test/kotlin/kweb/HrefTest.kt b/src/test/kotlin/kweb/HrefTest.kt index 4f42148a19..47027d8bf8 100644 --- a/src/test/kotlin/kweb/HrefTest.kt +++ b/src/test/kotlin/kweb/HrefTest.kt @@ -24,6 +24,10 @@ class HrefTest { companion object { private lateinit var hrefTestApp: HrefTestApp + init { + System.setProperty("webdriver.http.factory", "jdk-http-client") + } + @JvmStatic @BeforeAll fun setupServer() { diff --git a/src/test/kotlin/kweb/InputCheckedTest.kt b/src/test/kotlin/kweb/InputCheckedTest.kt index 45a04c7c76..0d18ee1740 100644 --- a/src/test/kotlin/kweb/InputCheckedTest.kt +++ b/src/test/kotlin/kweb/InputCheckedTest.kt @@ -20,7 +20,7 @@ import org.openqa.selenium.chrome.ChromeOptions import org.openqa.selenium.support.ThreadGuard @ExtendWith(SeleniumJupiter::class) -class InputCheckedTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { +class InputCheckedTest { companion object { private lateinit var inputCheckedTestApp: InputCheckedTestApp diff --git a/src/test/kotlin/kweb/SelectInitialValueTest.kt b/src/test/kotlin/kweb/SelectInitialValueTest.kt index ccc9d6c98f..8e331a6cc4 100644 --- a/src/test/kotlin/kweb/SelectInitialValueTest.kt +++ b/src/test/kotlin/kweb/SelectInitialValueTest.kt @@ -16,7 +16,7 @@ import org.openqa.selenium.chrome.ChromeOptions import org.openqa.selenium.support.ThreadGuard @ExtendWith(SeleniumJupiter::class) -class SelectInitialValueTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { +class SelectInitialValueTest { companion object { private lateinit var selectInitialValueTestApp: SelectInitialValueTestApp diff --git a/src/test/kotlin/kweb/SelectValueTest.kt b/src/test/kotlin/kweb/SelectValueTest.kt index e4b777abc3..f939fc9ed8 100644 --- a/src/test/kotlin/kweb/SelectValueTest.kt +++ b/src/test/kotlin/kweb/SelectValueTest.kt @@ -19,7 +19,7 @@ import org.openqa.selenium.support.ThreadGuard import org.openqa.selenium.support.ui.Select @ExtendWith(SeleniumJupiter::class) -class SelectValueTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { +class SelectValueTest { companion object { private lateinit var selectValueTestApp: SelectValueTestApp diff --git a/src/test/kotlin/kweb/StringDiffTest.kt b/src/test/kotlin/kweb/StringDiffTest.kt index dfc2c28117..fc00e4a69a 100644 --- a/src/test/kotlin/kweb/StringDiffTest.kt +++ b/src/test/kotlin/kweb/StringDiffTest.kt @@ -17,7 +17,7 @@ import org.openqa.selenium.chrome.ChromeOptions import org.openqa.selenium.support.ThreadGuard @ExtendWith(SeleniumJupiter::class) -class StringDiffTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { +class StringDiffTest { companion object { init { diff --git a/src/test/kotlin/kweb/state/render/RenderCleanupTest.kt b/src/test/kotlin/kweb/state/render/RenderCleanupTest.kt deleted file mode 100755 index d1cf82df77..0000000000 --- a/src/test/kotlin/kweb/state/render/RenderCleanupTest.kt +++ /dev/null @@ -1,115 +0,0 @@ -package kweb.state.render - -import io.github.bonigarcia.seljup.Options -import io.github.bonigarcia.seljup.SeleniumJupiter -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromJsonElement -import kweb.* -import kweb.plugins.fomanticUI.fomantic -import kweb.plugins.fomanticUI.fomanticUIPlugin -import kweb.state.KVar -import kweb.state.ReversibleFunction -import kweb.state.render -import org.junit.jupiter.api.AfterAll -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.extension.ExtendWith -import org.openqa.selenium.chrome.ChromeOptions -import org.openqa.selenium.firefox.FirefoxOptions - -@ExtendWith(SeleniumJupiter::class) -class RenderCleanupTest { - companion object { - private lateinit var renderCleanupTestApp: RenderCleanupTestApp - - @JvmStatic - @BeforeAll - fun setupServer() { - renderCleanupTestApp = RenderCleanupTestApp() - } - - @JvmStatic - @AfterAll - fun teardownServer() { - renderCleanupTestApp.server.close() - } - - //selenium-jupiter will automatically fall back if the first browser it tries doesn't work - //https://bonigarcia.github.io/selenium-jupiter/#generic-driver - @Options - var chromeOptions = ChromeOptions().apply { - setHeadless(true) - } - - @Options - var firefoxOptions = FirefoxOptions().apply { - setHeadless(true) - } - } - /* - @Test - fun initialRender(driver : ChromeDriver) { - driver.get("http://localhost:7659/") - val h1 = driver.findElement(By.tagName("H1")) - h1.shouldNotBeNull() - } - */ -} - -fun main() { - RenderCleanupTestApp() -} - -data class TaskList(val tasks: List) - -class RenderCleanupTestApp { - val taskList = KVar(emptyList()) - - val server: Kweb = Kweb(port = 7659, plugins = listOf(fomanticUIPlugin)) { - - val editing = KVar(false) - - doc.body { - render(editing) { _editing -> - if (_editing) { - div(fomantic.ui.form) { - div(fomantic.field) { - label().text("What tasks would you like to prioritize? (one per line)") - val ta = textArea() - ta.setValue(taskList.value.joinToString(separator = "\n")) - div(fomantic.ui.buttons) { - button(fomantic.ui.button, type = ButtonType.submit).text("Save") - .on("document.getElementById(${ta.id})").click { event -> - //TODO, I'm not sure about this change - taskList.value = Json.decodeFromJsonElement(event.retrieved) - //taskList.value = event.retrieved!!//.split('\n').map { it.trim() }.toList() - editing.value = false - } - button(fomantic.ui.button, type = ButtonType.submit).text("Cancel") - .on("document.getElementById(${ta.id}).value").click { - editing.value = false - } - } - } - } - } else { - render(taskList.map { it.size }) { listSize -> - div(fomantic.ui.bulleted.list) { - for (ix in 0 until listSize) { - div(fomantic.item).text(taskList[ix]) - } - } - button(fomantic.ui.button).text("Edit").on.click { - editing.value = true - } - Unit - } - } - } - } - } -} - -private val stringBool = object : ReversibleFunction(label = "bool -> string") { - override fun invoke(from: Boolean) = if (from) "true" else "false" - override fun reverse(original: Boolean, change: String) = change == "true" -} diff --git a/src/test/kotlin/kweb/state/render/RenderEachTest.kt b/src/test/kotlin/kweb/state/render/RenderEachTest.kt index bcd61df2a9..4d9580f159 100644 --- a/src/test/kotlin/kweb/state/render/RenderEachTest.kt +++ b/src/test/kotlin/kweb/state/render/RenderEachTest.kt @@ -1,6 +1,7 @@ package kweb.state.render import io.github.bonigarcia.seljup.Arguments +import io.github.bonigarcia.seljup.Options import io.github.bonigarcia.seljup.SeleniumJupiter import io.kotest.matchers.shouldBe import kweb.* @@ -13,15 +14,22 @@ import org.junit.jupiter.api.extension.ExtendWith import org.openqa.selenium.By import org.openqa.selenium.WebElement import org.openqa.selenium.chrome.ChromeDriver +import org.openqa.selenium.chrome.ChromeOptions import org.openqa.selenium.support.ThreadGuard @ExtendWith(SeleniumJupiter::class) -class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { +class RenderEachTest { - //ThreadGuard.protect ensures that the ChromeDriver can only be called by the thread that created it - //This should make this test thread safe. - val driver = ThreadGuard.protect(unprotectedDriver) + companion object { + init { + System.setProperty("webdriver.http.factory", "jdk-http-client") + } + @Options + var chromeOptions = ChromeOptions().apply { + addArguments("--headless=new") + } + } /*NOTE: Thread.sleep(50) is used throughout these tests. I had success with values as small as Thread.sleep(1) But for consistent success, I left it at 50. @@ -30,7 +38,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { It's an issue with Selenium, the client, clicking a button, and then giving the server literally 0 time to respond. */ @Test - fun prependItemTest() { + fun prependItemTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Horse")) val server = Kweb(port = 1240, buildPage = { @@ -63,7 +71,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun appendItemTest() { + fun appendItemTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Horse")) val server = Kweb(port = 1241, buildPage = { @@ -96,7 +104,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun insertItemTest() { + fun insertItemTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Horse")) val server = Kweb(port = 1242, buildPage = { @@ -129,7 +137,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun changeItemTest() { + fun changeItemTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear")) val server = Kweb(port = 1243, buildPage = { @@ -157,7 +165,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun deleteItemTest() { + fun deleteItemTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Aardvark", "Bear", "Cow", "Dog", "Elephant")) val server = Kweb(port = 1244, buildPage = { @@ -215,7 +223,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun moveItemFromEndToCenterTest() { + fun moveItemFromEndToCenterTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Moose", "Horse")) val server = Kweb(port = 1245, buildPage = { @@ -248,7 +256,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun moveItemFromStartToEnd() { + fun moveItemFromStartToEnd(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Moose", "Horse")) val server = Kweb(port = 1246, buildPage = { @@ -281,7 +289,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun moveItemFromEndToStart() { + fun moveItemFromEndToStart(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Moose", "Horse")) val server = Kweb(port = 1247, buildPage = { @@ -314,7 +322,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun ClearItemsTest() { + fun ClearItemsTest(driver : ChromeDriver) { val animals = ObservableList(mutableListOf("Dog", "Cat", "Bear", "Moose", "Horse")) val server = Kweb(port = 1248, buildPage = { @@ -343,7 +351,7 @@ class RenderEachTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) { } @Test - fun nestedRenderEachTest() { + fun nestedRenderEachTest(driver : ChromeDriver) { val planets = ObservableList(mutableListOf()) val jupiter = ObservableList(mutableListOf("Io", "Europa", "Ganymede", "Callisto")) val saturn = ObservableList(mutableListOf("Titan", "Rhea", "Enceladus", "Mimas", "Phoebe"))