Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Update of window behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
simonradier committed Feb 10, 2021
1 parent 05eda2e commit 020b9e8
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/nodes/get-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function NodeGetTitleConstructor (this : NodeGetTitle, conf : NodeGetTitl
done(e);
} else {
try {
msg.payload = await msg.driver.window().getTitle();
msg.payload = await msg.driver.window().current().getTitle();
} catch (sube) {
msg.payload = "[Unknown]";
}
Expand All @@ -55,7 +55,7 @@ export function NodeGetTitleConstructor (this : NodeGetTitle, conf : NodeGetTitl
}
} else {
try {
msg.payload = await msg.driver.window().getTitle();
msg.payload = await msg.driver.window().current().getTitle();
node.status({ fill : "green", shape : "dot", text : "success"});
if (msg.error) { delete msg.error; }
send([msg, null]);
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/open-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export function NodeOpenWebConstructor (this : NodeOpenWeb, conf : NodeOpenWebDe
if (msg.driver) {
if (!driverError)
if (!conf.maximized)
await msg.driver.window(null).setSize(parseInt(conf.width, 10), parseInt(conf.height, 10));
await msg.driver.window().current().setSize(parseInt(conf.width, 10), parseInt(conf.height, 10));
else if (!conf.headless)
await msg.driver.window(null).maximize();
await msg.driver.window().current().maximize();
send(msg);
this.status({ fill : "green", shape : "dot", text : "success"});
done();
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function NodeScreenshotConstructor (this : NodeScreenshot, conf : NodeScr
const filePath : string = msg.filePath ?? conf.filePath;
setTimeout (async () => {
try {
const sc = await msg.driver.window().screenshot();
const sc = await msg.driver.window().current().screenshot();
if (filePath)
await fs.promises.writeFile(filePath, sc, "base64");
msg.payload = sc;
Expand Down
5 changes: 3 additions & 2 deletions src/webdriver/api/jsonwire.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WDAPIDef, RequestDef } from "../interface";
import { CookieDef } from "../interface/cookie";

export class JSONWire implements WDAPIDef {
SESSION_START(browser: string, headless: boolean): RequestDef {
Expand Down Expand Up @@ -121,7 +122,7 @@ export class JSONWire implements WDAPIDef {
COOKIE_GET(sessionId: string, name: string): RequestDef {
throw new Error("Method not implemented.");
}
COOKIE_ADD(sessionId: string, name: string, value: string, path: string, domain: string): RequestDef {
COOKIE_ADD(sessionId: string, cookie : CookieDef): RequestDef {
throw new Error("Method not implemented.");
}
COOKIE_DELETE(sessionId: string, name: string): RequestDef {
Expand All @@ -139,7 +140,7 @@ export class JSONWire implements WDAPIDef {
ALERT_GETTEXT(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
}
ALERT_SENDTEXT(sessionId: string): RequestDef {
ALERT_SENDTEXT(sessionId: string, text : string): RequestDef {
throw new Error("Method not implemented.");
}
}
63 changes: 52 additions & 11 deletions src/webdriver/api/w3c.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestOptions } from "http";
import { WDAPIDef, RequestDef } from "../interface";
import { CookieDef } from "../interface/cookie";
import { WebDriverRequest, WebElement } from "../webdriver";

export class W3C implements WDAPIDef {
Expand Down Expand Up @@ -381,38 +382,78 @@ export class W3C implements WDAPIDef {
}

COOKIE_GETALL(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/cookie`;
result.requestOptions.method = "GET"
return result;
}

COOKIE_GET(sessionId: string, name: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/cookie/${name}`;
result.requestOptions.method = "GET"
return result;
}

COOKIE_ADD(sessionId: string, name: string, value: string, path: string, domain: string): RequestDef {
throw new Error("Method not implemented.");
COOKIE_ADD(sessionId: string, cookie : CookieDef): RequestDef {
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/cookie`;
result.requestOptions.method = "POST";
result.data = cookie;
return result;
}

COOKIE_DELETE(sessionId: string, name: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/cookie/${name}`;
result.requestOptions.method = "DELETE"
return result;
}

COOKIE_DELETEALL(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/cookie`;
result.requestOptions.method = "DELETE"
return result;
}

ALERT_ACCEPT(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/alert/accept`;
result.requestOptions.method = "POST"
result.data = {};
return result;
}

ALERT_DISMISS(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/alert/dismiss`;
result.requestOptions.method = "POST"
result.data = {};
return result;
}

ALERT_GETTEXT(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/alert/text`;
result.requestOptions.method = "GET"
return result;
}

ALERT_SENDTEXT(sessionId: string): RequestDef {
throw new Error("Method not implemented.");
ALERT_SENDTEXT(sessionId: string, text : string): RequestDef {
let result = new WebDriverRequest();
W3C._initHttpOptions(result);
result.path = `/session/${sessionId}/alert/text`;
result.requestOptions.method = "POST";
result.data = { text }
return result;
}
}
105 changes: 91 additions & 14 deletions src/webdriver/simple-webdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class SimpleWebDriver {

private _api : WDAPIDef;

private _session? : string;
private _session : string = null;

private _w3c : boolean;

Expand All @@ -65,7 +65,10 @@ export class SimpleWebDriver {
return this._timeouts;
}

private _currentHandle : string = null;

private _browserName : Browser;

public get browserName() {
return this._browserName;
}
Expand Down Expand Up @@ -108,15 +111,9 @@ export class SimpleWebDriver {
});
}

/**
* Allow to access windows capabilities, if no handle is provided, it will modify the current context
* @param handle
*/
public window(handle : string = null) {

private _windowActions(handle : string) {
return {
/**
*
*/
getTitle : async () => {
return new Promise<string> (async (resolve, reject) => {
let resp = wdapi.call<string>(this.serverURL, this._api.WINDOW_GETTITLE(this.session, handle)).then( resp => {
Expand Down Expand Up @@ -162,6 +159,16 @@ export class SimpleWebDriver {
});
})
},
switch : async () => {
return new Promise<string> (async (resolve, reject) => {
wdapi.call<string>(this.serverURL, this._api.WINDOW_SWITCH(this.session, handle)).then(resp => {
this._currentHandle = handle;
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
})
},
screenshot : async () => {
return new Promise<string> (async (resolve, reject) => {
wdapi.call<string>(this.serverURL, this._api.WINDOW_SCREENSHOT(this.session)).then(resp => {
Expand All @@ -174,6 +181,40 @@ export class SimpleWebDriver {
}
}

/**
* Allow to access windows capabilities, if no handle is provided, it will modify the current context
* @param handle
*/
public window() {
return {
getHandle : () => {
return new Promise<string> (async (resolve, reject) => {
let resp = wdapi.call<string>(this.serverURL, this._api.WINDOW_GETHANDLE(this.session)).then( resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
getAllHandles : () => {
return new Promise<string[]> (async (resolve, reject) => {
let resp = wdapi.call<string[]>(this.serverURL, this._api.WINDOW_GETHANDLES(this.session)).then( resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
handle : (handle : string) => {
return this._windowActions(handle);

},
current : () => {
return this._windowActions(this._currentHandle);
}
}
}

public navigate() {
return {
refresh : () => {
Expand Down Expand Up @@ -314,19 +355,49 @@ export class SimpleWebDriver {
public cookies() {
return {
getAll : () => {

return new Promise<CookieDef[]> ((resolve, reject) => {
wdapi.call<CookieDef[]>(this.serverURL, this._api.COOKIE_GETALL(this.session)).then(resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
get : (name : string) => {

return new Promise<CookieDef> ((resolve, reject) => {
wdapi.call<CookieDef>(this.serverURL, this._api.COOKIE_GET(this.session, name)).then(resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
add : (cookie : CookieDef) => {

return new Promise<void> ((resolve, reject) => {
wdapi.call<void>(this.serverURL, this._api.COOKIE_ADD(this.session, cookie)).then(resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
delete : (name : string) => {

return new Promise<void> ((resolve, reject) => {
wdapi.call<void>(this.serverURL, this._api.COOKIE_DELETE(this.session, name)).then(resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
},
deleteAll : () => {

return new Promise<void> ((resolve, reject) => {
wdapi.call<void>(this.serverURL, this._api.COOKIE_DELETEALL(this.session)).then(resp => {
resolve(resp.body.value);
}).catch(err => {
reject(err);
});
});
}
}
}
Expand Down Expand Up @@ -437,6 +508,11 @@ export class SimpleWebDriver {
}
this._session = resp.body.value.sessionId;
this._timeouts = resp.body.value.capabilities.timeouts;
try {
this._currentHandle = await this.window().getHandle();
} catch (err) {
reject (err);
}
resolve(true);
}
} catch (err) {
Expand All @@ -452,6 +528,7 @@ export class SimpleWebDriver {
} else {
wdapi.call<any>(this.serverURL, this._api.SESSION_STOP(this.session)).then(resp => {
this._session = null;
this._currentHandle = null;
resolve();
}).catch(err => {
reject(err);
Expand Down
10 changes: 6 additions & 4 deletions test/unit/nodes/open-web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import helper from 'node-red-node-test-helper';
import wd2 from '../../../src/selenium-wd2'
import { NODE_OPEN_WEB } from './data';
import { WD_NAVIGATE_TO_RESPONSE, WD_SERVER_URL_HTTP, WD_SESSION_ID, WD_START_SESSION_RESPONSE } from '../simple-webdriver/data';
import { WD_NAVIGATE_TO_RESPONSE, WD_SERVER_URL_HTTP, WD_SESSION_ID, WD_START_SESSION_RESPONSE, WD_WINDOW_HANDLE_RESPONSE } from '../simple-webdriver/data';
import nock from 'nock';
import { LoggerConfiguration, LogLevel } from '../../../src/webdriver/utils/logger';
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -39,9 +39,11 @@ describe('node : open-web', function (){
this.timeout(30000);
let resp = WD_START_SESSION_RESPONSE.OK;
nock(WD_SERVER_URL_HTTP).post("/session").reply(resp.code, resp.body, resp.headers);
let resp2 = WD_NAVIGATE_TO_RESPONSE.OK;
nock(WD_SERVER_URL_HTTP).post(`/session/${WD_SESSION_ID}/url`).reply(resp.code, resp.body, resp.headers);
nock(WD_SERVER_URL_HTTP).post(`/session/${WD_SESSION_ID}/window/rect`).reply(resp.code, resp.body, resp.headers);
let resp2 = WD_WINDOW_HANDLE_RESPONSE.OK;
nock(WD_SERVER_URL_HTTP).get(`/session/${WD_SESSION_ID}/window`).reply(resp2.code, resp2.body, resp2.headers);
let resp3 = WD_NAVIGATE_TO_RESPONSE.OK;
nock(WD_SERVER_URL_HTTP).post(`/session/${WD_SESSION_ID}/url`).reply(resp3.code, resp3.body, resp3.headers);
nock(WD_SERVER_URL_HTTP).post(`/session/${WD_SESSION_ID}/window/rect`).reply(resp3.code, resp3.body, resp3.headers);
let flow = [
NODE_OPEN_WEB.OK_CHROME("n1", ["n3"]),
//@ts-ignore
Expand Down
13 changes: 13 additions & 0 deletions test/unit/simple-webdriver/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export let WD_SESSION_ID = "session-test-id-1337"

export let WD_ELEMENT_ID = "element-test-id-1337"

export let WD_HANDLE_ID = "window-test-id-1337"

export let WD_CAPABILITIES = {
"acceptInsecureCerts": false,
"browserName": "chrome",
Expand Down Expand Up @@ -177,4 +179,15 @@ export let WD_NAVIGATE_TO_RESPONSE = {
},
headers : { "Content-Type" : "application/json"}
}
}

export let WD_WINDOW_HANDLE_RESPONSE = {
OK : {
code : 200,
body : {
//@ts-ignore
"value" : WD_HANDLE_ID
},
headers : { "Content-Type" : "application/json"}
}
}
Loading

0 comments on commit 020b9e8

Please sign in to comment.