|
| 1 | +package io.github.easy4j.opencli.contract; |
| 2 | + |
| 3 | +import io.github.easy4j.opencli.OpenCliClient; |
| 4 | +import io.github.easy4j.opencli.OpenCliProperties; |
| 5 | +import io.github.easy4j.opencli.browser.OpenCliBrowserSession; |
| 6 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserOpenResult; |
| 7 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult; |
| 8 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status; |
| 9 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserSnapshot; |
| 10 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison; |
| 11 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserTab; |
| 12 | +import io.github.easy4j.opencli.browser.support.OpenCliBrowserSnapshotSource; |
| 13 | +import java.net.URI; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +/** Opt-in verification against an externally provisioned disposable Browser Bridge. */ |
| 24 | +class OpenCliBrowserBridgeIT { |
| 25 | + @Test |
| 26 | + void typedResultsFromAnIsolatedChromePage() { |
| 27 | + String fixtureUrl = requiredEnvironment("OPENCLI_BRIDGE_FIXTURE_URL"); |
| 28 | + URI uri = URI.create(fixtureUrl); |
| 29 | + assertEquals("http", uri.getScheme()); |
| 30 | + assertEquals("127.0.0.1", uri.getHost()); |
| 31 | + |
| 32 | + OpenCliProperties properties = new OpenCliProperties(); |
| 33 | + properties.setExecutable(requiredEnvironment("OPENCLI_INSTALLED_NODE")); |
| 34 | + properties.setLeadingArguments(Collections.singletonList(requiredEnvironment("OPENCLI_INSTALLED_ENTRY"))); |
| 35 | + properties.setCommandTimeoutMillis(30_000L); |
| 36 | + try (OpenCliClient client = new OpenCliClient(properties)) { |
| 37 | + OpenCliBrowserSession session = new OpenCliBrowserSession(client.getExecutor(), "sdk-bridge-fixture", "background"); |
| 38 | + OpenCliBrowserOpenResult opened = parsed(session.openTyped(fixtureUrl, null)); |
| 39 | + assertEquals(fixtureUrl, opened.getUrl()); |
| 40 | + assertNotNull(opened.getPageId()); |
| 41 | + |
| 42 | + List<OpenCliBrowserTab> tabs = parsed(session.tabListTyped()); |
| 43 | + assertTrue(tabs.stream().anyMatch(tab -> opened.getPageId().equals(tab.getPageId()) |
| 44 | + && fixtureUrl.equals(tab.getUrl()) && tab.isActive())); |
| 45 | + |
| 46 | + OpenCliBrowserSnapshot dom = parsed(session.snapshotTyped(null, OpenCliBrowserSnapshotSource.DOM)); |
| 47 | + assertEquals(fixtureUrl, dom.getUrl()); |
| 48 | + assertTrue(dom.getText().contains("Continue")); |
| 49 | + |
| 50 | + OpenCliBrowserSnapshot ax = parsed(session.snapshotTyped(null, OpenCliBrowserSnapshotSource.AX)); |
| 51 | + assertEquals(fixtureUrl, ax.getUrl()); |
| 52 | + assertTrue(ax.getText().contains("RootWebArea")); |
| 53 | + assertTrue(ax.getText().contains("Bridge fixture")); |
| 54 | + |
| 55 | + OpenCliBrowserSourceComparison comparison = parsed(session.compareStateSourcesTyped(null)); |
| 56 | + assertEquals(fixtureUrl, comparison.getUrl()); |
| 57 | + assertTrue(comparison.getDom().isOk()); |
| 58 | + assertTrue(comparison.getAx().isOk()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static String requiredEnvironment(String name) { |
| 63 | + return Objects.requireNonNull(System.getenv(name), name + " fixture is required"); |
| 64 | + } |
| 65 | + |
| 66 | + private static <T> T parsed(OpenCliBrowserResult<T> result) { |
| 67 | + assertEquals(Status.PARSED, result.getStatus()); |
| 68 | + assertNotNull(result.getValue()); |
| 69 | + return result.getValue(); |
| 70 | + } |
| 71 | +} |
0 commit comments