Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
should reduce memory usage significantly, but some local unit tests f…
Browse files Browse the repository at this point in the history
…ailing
  • Loading branch information
sanity committed May 27, 2023
1 parent 72bf50d commit d2c0127
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
26 changes: 13 additions & 13 deletions src/main/kotlin/kweb/ElementCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ open class ElementCreator<out PARENT_TYPE : Element>(

val id: String = mutAttributes.computeIfAbsent("id") { JsonPrimitive("K" + browser.generateId()) }.content
val htmlDoc = browser.htmlDocument.get()
val createElementStatement = when (namespace) {
null -> "document.createElement(tag);"
else -> "document.createElementNS(\"${namespace}\", tag);"
}
when {
htmlDoc != null -> {
val parentElement = when (element) {
Expand All @@ -92,14 +88,16 @@ open class ElementCreator<out PARENT_TYPE : Element>(
}

element.browser.isCatchingOutbound() != null -> {

//language=JavaScript
val createElementJs = """
let tag = {};
let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
let newEl = $createElementStatement
let namespace = {};
let newEl = namespace ? document.createElementNS(namespace, tag) : document.createElement(tag);
newEl.setAttribute("id", myId);
for (const key in attributes) {
if ( key !== "id") {
Expand All @@ -114,10 +112,11 @@ if (insertBefore !== undefined) {
} else {
parentElement.appendChild(newEl);
}
"""
"""

browser.callJsFunction(
createElementJs, JsonPrimitive(tag), JsonObject(mutAttributes), id.json,
JsonPrimitive(element.id), JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
createElementJs, JsonPrimitive(tag), JsonObject(mutAttributes), JsonPrimitive(id),
JsonPrimitive(element.id), JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount), JsonPrimitive(namespace ?: "")
)
}

Expand All @@ -130,12 +129,13 @@ let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
let newEl = document.createElement(tag);
if (attributes["id"] === undefined) {
let namespace = {};
let newEl = namespace ? document.createElementNS(namespace, tag) : document.createElement(tag);
if (!attributes["id"]) {
newEl.setAttribute("id", myId);
}
for (const key in attributes) {
newEl.setAttribute(key, attributes[key]);
newEl.setAttribute(key, attributes[key]);
}
let parentElement = document.getElementById(parentId);
let startNode = document.getElementById(insertBefore)
Expand All @@ -145,10 +145,10 @@ if (insertBefore !== undefined) {
} else {
parentElement.appendChild(newEl);
}
"""
"""
element.browser.callJsFunction(
createElementJs, tag.json, JsonObject(mutAttributes), id.json,
element.id.json, JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
element.id.json, JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount), JsonPrimitive(namespace ?: "")
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/kweb/WebBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class WebBrowser(val sessionId: String, val httpRequestInfo: HttpRequestInfo, va
if (outboundMessageCatcher == null) {
kweb.callJs(sessionId, functionCall, debugInfo)
} else {
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher")
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher type ${outboundMessageCatcher.catcherType}")
outboundMessageCatcher.functionList.add(functionCall)
//If we are collecting calls for an immediate event, we run the risk of the client calling JS code that has yet to be cached
//A functionCall object having a non null js parameter, means the function has not been cached.
Expand Down Expand Up @@ -220,7 +220,7 @@ class WebBrowser(val sessionId: String, val httpRequestInfo: HttpRequestInfo, va
if (outboundMessageCatcher == null) {
kweb.callJs(sessionId, functionCall, debugInfo)
} else {
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher")
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher type ${outboundMessageCatcher.catcherType}")
outboundMessageCatcher.functionList.add(functionCall)
if (outboundMessageCatcher.catcherType == CatcherType.IMMEDIATE_EVENT) {
if (functionCall.js != null) {
Expand Down
15 changes: 10 additions & 5 deletions src/test/kotlin/kweb/demos/todo/TodoDemoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kweb.demos.todo

import io.github.bonigarcia.seljup.Options
import io.github.bonigarcia.seljup.SeleniumJupiter
import io.kotest.matchers.ints.shouldBeExactly
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.ktor.util.*
Expand Down Expand Up @@ -64,8 +66,10 @@ class TodoDemoTest {
val todoItem = "Right eyelids closed, both feet behind"
val site = TodoSite(driver)
site.addTodoItem(todoItem)
await().pollInSameThread().untilAsserted {
site.driver.findElement(By.xpath("//${"div"}[text()='$todoItem']")).isDisplayed shouldBe true
await().untilAsserted {
val matchingCount = site.driver.findElements(By.xpath("//div[@class='content']"))
.count { it.text.contains(todoItem) }
matchingCount shouldBeExactly 1
}
}

Expand All @@ -84,9 +88,10 @@ class TodoDemoTest {
site.addTodoItem(todoItem)

//make sure it appears for second driver
await().pollInSameThread().untilAsserted {
site2.driver.findElement(By.xpath("//${"div"}[text()='$todoItem']")).isDisplayed shouldBe true
}
await().untilAsserted {
val matchingCount = site.driver.findElements(By.xpath("//div[@class='content']"))
.count { it.text.contains(todoItem) }
matchingCount shouldBeExactly 1 }
}

@Test
Expand Down

0 comments on commit d2c0127

Please sign in to comment.