Skip to content

Commit

Permalink
browsingContext.navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Oct 7, 2021
1 parent 6e5fc4b commit 89609d2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
24 changes: 22 additions & 2 deletions src/bidiMapper/bidiProtocolTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export namespace Script {
export type ScriptEvaluateCommand = {
method: 'script.evaluate';
params: ScriptEvaluateParameters;
}
};

export type ScriptExceptionResult = {
exceptionDetails: CommonDataTypes.ExceptionDetails;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/bidiMapper/commandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions src/bidiMapper/domains/context/browsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,22 @@ export class BrowsingContextProcessor {
commandData: BrowsingContext.BrowsingContextCreateCommand
): Promise<BrowsingContext.BrowsingContextCreateResult> {
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
);

const context = await this._getOrCreateContext(
targetId,
params.sessionId
attachToTargetEventParams.sessionId
);
resolve(context.toBidi());
}
Expand All @@ -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<BrowsingContext.BrowsingContextNavigateResult> {
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<Script.ScriptEvaluateResult> {
Expand Down
23 changes: 22 additions & 1 deletion src/bidiMapper/domains/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -80,6 +84,23 @@ export class Context {
};
}

public async navigate(
url: string,
wait: BrowsingContext.ReadinessState = 'none'
): Promise<BrowsingContext.BrowsingContextNavigateResult> {
// 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`.
Expand Down
33 changes: 21 additions & 12 deletions tests/test_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,<h2>test</h2>",
"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,<h2>test</h2>"}},
["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.
Expand Down

0 comments on commit 89609d2

Please sign in to comment.