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

Commit d2c0127

Browse files
committed
should reduce memory usage significantly, but some local unit tests failing
1 parent 72bf50d commit d2c0127

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/main/kotlin/kweb/ElementCreator.kt

+13-13
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ open class ElementCreator<out PARENT_TYPE : Element>(
6767

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

9490
element.browser.isCatchingOutbound() != null -> {
91+
9592
//language=JavaScript
9693
val createElementJs = """
9794
let tag = {};
9895
let attributes = {};
9996
let myId = {};
10097
let parentId = {};
10198
let insertBefore = {};
102-
let newEl = $createElementStatement
99+
let namespace = {};
100+
let newEl = namespace ? document.createElementNS(namespace, tag) : document.createElement(tag);
103101
newEl.setAttribute("id", myId);
104102
for (const key in attributes) {
105103
if ( key !== "id") {
@@ -114,10 +112,11 @@ if (insertBefore !== undefined) {
114112
} else {
115113
parentElement.appendChild(newEl);
116114
}
117-
"""
115+
"""
116+
118117
browser.callJsFunction(
119-
createElementJs, JsonPrimitive(tag), JsonObject(mutAttributes), id.json,
120-
JsonPrimitive(element.id), JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
118+
createElementJs, JsonPrimitive(tag), JsonObject(mutAttributes), JsonPrimitive(id),
119+
JsonPrimitive(element.id), JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount), JsonPrimitive(namespace ?: "")
121120
)
122121
}
123122

@@ -130,12 +129,13 @@ let attributes = {};
130129
let myId = {};
131130
let parentId = {};
132131
let insertBefore = {};
133-
let newEl = document.createElement(tag);
134-
if (attributes["id"] === undefined) {
132+
let namespace = {};
133+
let newEl = namespace ? document.createElementNS(namespace, tag) : document.createElement(tag);
134+
if (!attributes["id"]) {
135135
newEl.setAttribute("id", myId);
136136
}
137137
for (const key in attributes) {
138-
newEl.setAttribute(key, attributes[key]);
138+
newEl.setAttribute(key, attributes[key]);
139139
}
140140
let parentElement = document.getElementById(parentId);
141141
let startNode = document.getElementById(insertBefore)
@@ -145,10 +145,10 @@ if (insertBefore !== undefined) {
145145
} else {
146146
parentElement.appendChild(newEl);
147147
}
148-
"""
148+
"""
149149
element.browser.callJsFunction(
150150
createElementJs, tag.json, JsonObject(mutAttributes), id.json,
151-
element.id.json, JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
151+
element.id.json, JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount), JsonPrimitive(namespace ?: "")
152152
)
153153
}
154154
}

src/main/kotlin/kweb/WebBrowser.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class WebBrowser(val sessionId: String, val httpRequestInfo: HttpRequestInfo, va
177177
if (outboundMessageCatcher == null) {
178178
kweb.callJs(sessionId, functionCall, debugInfo)
179179
} else {
180-
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher")
180+
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher type ${outboundMessageCatcher.catcherType}")
181181
outboundMessageCatcher.functionList.add(functionCall)
182182
//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
183183
//A functionCall object having a non null js parameter, means the function has not been cached.
@@ -220,7 +220,7 @@ class WebBrowser(val sessionId: String, val httpRequestInfo: HttpRequestInfo, va
220220
if (outboundMessageCatcher == null) {
221221
kweb.callJs(sessionId, functionCall, debugInfo)
222222
} else {
223-
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher")
223+
logger.debug("Temporarily storing message for $sessionId in threadlocal outboundMessageCatcher type ${outboundMessageCatcher.catcherType}")
224224
outboundMessageCatcher.functionList.add(functionCall)
225225
if (outboundMessageCatcher.catcherType == CatcherType.IMMEDIATE_EVENT) {
226226
if (functionCall.js != null) {

src/test/kotlin/kweb/demos/todo/TodoDemoTest.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package kweb.demos.todo
22

33
import io.github.bonigarcia.seljup.Options
44
import io.github.bonigarcia.seljup.SeleniumJupiter
5+
import io.kotest.matchers.ints.shouldBeExactly
6+
import io.kotest.matchers.ints.shouldBeGreaterThan
57
import io.kotest.matchers.shouldBe
68
import io.kotest.matchers.shouldNotBe
79
import io.ktor.util.*
@@ -64,8 +66,10 @@ class TodoDemoTest {
6466
val todoItem = "Right eyelids closed, both feet behind"
6567
val site = TodoSite(driver)
6668
site.addTodoItem(todoItem)
67-
await().pollInSameThread().untilAsserted {
68-
site.driver.findElement(By.xpath("//${"div"}[text()='$todoItem']")).isDisplayed shouldBe true
69+
await().untilAsserted {
70+
val matchingCount = site.driver.findElements(By.xpath("//div[@class='content']"))
71+
.count { it.text.contains(todoItem) }
72+
matchingCount shouldBeExactly 1
6973
}
7074
}
7175

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

8690
//make sure it appears for second driver
87-
await().pollInSameThread().untilAsserted {
88-
site2.driver.findElement(By.xpath("//${"div"}[text()='$todoItem']")).isDisplayed shouldBe true
89-
}
91+
await().untilAsserted {
92+
val matchingCount = site.driver.findElements(By.xpath("//div[@class='content']"))
93+
.count { it.text.contains(todoItem) }
94+
matchingCount shouldBeExactly 1 }
9095
}
9196

9297
@Test

0 commit comments

Comments
 (0)