Skip to content

Commit 5226ac9

Browse files
committed
Extract HTTP API tests, which are not supported by Browserless
1 parent e630e06 commit 5226ac9

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

src/jvmTest/kotlin/BrowserlessLocalIntegrationTests.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import org.testcontainers.containers.*
22
import org.testcontainers.junit.jupiter.*
33
import org.testcontainers.junit.jupiter.Container
44
import org.testcontainers.utility.*
5-
import kotlin.test.*
65

76
@Testcontainers
87
class BrowserlessLocalIntegrationTests : IntegrationTestBase() {
98

109
/**
11-
* A container running the Browserless with Chromium support.
10+
* A container running Browserless with Chromium support.
1211
* It is meant to be used mostly with the web socket API, which is accessible directly at `ws://localhost:{port}`
1312
* (no need for an intermediate HTTP call).
1413
*
15-
* It provides a bridge to the JSON HTTP API of the DevTools protocol as well, but only for a subset of the
16-
* endpoints. See [Browser REST APIs](https://docs.browserless.io/open-api#tag/Browser-REST-APIs) in the docs.
17-
*
18-
* Also, there is [a bug](https://github.com/browserless/browserless/issues/4566) with the `/json/new` endpoint.
14+
* It provides a bridge to the HTTP endpoints `/json/protocol` and `/json/version` of the DevTools protocol as well,
15+
* but not for endpoints related to tab manipulation.
16+
* The `/json/list` and `/json/new` endpoints are just mocked to allow some clients to get the WS URL, but they
17+
* don't actually reflect the reality or affect the browser's state.
18+
* See [Browser REST APIs](https://docs.browserless.io/open-api#tag/Browser-REST-APIs) in the docs.
1919
*/
2020
@Container
2121
var browserlessChromium: GenericContainer<*> = GenericContainer("ghcr.io/browserless/chromium:latest")
@@ -27,9 +27,4 @@ class BrowserlessLocalIntegrationTests : IntegrationTestBase() {
2727

2828
override val wsConnectUrl: String
2929
get() = "ws://localhost:${browserlessChromium.firstMappedPort}"
30-
31-
@Ignore("The /json/new endpoint doesn't work with the HTTP API of Browserless: " +
32-
"https://github.com/browserless/browserless/issues/4566")
33-
override fun httpTabEndpoints() {
34-
}
3530
}

src/jvmTest/kotlin/IntegrationTestBase.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ abstract class IntegrationTestBase {
6565
}
6666
}
6767

68-
@Test
69-
open fun httpTabEndpoints() {
70-
runBlockingWithTimeout {
71-
val chrome = chromeHttp()
72-
73-
@Suppress("DEPRECATION") // the point is to test this deprecated API
74-
val googleTab = chrome.newTab(url = "https://www.google.com")
75-
assertEquals("https://www.google.com", googleTab.url.trimEnd('/'))
76-
77-
val targets = chrome.targets()
78-
assertTrue(targets.any { it.url.trimEnd('/') == "https://www.google.com" }, "the google.com page target should be listed, got: $targets")
79-
80-
chrome.closeTab(googleTab.id)
81-
}
82-
}
83-
8468
@OptIn(ExperimentalChromeApi::class)
8569
@Test
8670
fun webSocket_basic() {

src/jvmTest/kotlin/ZenikaIntegrationTests.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import org.hildan.chrome.devtools.protocol.ChromeDPTarget
2+
import org.junit.jupiter.api.Test
13
import org.testcontainers.containers.*
24
import org.testcontainers.junit.jupiter.*
35
import org.testcontainers.junit.jupiter.Container
46
import org.testcontainers.utility.*
57
import kotlin.test.Ignore
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertTrue
610

711
@Testcontainers
812
class ZenikaIntegrationTests : LocalIntegrationTestBase() {
@@ -22,10 +26,65 @@ class ZenikaIntegrationTests : LocalIntegrationTestBase() {
2226
override val httpUrl: String
2327
get() = "http://localhost:${zenikaChrome.firstMappedPort}"
2428

29+
// the WS URL is not known in advance and needs to be queried first via the HTTP API, hence the HTTP URL here
2530
override val wsConnectUrl: String
2631
get() = "http://localhost:${zenikaChrome.firstMappedPort}"
2732

2833
@Ignore("The Zenika container seems out of data and still treats cookiePartitionKey as a string instead of object")
2934
override fun missingExpiresInCookie() {
3035
}
36+
37+
@Suppress("DEPRECATION") // the point is to test this deprecated API
38+
@Test
39+
fun httpTabEndpoints_basic() {
40+
runBlockingWithTimeout {
41+
val chrome = chromeHttp()
42+
val originalTabCount = chrome.targets().size
43+
44+
val newTab = chrome.newTab()
45+
assertEquals("about:blank", newTab.url.trimEnd('/'))
46+
assertTargetCount(originalTabCount + 1, chrome.targets())
47+
chrome.closeTab(newTab.id)
48+
49+
assertTargetCount(originalTabCount, chrome.targets())
50+
chrome.newTab()
51+
chrome.newTab()
52+
chrome.closeAllTargets()
53+
assertTargetCount(0, chrome.targets())
54+
}
55+
}
56+
57+
@Suppress("DEPRECATION") // the point is to test this deprecated API
58+
@Test
59+
fun httpTabEndpoints_newTabWithCustomUrl() {
60+
runBlockingWithTimeout {
61+
val chrome = chromeHttp()
62+
63+
val googleTab = chrome.newTab(url = "https://www.google.com")
64+
assertEquals("https://www.google.com", googleTab.url.trimEnd('/'))
65+
66+
val targets = chrome.targets()
67+
assertTrue(
68+
actual = targets.any { it.url.trimEnd('/') == "https://www.google.com" },
69+
message = "the google.com page target should be listed, got:\n${targets.joinToString("\n")}",
70+
)
71+
72+
chrome.closeTab(googleTab.id)
73+
74+
val targetsAfterClose = chrome.targets()
75+
assertTrue(
76+
actual = targetsAfterClose.none { it.url.trimEnd('/') == "https://www.google.com" },
77+
message = "the google.com page target should be closed, got:\n${targetsAfterClose.joinToString("\n")}",
78+
)
79+
}
80+
}
81+
82+
private fun assertTargetCount(expected: Int, currentTargets: List<ChromeDPTarget>) {
83+
assertEquals(
84+
expected = expected,
85+
actual = currentTargets.size,
86+
message = "Expected $expected tab(s) but got ${currentTargets.size} instead:\n" +
87+
currentTargets.joinToString("\n"),
88+
)
89+
}
3190
}

0 commit comments

Comments
 (0)