From d1b9da21bae4a7c6c1d9fae69e1063d705ae0b38 Mon Sep 17 00:00:00 2001 From: rodrigorodriguez Date: Tue, 7 Mar 2023 12:06:15 -0300 Subject: [PATCH] fix(basic.gblib): Fixes in WebAutomation. --- .../services/KeywordsExpressions.ts | 18 +++++++-------- .../basic.gblib/services/SystemKeywords.ts | 12 ++++++---- .../services/WebAutomationServices.ts | 22 +++++++++---------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/basic.gblib/services/KeywordsExpressions.ts b/packages/basic.gblib/services/KeywordsExpressions.ts index 672e750c..008ec5d3 100644 --- a/packages/basic.gblib/services/KeywordsExpressions.ts +++ b/packages/basic.gblib/services/KeywordsExpressions.ts @@ -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}})`; } ]; @@ -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; @@ -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}]})`; } ]; @@ -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}})`; } ]; @@ -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}})`; } ]; diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index 820248fb..1fb57d6c 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -689,7 +689,7 @@ export class SystemKeywords { * @see NPM package data-forge * */ - public async find({ pid, args }): Promise { + public async find({ pid, handle, args }): Promise { const { min, user, params } = await DialogKeywords.getProcessInfo(pid); const file = args[0]; args.shift(); @@ -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 diff --git a/packages/basic.gblib/services/WebAutomationServices.ts b/packages/basic.gblib/services/WebAutomationServices.ts index e56e639b..829b3365 100644 --- a/packages/basic.gblib/services/WebAutomationServices.ts +++ b/packages/basic.gblib/services/WebAutomationServices.ts @@ -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); @@ -190,7 +190,7 @@ export class WebAutomationServices { return handle; } - public getPageByHandle(handle) { + public static getPageByHandle(handle) { return GBServer.globals.webSessions[handle].page; } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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'; @@ -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; @@ -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`; @@ -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 }); @@ -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