Skip to content

Commit

Permalink
fix(basic.gblib): Fixes in WebAutomation.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Mar 7, 2023
1 parent cfe3ab3 commit d1b9da2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
18 changes: 9 additions & 9 deletions packages/basic.gblib/services/KeywordsExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class KeywordsExpressions {
}
const params = this.getParams($1, ['url', 'username', 'password']);

return `page = await wa.getPage({pid: pid, page: page, sessionKind: ${kind}, sessionName: ${sessionName}, ${params}})`;
return `page = await wa.openPage({pid: pid, handle: page, sessionKind: ${kind}, sessionName: ${sessionName}, ${params}})`;
}
];

Expand Down Expand Up @@ -292,7 +292,7 @@ export class KeywordsExpressions {
keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gim,
($0, $1, $2, $3) => {
return `${$1} = await sys.find({pid: pid, args:[${$2}])\n
return `${$1} = await sys.find({pid: pid, handle: page, args:[${$2}])\n
if (!${$1}) {s
await dk.talk ({pid: pid, ${$3}})\n;
return -1;
Expand All @@ -312,7 +312,7 @@ export class KeywordsExpressions {
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)/gim,
($0, $1, $2, $3) => {
return `
${$1} = await sys.find({pid: pid, args: [${$2}]})`;
${$1} = await sys.find({pid: pid, handle: page, args: [${$2}]})`;
}
];

Expand Down Expand Up @@ -643,16 +643,16 @@ export class KeywordsExpressions {
keywords[i++] = [
/^\s*(hover)(\s*)(.*)/gim,
($0, $1, $2, $3) => {
const params = this.getParams($3, ['handle', 'selector']);
return `await wa.hover ({pid: pid, ${params}})`;
const params = this.getParams($3, ['selector']);
return `await wa.hover ({pid: pid, handle: page, ${params}})`;
}
];

keywords[i++] = [
/^\s*(click link text)(\s*)(.*)/gim,
($0, $1, $2, $3) => {
const params = this.getParams('page,' + $3, ['handle', 'text', 'index']);
return `await wa.linkByText ({pid: pid, ${params}})`;
const params = this.getParams('page,' + $3, ['text', 'index']);
return `await wa.linkByText ({pid: pid, handle: page, ${params}})`;
}
];

Expand All @@ -661,8 +661,8 @@ export class KeywordsExpressions {
($0, $1, $2, $3) => {
// page is not string.
// https://github.com/GeneralBots/BotServer/issues/310
const params = this.getParams($3, ['handle', 'frameOrSelector', 'selector']);
return `await wa.click ({pid: pid, ${params}})`;
const params = this.getParams($3, ['frameOrSelector', 'selector']);
return `await wa.click ({pid: pid, handle:page, ${params}})`;
}
];

Expand Down
12 changes: 8 additions & 4 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ export class SystemKeywords {
* @see NPM package data-forge
*
*/
public async find({ pid, args }): Promise<any> {
public async find({ pid, handle, args }): Promise<any> {
const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const file = args[0];
args.shift();
Expand All @@ -713,10 +713,14 @@ export class SystemKeywords {

let results;
let header, rows;
let page;
if (handle){
page = WebAutomationServices.getPageByHandle(handle);
}

if (file['$eval']) {
const container = file['frame'] ? file['frame'] : file['_page'];
const originalSelector = file['originalSelector'];
if (page['$eval'] && (file.startsWith('.') || file.startsWith('#'))) {
const container = page['frame'] ? page['frame'] : page;
const originalSelector = file;

// Transforms table

Expand Down
22 changes: 11 additions & 11 deletions packages/basic.gblib/services/WebAutomationServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class WebAutomationServices {
* @example OPEN "https://wikipedia.org"
*/

public async getPage({ pid, handle, sessionKind, sessionName, url, username, password }) {
public async openPage({ pid, handle, sessionKind, sessionName, url, username, password }) {
GBLog.info(`BASIC: Web Automation OPEN ${sessionName ? sessionName : ''} ${url}.`);
const { min, user } = await DialogKeywords.getProcessInfo(pid);

Expand Down Expand Up @@ -190,7 +190,7 @@ export class WebAutomationServices {
return handle;
}

public getPageByHandle(handle) {
public static getPageByHandle(handle) {
return GBServer.globals.webSessions[handle].page;
}

Expand All @@ -200,7 +200,7 @@ export class WebAutomationServices {
* @example GET "selector"
*/
public async getBySelector({ handle, selector }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation GET element: ${selector}.`);
await page.waitForSelector(selector);
let elements = await page.$$(selector);
Expand All @@ -223,7 +223,7 @@ export class WebAutomationServices {
* @example GET page,"frameSelector,"elementSelector"
*/
public async getByFrame({ handle, frame, selector }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation GET element by frame: ${selector}.`);
await page.waitForSelector(frame);
let frameHandle = await page.$(frame);
Expand All @@ -243,7 +243,7 @@ export class WebAutomationServices {
* Simulates a mouse hover an web page element.
*/
public async hover({ pid, handle, selector }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation HOVER element: ${selector}.`);
await this.getBySelector({ handle, selector: selector });
await page.hover(selector);
Expand All @@ -256,7 +256,7 @@ export class WebAutomationServices {
* @example CLICK page,"#idElement"
*/
public async click({ pid, handle, frameOrSelector, selector }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation CLICK element: ${frameOrSelector}.`);
if (selector) {
await page.waitForSelector(frameOrSelector);
Expand Down Expand Up @@ -294,7 +294,7 @@ export class WebAutomationServices {
* @example PRESS ENTER ON page
*/
public async pressKey({ handle, char, frame }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation PRESS ${char} ON element: ${frame}.`);
if (char.toLowerCase() === 'enter') {
char = '\n';
Expand All @@ -310,7 +310,7 @@ export class WebAutomationServices {
}

public async linkByText({ pid, handle, text, index }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation CLICK LINK TEXT: ${text} ${index}.`);
if (!index) {
index = 1;
Expand All @@ -327,7 +327,7 @@ export class WebAutomationServices {
*/
public async screenshot({ pid, handle, selector }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation SCREENSHOT ${selector}.`);

const gbaiName = `${min.botId}.gbai`;
Expand All @@ -347,7 +347,7 @@ export class WebAutomationServices {
* @example SET page,"selector","text"
*/
public async setElementText({ pid, handle, selector, text }) {
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation TYPE on ${selector}: ${text}.`);
const e = await this.getBySelector({ handle, selector });
await e.click({ clickCount: 3 });
Expand All @@ -363,7 +363,7 @@ export class WebAutomationServices {
*/
public async download({ pid, handle, selector, folder }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const page = this.getPageByHandle(handle);
const page = WebAutomationServices.getPageByHandle(handle);

const element = await this.getBySelector({ handle, selector });
// https://github.com/GeneralBots/BotServer/issues/311
Expand Down

0 comments on commit d1b9da2

Please sign in to comment.