fix(basic.gblib): Fixes in WebAutomation.

This commit is contained in:
rodrigorodriguez 2023-03-07 12:06:15 -03:00
parent cfe3ab30f4
commit d1b9da21ba
3 changed files with 28 additions and 24 deletions

View file

@ -159,7 +159,7 @@ export class KeywordsExpressions {
} }
const params = this.getParams($1, ['url', 'username', 'password']); 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++] = [ keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gim, /^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gim,
($0, $1, $2, $3) => { ($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 if (!${$1}) {s
await dk.talk ({pid: pid, ${$3}})\n; await dk.talk ({pid: pid, ${$3}})\n;
return -1; return -1;
@ -312,7 +312,7 @@ export class KeywordsExpressions {
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)/gim, /^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)/gim,
($0, $1, $2, $3) => { ($0, $1, $2, $3) => {
return ` 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++] = [ keywords[i++] = [
/^\s*(hover)(\s*)(.*)/gim, /^\s*(hover)(\s*)(.*)/gim,
($0, $1, $2, $3) => { ($0, $1, $2, $3) => {
const params = this.getParams($3, ['handle', 'selector']); const params = this.getParams($3, ['selector']);
return `await wa.hover ({pid: pid, ${params}})`; return `await wa.hover ({pid: pid, handle: page, ${params}})`;
} }
]; ];
keywords[i++] = [ keywords[i++] = [
/^\s*(click link text)(\s*)(.*)/gim, /^\s*(click link text)(\s*)(.*)/gim,
($0, $1, $2, $3) => { ($0, $1, $2, $3) => {
const params = this.getParams('page,' + $3, ['handle', 'text', 'index']); const params = this.getParams('page,' + $3, ['text', 'index']);
return `await wa.linkByText ({pid: pid, ${params}})`; return `await wa.linkByText ({pid: pid, handle: page, ${params}})`;
} }
]; ];
@ -661,8 +661,8 @@ export class KeywordsExpressions {
($0, $1, $2, $3) => { ($0, $1, $2, $3) => {
// page is not string. // page is not string.
// https://github.com/GeneralBots/BotServer/issues/310 // https://github.com/GeneralBots/BotServer/issues/310
const params = this.getParams($3, ['handle', 'frameOrSelector', 'selector']); const params = this.getParams($3, ['frameOrSelector', 'selector']);
return `await wa.click ({pid: pid, ${params}})`; return `await wa.click ({pid: pid, handle:page, ${params}})`;
} }
]; ];

View file

@ -689,7 +689,7 @@ export class SystemKeywords {
* @see NPM package data-forge * @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 { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const file = args[0]; const file = args[0];
args.shift(); args.shift();
@ -713,10 +713,14 @@ export class SystemKeywords {
let results; let results;
let header, rows; let header, rows;
let page;
if (handle){
page = WebAutomationServices.getPageByHandle(handle);
}
if (file['$eval']) { if (page['$eval'] && (file.startsWith('.') || file.startsWith('#'))) {
const container = file['frame'] ? file['frame'] : file['_page']; const container = page['frame'] ? page['frame'] : page;
const originalSelector = file['originalSelector']; const originalSelector = file;
// Transforms table // Transforms table

View file

@ -100,7 +100,7 @@ export class WebAutomationServices {
* @example OPEN "https://wikipedia.org" * @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}.`); GBLog.info(`BASIC: Web Automation OPEN ${sessionName ? sessionName : ''} ${url}.`);
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
@ -190,7 +190,7 @@ export class WebAutomationServices {
return handle; return handle;
} }
public getPageByHandle(handle) { public static getPageByHandle(handle) {
return GBServer.globals.webSessions[handle].page; return GBServer.globals.webSessions[handle].page;
} }
@ -200,7 +200,7 @@ export class WebAutomationServices {
* @example GET "selector" * @example GET "selector"
*/ */
public async getBySelector({ handle, selector }) { public async getBySelector({ handle, selector }) {
const page = this.getPageByHandle(handle); const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation GET element: ${selector}.`); GBLog.info(`BASIC: Web Automation GET element: ${selector}.`);
await page.waitForSelector(selector); await page.waitForSelector(selector);
let elements = await page.$$(selector); let elements = await page.$$(selector);
@ -223,7 +223,7 @@ export class WebAutomationServices {
* @example GET page,"frameSelector,"elementSelector" * @example GET page,"frameSelector,"elementSelector"
*/ */
public async getByFrame({ handle, frame, selector }) { 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}.`); GBLog.info(`BASIC: Web Automation GET element by frame: ${selector}.`);
await page.waitForSelector(frame); await page.waitForSelector(frame);
let frameHandle = await page.$(frame); let frameHandle = await page.$(frame);
@ -243,7 +243,7 @@ export class WebAutomationServices {
* Simulates a mouse hover an web page element. * Simulates a mouse hover an web page element.
*/ */
public async hover({ pid, handle, selector }) { public async hover({ pid, handle, selector }) {
const page = this.getPageByHandle(handle); const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation HOVER element: ${selector}.`); GBLog.info(`BASIC: Web Automation HOVER element: ${selector}.`);
await this.getBySelector({ handle, selector: selector }); await this.getBySelector({ handle, selector: selector });
await page.hover(selector); await page.hover(selector);
@ -256,7 +256,7 @@ export class WebAutomationServices {
* @example CLICK page,"#idElement" * @example CLICK page,"#idElement"
*/ */
public async click({ pid, handle, frameOrSelector, selector }) { 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}.`); GBLog.info(`BASIC: Web Automation CLICK element: ${frameOrSelector}.`);
if (selector) { if (selector) {
await page.waitForSelector(frameOrSelector); await page.waitForSelector(frameOrSelector);
@ -294,7 +294,7 @@ export class WebAutomationServices {
* @example PRESS ENTER ON page * @example PRESS ENTER ON page
*/ */
public async pressKey({ handle, char, frame }) { 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}.`); GBLog.info(`BASIC: Web Automation PRESS ${char} ON element: ${frame}.`);
if (char.toLowerCase() === 'enter') { if (char.toLowerCase() === 'enter') {
char = '\n'; char = '\n';
@ -310,7 +310,7 @@ export class WebAutomationServices {
} }
public async linkByText({ pid, handle, text, index }) { 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}.`); GBLog.info(`BASIC: Web Automation CLICK LINK TEXT: ${text} ${index}.`);
if (!index) { if (!index) {
index = 1; index = 1;
@ -327,7 +327,7 @@ export class WebAutomationServices {
*/ */
public async screenshot({ pid, handle, selector }) { public async screenshot({ pid, handle, selector }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
const page = this.getPageByHandle(handle); const page = WebAutomationServices.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation SCREENSHOT ${selector}.`); GBLog.info(`BASIC: Web Automation SCREENSHOT ${selector}.`);
const gbaiName = `${min.botId}.gbai`; const gbaiName = `${min.botId}.gbai`;
@ -347,7 +347,7 @@ export class WebAutomationServices {
* @example SET page,"selector","text" * @example SET page,"selector","text"
*/ */
public async setElementText({ pid, handle, 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}.`); GBLog.info(`BASIC: Web Automation TYPE on ${selector}: ${text}.`);
const e = await this.getBySelector({ handle, selector }); const e = await this.getBySelector({ handle, selector });
await e.click({ clickCount: 3 }); await e.click({ clickCount: 3 });
@ -363,7 +363,7 @@ export class WebAutomationServices {
*/ */
public async download({ pid, handle, selector, folder }) { public async download({ pid, handle, selector, folder }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
const page = this.getPageByHandle(handle); const page = WebAutomationServices.getPageByHandle(handle);
const element = await this.getBySelector({ handle, selector }); const element = await this.getBySelector({ handle, selector });
// https://github.com/GeneralBots/BotServer/issues/311 // https://github.com/GeneralBots/BotServer/issues/311