From 89609d26a11adecf1cb8916746b60594cb716c8a Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Thu, 7 Oct 2021 14:40:23 +0000 Subject: [PATCH] `browsingContext.navigate` --- src/bidiMapper/bidiProtocolTypes.ts | 24 ++++++++++++-- src/bidiMapper/commandProcessor.ts | 4 +++ .../context/browsingContextProcessor.ts | 17 ++++++++-- src/bidiMapper/domains/context/context.ts | 23 ++++++++++++- tests/test_bidi.py | 33 ++++++++++++------- 5 files changed, 83 insertions(+), 18 deletions(-) diff --git a/src/bidiMapper/bidiProtocolTypes.ts b/src/bidiMapper/bidiProtocolTypes.ts index bb48501f6e..38e47997ff 100644 --- a/src/bidiMapper/bidiProtocolTypes.ts +++ b/src/bidiMapper/bidiProtocolTypes.ts @@ -38,7 +38,7 @@ export namespace Script { export type ScriptEvaluateCommand = { method: 'script.evaluate'; params: ScriptEvaluateParameters; - } + }; export type ScriptExceptionResult = { exceptionDetails: CommonDataTypes.ExceptionDetails; @@ -93,6 +93,7 @@ export namespace Script { // https://w3c.github.io/webdriver-bidi/#module-browsingContext export namespace BrowsingContext { export type BrowsingContext = string; + export type Navigation = string; export type BrowsingContextGetTreeCommand = { method: 'browsingContext.getTree'; @@ -126,11 +127,30 @@ export namespace BrowsingContext { export type BrowsingContextCreateType = 'tab' | 'window'; export type BrowsingContextCreateParameters = { - type: BrowsingContextCreateType; + type?: BrowsingContextCreateType; }; + export type BrowsingContextCreateResult = { context: BrowsingContext; }; + + export type BrowsingContextNavigateCommand = { + method: 'browsingContext.navigate'; + params: BrowsingContextNavigateParameters; + }; + + export type BrowsingContextNavigateParameters = { + context: BrowsingContext; + url: string; + wait?: ReadinessState; + }; + + export type ReadinessState = 'none'; + // TODO sadym: implement 'interactive' and 'complete' states. + export type BrowsingContextNavigateResult = { + navigation?: Navigation; + url: string; + }; } export namespace Session { diff --git a/src/bidiMapper/commandProcessor.ts b/src/bidiMapper/commandProcessor.ts index c5525caf44..eb7f920865 100644 --- a/src/bidiMapper/commandProcessor.ts +++ b/src/bidiMapper/commandProcessor.ts @@ -189,6 +189,10 @@ export class CommandProcessor { return await this._contextProcessor.process_createContext( commandData as BrowsingContext.BrowsingContextCreateCommand ); + case 'browsingContext.navigate': + return await this._contextProcessor.process_navigate( + commandData as BrowsingContext.BrowsingContextNavigateCommand + ); case 'DEBUG.Page.close': return await this._process_DEBUG_Page_close(commandData.params as any); diff --git a/src/bidiMapper/domains/context/browsingContextProcessor.ts b/src/bidiMapper/domains/context/browsingContextProcessor.ts index a0cfcb7d91..cc8681e646 100644 --- a/src/bidiMapper/domains/context/browsingContextProcessor.ts +++ b/src/bidiMapper/domains/context/browsingContextProcessor.ts @@ -126,13 +126,14 @@ export class BrowsingContextProcessor { commandData: BrowsingContext.BrowsingContextCreateCommand ): Promise { const params = commandData.params; + return new Promise(async (resolve) => { let targetId: string; const onAttachedToTarget = async ( - params: Protocol.Target.AttachedToTargetEvent + attachToTargetEventParams: Protocol.Target.AttachedToTargetEvent ) => { - if (params.targetInfo.targetId === targetId) { + if (attachToTargetEventParams.targetInfo.targetId === targetId) { browserCdpClient.Target.removeListener( 'attachedToTarget', onAttachedToTarget @@ -140,7 +141,7 @@ export class BrowsingContextProcessor { const context = await this._getOrCreateContext( targetId, - params.sessionId + attachToTargetEventParams.sessionId ); resolve(context.toBidi()); } @@ -151,11 +152,21 @@ export class BrowsingContextProcessor { const result = await browserCdpClient.Target.createTarget({ url: 'about:blank', + newWindow: params.type === 'window', }); targetId = result.targetId; }); } + async process_navigate( + commandData: BrowsingContext.BrowsingContextNavigateCommand + ): Promise { + const params = commandData.params; + const context = this._getKnownContext(params.context); + + return await context.navigate(params.url, params.wait); + } + async process_script_evaluate( commandData: Script.ScriptEvaluateCommand ): Promise { diff --git a/src/bidiMapper/domains/context/context.ts b/src/bidiMapper/domains/context/context.ts index bb2096aa3b..45f26995cf 100644 --- a/src/bidiMapper/domains/context/context.ts +++ b/src/bidiMapper/domains/context/context.ts @@ -17,7 +17,11 @@ import { Protocol } from 'devtools-protocol'; import { CdpClient } from '../../../cdp'; -import { Script, CommonDataTypes } from '../../bidiProtocolTypes'; +import { + BrowsingContext, + CommonDataTypes, + Script, +} from '../../bidiProtocolTypes'; import EVALUATOR_SCRIPT from '../../scripts/eval.es'; @@ -80,6 +84,23 @@ export class Context { }; } + public async navigate( + url: string, + wait: BrowsingContext.ReadinessState = 'none' + ): Promise { + // TODO sadym: implement. + if (wait !== 'none') { + throw new Error(`Not implenented wait '${wait}'`); + } + + const cdpNavigateResult = await this._cdpClient.Page.navigate({ url }); + + return { + navigation: cdpNavigateResult.loaderId, + url: url, + }; + } + /** * Serializes a given CDP object into BiDi, keeping references in the * target's `globalThis`. diff --git a/tests/test_bidi.py b/tests/test_bidi.py index 7f2a388075..29c822d7ad 100644 --- a/tests/test_bidi.py +++ b/tests/test_bidi.py @@ -220,30 +220,39 @@ async def _ignore_test_PageClose_browsingContextContextDestroyedEmitted(websocke "url": "about:blank"}} @pytest.mark.asyncio -# Not implemented yet. -async def _ignore_test_navigate_eventPageLoadEmittedAndNavigated(websocket): +async def test_navigateWaitNone_navigated(websocket): contextID = await get_open_context_id(websocket) # Send command. command = { "id": 15, - "method": "PROTO.browsingContext.navigate", + "method": "browsingContext.navigate", "params": { "url": "data:text/html,

test

", - "waitUntil": ["load", "domcontentloaded", "networkidle0", "networkidle2"], + "wait": "none", "context": contextID}} await send_JSON_command(websocket, command) - # Assert "DEBUG.Page.load" event emitted. - resp = await read_JSON_message(websocket) - assert resp == { - "method": "DEBUG.Page.load", - "params": { - "context": contextID}} - # Assert command done. resp = await read_JSON_message(websocket) - assert resp == {"id": 15, "result": {}} + recursiveCompare( + resp, + { + "id": 15, + "result": { + "navigation": "F595626837D41F9A72482D53B0C22F25", + "url": "data:text/html,

test

"}}, + ["navigation"]) + +@pytest.mark.asyncio +# Not implemented yet. +async def _ignore_test_navigateWaitInteractive_navigated(websocket): + ignore = True + +@pytest.mark.asyncio +# Not implemented yet. +async def _ignore_test_navigateWaitComplete_navigated(websocket): + ignore = True @pytest.mark.asyncio # Not implemented yet.