@measey/mycoder-agent
Version:
Agent module for mycoder - an AI-powered software development assistant
72 lines • 2.93 kB
JavaScript
import { errorToString } from '../../../utils/errorToString.js';
import { SelectorType, BrowserError, BrowserErrorCode, } from './types.js';
export class PageController {
page;
constructor(page) {
this.page = page;
}
getSelector(selector, type = SelectorType.CSS) {
switch (type) {
case SelectorType.XPATH:
return `xpath=${selector}`;
case SelectorType.TEXT:
return `text=${selector}`;
case SelectorType.ROLE:
return `role=${selector}`;
case SelectorType.TESTID:
return `[data-testid="${selector}"]`;
default:
return selector;
}
}
validateSelector(selector, _type) {
if (!selector) {
throw new BrowserError('Invalid selector: empty string', BrowserErrorCode.SELECTOR_INVALID);
}
// TODO: Add more validation
}
async waitForSelector(selector, options = {}) {
this.validateSelector(selector, options.type || SelectorType.CSS);
try {
const locator = this.page.locator(this.getSelector(selector, options.type));
await locator.waitFor({
state: options.visible ? 'visible' : 'attached',
timeout: options.timeout,
});
}
catch (error) {
throw new BrowserError(`Failed to find element: ${errorToString(error)}`, BrowserErrorCode.ELEMENT_NOT_FOUND, error);
}
}
async click(selector, options = {}) {
this.validateSelector(selector, options.type || SelectorType.CSS);
try {
const locator = this.page.locator(this.getSelector(selector, options.type));
await locator.click({ timeout: options.timeout });
}
catch (error) {
throw new BrowserError(`Failed to click element: ${errorToString(error)}`, BrowserErrorCode.SELECTOR_ERROR, error);
}
}
async type(selector, text, options = {}) {
this.validateSelector(selector, options.type || SelectorType.CSS);
try {
const locator = this.page.locator(this.getSelector(selector, options.type));
await locator.fill(text, { timeout: options.timeout });
}
catch (error) {
throw new BrowserError(`Failed to type text: ${errorToString(error)}`, BrowserErrorCode.SELECTOR_ERROR, error);
}
}
async getText(selector, options = {}) {
this.validateSelector(selector, options.type || SelectorType.CSS);
try {
const locator = this.page.locator(this.getSelector(selector, options.type));
return (await locator.textContent()) || '';
}
catch (error) {
throw new BrowserError(`Failed to get text: ${errorToString(error)}`, BrowserErrorCode.SELECTOR_ERROR, error);
}
}
}
//# sourceMappingURL=PageController.js.map