Skip to content

Commit

Permalink
test: custom content in local http server (#2374)
Browse files Browse the repository at this point in the history
* Add possibility to provide a URL with a custom content.
* Remove `data:text/html` fixture
* Address concurrency issue in local http server by adding several
instances.
  • Loading branch information
sadym-chromium authored Jul 2, 2024
1 parent f4daf67 commit be5e61b
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 140 deletions.
8 changes: 4 additions & 4 deletions tests/browsing_context/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ async def test_browsingContext_create_eventContextCreatedEmitted(
async def test_browsingContext_createWithNestedSameOriginContexts_eventContextCreatedEmitted(
websocket, context_id, html, iframe):
nested_iframe = html('<h1>PAGE_WITHOUT_CHILD_IFRAMES</h1>')
intermediate_page = html('<h1>PAGE_WITH_1_CHILD_IFRAME</h1>' +
iframe(nested_iframe.replace('"', '&quot;')))
top_level_page = html('<h1>PAGE_WITH_2_CHILD_IFRAMES</h1>' +
iframe(intermediate_page.replace('"', '&quot;')))
intermediate_page = html(
f'<h1>PAGE_WITH_1_CHILD_IFRAME</h1>{iframe(nested_iframe)}')
top_level_page = html(
f'<h1>PAGE_WITH_2_CHILD_IFRAMES</h1>{iframe(intermediate_page)}')

await subscribe(websocket, ["browsingContext.contextCreated"])

Expand Down
47 changes: 6 additions & 41 deletions tests/browsing_context/test_get_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,55 +62,20 @@ async def test_browsingContext_getTreeWithRoot_contextReturned(websocket):


@pytest.mark.asyncio
async def test_browsingContext_afterNavigation_getTreeWithNestedCrossOriginContexts_contextsReturned(
websocket, context_id, html, iframe, url_example, url_another_example):
page_with_nested_iframe = html(iframe(url_example))
another_page_with_nested_iframe = html(iframe(url_another_example))

await goto_url(websocket, context_id, page_with_nested_iframe, "complete")
await goto_url(websocket, context_id, another_page_with_nested_iframe,
"complete")

result = await get_tree(websocket)

assert {
"contexts": [{
"context": context_id,
"children": [{
"context": ANY_STR,
"url": url_another_example,
"children": [],
"userContext": "default",
"originalOpener": None
}],
"parent": None,
"url": another_page_with_nested_iframe,
"userContext": "default",
"originalOpener": None
}]
} == result


@pytest.mark.asyncio
async def test_browsingContext_afterNavigation_getTreeWithNestedContexts_contextsReturned(
websocket, context_id, html, iframe):
nested_iframe = html('<h2>IFRAME</h2>')
another_nested_iframe = html('<h2>ANOTHER_IFRAME</h2>')
page_with_nested_iframe = html('<h1>MAIN_PAGE</h1>' +
iframe(nested_iframe))
another_page_with_nested_iframe = html('<h1>ANOTHER_MAIN_PAGE</h1>' +
iframe(another_nested_iframe))
async def test_browsingContext_afterNavigation_getTree_contextsReturned(
websocket, context_id, html, iframe, url_all_origins):
page_with_nested_iframe = html(iframe(url_all_origins))
another_page_with_nested_iframe = html(iframe(url_all_origins))

await goto_url(websocket, context_id, page_with_nested_iframe, "complete")

result = await get_tree(websocket)

assert {
"contexts": [{
"context": context_id,
"children": [{
"context": ANY_STR,
"url": nested_iframe,
"url": url_all_origins,
"children": [],
"userContext": "default",
"originalOpener": None
Expand All @@ -131,7 +96,7 @@ async def test_browsingContext_afterNavigation_getTreeWithNestedContexts_context
"context": context_id,
"children": [{
"context": ANY_STR,
"url": another_nested_iframe,
"url": url_all_origins,
"children": [],
"userContext": "default",
"originalOpener": None
Expand Down
6 changes: 3 additions & 3 deletions tests/browsing_context/test_navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def test_browsingContext_navigateWaitNone_navigated(
"context": context_id,
"navigation": navigation_id,
"timestamp": ANY_TIMESTAMP,
"url": html("<h2>test</h2>")
"url": url
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ async def test_browsingContext_navigateWaitInteractive_navigated(
"type": "success",
"result": {
"navigation": navigation_id,
"url": html("<h2>test</h2>")
"url": url
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ async def test_browsingContext_navigateWaitComplete_navigated(
"context": context_id,
"navigation": navigation_id,
"timestamp": ANY_TIMESTAMP,
"url": html("<h2>test</h2>")
"url": url
}
}

Expand Down
39 changes: 28 additions & 11 deletions tests/browsing_context/test_nested_browsing_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ async def test_nestedBrowsingContext_navigateSameDocumentNavigation_waitComplete

@pytest.mark.asyncio
async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedCrossOriginContexts_contextsReturned(
websocket, iframe_id, html, iframe, url_example, url_another_example):
websocket, iframe_id, html, iframe, url_example,
url_example_another_origin):
page_with_nested_iframe = html(iframe(url_example))
another_page_with_nested_iframe = html(iframe(url_another_example))
another_page_with_nested_iframe = html(iframe(url_example_another_origin))

await goto_url(websocket, iframe_id, page_with_nested_iframe, "complete")
await goto_url(websocket, iframe_id, another_page_with_nested_iframe,
Expand All @@ -277,7 +278,7 @@ async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedCrossOrigi
"context": iframe_id,
"children": [{
"context": ANY_STR,
"url": url_another_example,
"url": url_example_another_origin,
"children": [],
"userContext": "default",
"originalOpener": None
Expand All @@ -291,28 +292,44 @@ async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedCrossOrigi


@pytest.mark.asyncio
async def test_nestedBrowsingContext_afterNavigation_getTreeWithNestedContexts_contextsReturned(
websocket, iframe_id, html, iframe):
nested_iframe = html('<h2>IFRAME</h2>')
another_nested_iframe = html('<h2>ANOTHER_IFRAME</h2>')
async def test_nestedBrowsingContext_afterNavigation_getTree_contextsReturned(
websocket, iframe_id, html, iframe, url_all_origins):
page_with_nested_iframe = html('<h1>MAIN_PAGE</h1>' +
iframe(nested_iframe))
iframe(url_all_origins))
another_page_with_nested_iframe = html('<h1>ANOTHER_MAIN_PAGE</h1>' +
iframe(another_nested_iframe))
iframe(url_all_origins))

await goto_url(websocket, iframe_id, page_with_nested_iframe, "complete")

result = await get_tree(websocket, iframe_id)
assert {
"contexts": [{
"context": iframe_id,
"url": page_with_nested_iframe,
"children": [{
"context": ANY_STR,
"url": url_all_origins,
"children": [],
"userContext": "default",
"originalOpener": None
}],
"parent": ANY_STR,
"userContext": "default",
"originalOpener": None
}]
} == result

await goto_url(websocket, iframe_id, another_page_with_nested_iframe,
"complete")

result = await get_tree(websocket, iframe_id)

assert {
"contexts": [{
"context": iframe_id,
"url": another_page_with_nested_iframe,
"children": [{
"context": ANY_STR,
"url": another_nested_iframe,
"url": url_all_origins,
"children": [],
"userContext": "default",
"originalOpener": None
Expand Down
19 changes: 12 additions & 7 deletions tests/browsing_context/test_traverse_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,33 @@


@pytest.mark.asyncio
async def test_traverse_history(websocket, context_id, html):
async def test_traverse_history(websocket, context_id):
urls = []
for i in range(HISTORY_LENGTH + 1):
await goto_url(websocket, context_id, html(i))
# TODO: use `html` fixture instead.
# https://github.com/GoogleChromeLabs/chromium-bidi/issues/2376
url = f'data:text/html,{i}'
urls.append(url)
await goto_url(websocket, context_id, url)

await subscribe(websocket, ["browsingContext.load"])

await traverse_history(websocket, context_id, -2)
await assert_href_equals(websocket, html(HISTORY_LENGTH - 2))
await assert_href_equals(websocket, urls[HISTORY_LENGTH - 2])

await traverse_history(websocket, context_id, 2)
await assert_href_equals(websocket, html(HISTORY_LENGTH))
await assert_href_equals(websocket, urls[HISTORY_LENGTH])

await traverse_history(websocket, context_id, -1)
await assert_href_equals(websocket, html(HISTORY_LENGTH - 1))
await assert_href_equals(websocket, urls[HISTORY_LENGTH - 1])

await traverse_history(websocket, context_id, 1)
await assert_href_equals(websocket, html(HISTORY_LENGTH))
await assert_href_equals(websocket, urls[HISTORY_LENGTH])

# There is no event here.
await traverse_history(websocket, context_id, 0)
await assert_location_href_equals(websocket, context_id,
html(HISTORY_LENGTH))
urls[HISTORY_LENGTH])


@pytest.mark.asyncio
Expand Down
55 changes: 30 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

@pytest_asyncio.fixture(scope='session')
def local_server_http() -> Generator[LocalHttpServer, None, None]:
""" Returns an instance of a LocalHttpServer without SSL. """
"""
Returns an instance of a LocalHttpServer without SSL pointing to localhost.
"""
server = LocalHttpServer()
yield server

Expand All @@ -40,6 +42,20 @@ def local_server_http() -> Generator[LocalHttpServer, None, None]:
return


@pytest_asyncio.fixture(scope='session')
def local_server_http_another_host() -> Generator[LocalHttpServer, None, None]:
"""
Returns an instance of a LocalHttpServer without SSL pointing to `127.0.0.1`
"""
server = LocalHttpServer('127.0.0.1')
yield server

server.clear()
if server.is_running():
server.stop()
return


@pytest_asyncio.fixture(scope='session')
def local_server_bad_ssl() -> Generator[LocalHttpServer, None, None]:
""" Returns an instance of a LocalHttpServer with bad SSL certificate. """
Expand Down Expand Up @@ -208,19 +224,14 @@ async def sandbox_realm(websocket, context_id: str):
return result["realm"]


@pytest.fixture
def url_same_origin():
"""Return a same-origin URL."""
return 'about:blank'


@pytest.fixture(
params=['url_example', 'url_another_example', 'html', 'about:blank'])
def url_all_origins(request, url_example, url_another_example, html):
@pytest.fixture(params=[
'url_example', 'url_example_another_origin', 'html', 'about:blank'
])
def url_all_origins(request, url_example, url_example_another_origin, html):
if request.param == 'url_example':
return url_example
if request.param == 'url_another_example':
return url_another_example
if request.param == 'url_example_another_origin':
return url_example_another_origin
if request.param == 'html':
return html('data:text/html,<h2>some page</h2>')
if request.param == 'about:blank':
Expand All @@ -241,10 +252,10 @@ def url_example(local_server_http):


@pytest.fixture
def url_another_example(local_server_http):
def url_example_another_origin(local_server_http_another_host):
"""Return a generic example URL with status code 200, in a domain other than
the example_url fixture."""
return local_server_http.url_200('127.0.0.1')
return local_server_http_another_host.url_200()


@pytest.fixture
Expand Down Expand Up @@ -397,10 +408,10 @@ async def activate_main_tab():


@pytest.fixture
def html():
"""Return a factory for HTML data URL with the given content."""
def html(local_server_http):
"""Return a factory for URL with the given content."""
def html(content=""):
return f'data:text/html,{content}'
return local_server_http.url_200(content=content)

return html

Expand All @@ -414,17 +425,11 @@ def iframe(src=""):
return iframe


@pytest.fixture
def html_iframe_same_origin(html, iframe, url_same_origin):
"""Return a page URL with an iframe of the same origin."""
return html(iframe(url_same_origin))


@pytest_asyncio.fixture
async def iframe_id(websocket, context_id: str, html_iframe_same_origin, html):
async def iframe_id(websocket, context_id, html, iframe):
"""Navigate to a page with an iframe of the same origin, and return the
iframe browser context id."""
await goto_url(websocket, context_id, html_iframe_same_origin)
await goto_url(websocket, context_id, html(iframe(html("<h1>FRAME</h1>"))))
result = await get_tree(websocket, context_id)

iframe_id = result["contexts"][0]["children"][0]["context"]
Expand Down
9 changes: 6 additions & 3 deletions tests/log/test_log_entry_added.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,12 @@ async def test_exceptionThrown_logEntryAddedEventEmitted(
websocket, context_id, html):
await subscribe(websocket, ["log.entryAdded"])

url = html("<script>throw new Error('some error')</script>")
await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": html("<script>throw new Error('some error')</script>"),
"url": url,
"wait": "interactive",
"context": context_id
}
Expand All @@ -353,10 +354,12 @@ async def test_exceptionThrown_logEntryAddedEventEmitted(
"timestamp": ANY_TIMESTAMP,
"stackTrace": {
"callFrames": [{
"url": "",
"url": url,
"functionName": "",
"lineNumber": 0,
"columnNumber": 14
# Column number is a magical constant. It depends on the
# html fixture wrapping content in document tag.
"columnNumber": 127
}]
},
# ConsoleLogEntry
Expand Down
Loading

0 comments on commit be5e61b

Please sign in to comment.