Skip to content

Commit

Permalink
test: make test_scriptNavigate_checkEvents stable
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Dec 16, 2024
1 parent 960531f commit 1451212
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 91 deletions.
64 changes: 32 additions & 32 deletions src/bidiMapper/modules/context/NavigationTracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ describe('NavigationTracker', () => {
let eventManager: SinonStubbedInstance<EventManager>;
let initialNavigationId: string;

async function assertNoNavigationEvents() {
function assertNoNavigationEvents() {
// `eventManager.registerEvent` is safe do be used unbound.
// eslint-disable-next-line @typescript-eslint/unbound-method
sinon.assert.notCalled(eventManager.registerEvent);
}

async function assertNavigationEvent(
function assertNavigationEvent(
this: void,
eventName: string,
navigationId: string | sinon.SinonMatcher,
Expand Down Expand Up @@ -89,14 +89,14 @@ describe('NavigationTracker', () => {
navigationTracker.navigationCommandFinished(navigation, undefined);

// Assert navigation is not finished.
await assertNoNavigationEvents();
assertNoNavigationEvents();

// Fragment navigation should not update the current navigation.
assert.equal(navigationTracker.currentNavigationId, initialNavigationId);

navigationTracker.navigatedWithinDocument(SOME_URL, 'fragment');

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.FragmentNavigated,
navigation.navigationId,
SOME_URL,
Expand All @@ -114,7 +114,7 @@ describe('NavigationTracker', () => {
it('started', async () => {
const navigation = navigationTracker.createPendingNavigation(SOME_URL);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(navigation.url, SOME_URL);
assert.equal(navigationTracker.url, INITIAL_URL);
assert.equal(
Expand All @@ -124,7 +124,7 @@ describe('NavigationTracker', () => {

navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
navigation.navigationId,
ANOTHER_URL,
Expand All @@ -138,7 +138,7 @@ describe('NavigationTracker', () => {

navigationTracker.navigationCommandFinished(navigation, LOADER_ID);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(navigation.url, ANOTHER_URL);
assert.equal(navigationTracker.url, ANOTHER_URL);
assert.equal(
Expand All @@ -148,11 +148,11 @@ describe('NavigationTracker', () => {

navigationTracker.loadPageEvent(ANOTHER_LOADER_ID);

await assertNoNavigationEvents();
assertNoNavigationEvents();

navigationTracker.loadPageEvent(LOADER_ID);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(
(await navigation.finished).eventName,
NavigationEventName.Load,
Expand All @@ -164,15 +164,15 @@ describe('NavigationTracker', () => {
const navigation = navigationTracker.createPendingNavigation(SOME_URL);
navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
navigation.navigationId,
ANOTHER_URL,
);

navigationTracker.frameRequestedNavigation(YET_ANOTHER_URL);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
navigation.navigationId,
ANOTHER_URL,
Expand All @@ -190,15 +190,15 @@ describe('NavigationTracker', () => {
const navigation = navigationTracker.createPendingNavigation(SOME_URL);
navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
navigation.navigationId,
ANOTHER_URL,
);

navigationTracker.failNavigation(navigation, ERROR_MESSAGE);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationFailed,
navigation.navigationId,
ANOTHER_URL,
Expand All @@ -220,15 +220,15 @@ describe('NavigationTracker', () => {
const navigation = navigationTracker.createPendingNavigation(SOME_URL);
navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
navigation.navigationId,
ANOTHER_URL,
);

navigationTracker.networkLoadingFailed(LOADER_ID, ERROR_MESSAGE);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationFailed,
navigation.navigationId,
ANOTHER_URL,
Expand All @@ -248,10 +248,10 @@ describe('NavigationTracker', () => {
});

describe('Renderer initiated navigation', () => {
it('should process fragment navigation', async () => {
it('should process fragment navigation', () => {
navigationTracker.navigatedWithinDocument(SOME_URL, 'fragment');

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.FragmentNavigated,
sinon.match.any,
SOME_URL,
Expand All @@ -262,10 +262,10 @@ describe('NavigationTracker', () => {
});

describe('cross-document navigation', () => {
it('started', async () => {
it('started', () => {
navigationTracker.frameRequestedNavigation(SOME_URL);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(
navigationTracker.currentNavigationId,
initialNavigationId,
Expand All @@ -274,7 +274,7 @@ describe('NavigationTracker', () => {

navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
sinon.match.any,
ANOTHER_URL,
Expand All @@ -288,18 +288,18 @@ describe('NavigationTracker', () => {

navigationTracker.frameNavigated(YET_ANOTHER_URL, LOADER_ID);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(navigationTracker.url, YET_ANOTHER_URL);

navigationTracker.loadPageEvent(LOADER_ID);

await assertNoNavigationEvents();
assertNoNavigationEvents();
});

it('aborted by script-initiated navigation', async () => {
it('aborted by script-initiated navigation', () => {
navigationTracker.frameRequestedNavigation(SOME_URL);

await assertNoNavigationEvents();
assertNoNavigationEvents();
assert.equal(
navigationTracker.currentNavigationId,
initialNavigationId,
Expand All @@ -308,15 +308,15 @@ describe('NavigationTracker', () => {

navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
sinon.match.any,
ANOTHER_URL,
);

navigationTracker.frameRequestedNavigation(YET_ANOTHER_URL);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
sinon.match.any,
ANOTHER_URL,
Expand All @@ -328,19 +328,19 @@ describe('NavigationTracker', () => {
assert.equal(navigationTracker.url, INITIAL_URL);
});

it('aborted by command navigation', async () => {
it('aborted by command navigation', () => {
navigationTracker.frameRequestedNavigation(SOME_URL);
navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
sinon.match.any,
ANOTHER_URL,
);

navigationTracker.createPendingNavigation(YET_ANOTHER_URL);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
sinon.match.any,
ANOTHER_URL,
Expand All @@ -352,19 +352,19 @@ describe('NavigationTracker', () => {
assert.equal(navigationTracker.url, INITIAL_URL);
});

it('failed network', async () => {
it('failed network', () => {
navigationTracker.frameRequestedNavigation(SOME_URL);
navigationTracker.frameStartedNavigating(ANOTHER_URL, LOADER_ID);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
sinon.match.any,
ANOTHER_URL,
);

navigationTracker.networkLoadingFailed(LOADER_ID, ERROR_MESSAGE);

await assertNavigationEvent(
assertNavigationEvent(
ChromiumBidi.BrowsingContext.EventNames.NavigationFailed,
sinon.match.any,
ANOTHER_URL,
Expand Down
53 changes: 6 additions & 47 deletions tests/browsing_context/__snapshots__/test_navigate_events.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -614,20 +614,6 @@
# ---
# name: test_scriptNavigate_checkEvents
list([
dict({
'method': 'script.message',
'params': dict({
'channel': 'beforeunload_channel',
'data': dict({
'type': 'string',
'value': 'beforeunload',
}),
'source': dict({
'context': 'stable_0',
}),
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.navigationStarted',
'params': dict({
Expand All @@ -637,34 +623,12 @@
}),
'type': 'event',
}),
dict({
'method': 'network.beforeRequestSent',
'params': dict({
'context': 'stable_0',
'isBlocked': False,
'navigation': 'stable_1',
'redirectCount': 0,
'request': 'stable_4',
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.navigationStarted',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_5',
'url': 'stable_6',
}),
'type': 'event',
}),
dict({
'method': 'network.beforeRequestSent',
'params': dict({
'context': 'stable_0',
'isBlocked': False,
'navigation': 'stable_5',
'redirectCount': 0,
'request': 'stable_8',
'navigation': 'stable_3',
'url': 'stable_4',
}),
'type': 'event',
}),
Expand All @@ -677,26 +641,21 @@
}),
'type': 'event',
}),
dict({
'error': 'unknown error',
'id': 'stable_9',
'type': 'error',
}),
dict({
'method': 'browsingContext.domContentLoaded',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_5',
'url': 'stable_6',
'navigation': 'stable_3',
'url': 'stable_4',
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.load',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_5',
'url': 'stable_6',
'navigation': 'stable_3',
'url': 'stable_4',
}),
'type': 'event',
}),
Expand Down
23 changes: 11 additions & 12 deletions tests/browsing_context/test_navigate_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,9 @@ async def test_navigate_dataUrl_checkEvents(websocket, context_id, url_base,


@pytest.mark.asyncio
async def test_scriptNavigate_checkEvents(websocket, context_id, url_base,
url_example, html, read_messages,
snapshot):
await goto_url(websocket, context_id, url_base)
await set_beforeunload_handler(websocket, context_id)
await subscribe(
websocket,
["browsingContext", "script.message", "network.beforeRequestSent"])
async def test_scriptNavigate_checkEvents(websocket, context_id, url_example,
html, read_messages, snapshot):
await subscribe(websocket, ["browsingContext"])

initial_url = html(f"<script>window.location='{url_example}';</script>")

Expand All @@ -213,10 +208,14 @@ async def test_scriptNavigate_checkEvents(websocket, context_id, url_base,
}
})

messages = await read_messages(9,
keys_to_stabilize=KEYS_TO_STABILIZE,
check_no_other_messages=True,
sort=False)
messages = await read_messages(
5,
# Filter out command result, as it can be
# racy with other events.
filter_lambda=lambda x: 'id' not in x,
keys_to_stabilize=KEYS_TO_STABILIZE,
check_no_other_messages=True,
sort=False)
assert messages == snapshot(exclude=SNAPSHOT_EXCLUDE)


Expand Down

0 comments on commit 1451212

Please sign in to comment.