UNPKG

selenium-webdriver-mcp

Version:
108 lines 5.44 kB
import { until } from 'selenium-webdriver'; import { Select } from 'selenium-webdriver/lib/select.js'; import { LocatorFactory } from '../utils/locators.js'; export class ActionService { driver; constructor(driver) { this.driver = driver; } async hoverOverElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); const actions = this.driver.actions({ bridge: true }); await actions.move({ origin: element }).perform(); } async waitForElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); return this.driver.wait(until.elementLocated(locator), params.timeout || 15000); } async dragAndDrop(sourceParams, targetParams) { const sourceLocator = LocatorFactory.createLocator(sourceParams.by, sourceParams.value); const targetLocator = LocatorFactory.createLocator(targetParams.by, targetParams.value); const sourceElement = await this.driver.wait(until.elementLocated(sourceLocator), sourceParams.timeout || 15000); const targetElement = await this.driver.wait(until.elementLocated(targetLocator), targetParams.timeout || 15000); const actions = this.driver.actions({ bridge: true }); await actions.dragAndDrop(sourceElement, targetElement).perform(); } async doubleClickElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); const actions = this.driver.actions({ bridge: true }); await actions.doubleClick(element).perform(); } async rightClickElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); const actions = this.driver.actions({ bridge: true }); await actions.contextClick(element).perform(); } async selectDropdownByText(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const selectElement = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); const select = new Select(selectElement); await select.selectByVisibleText(params.text); } async selectDropdownByValue(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const selectElement = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); const select = new Select(selectElement); await select.selectByValue(params.value); } async pressKey(key) { const actions = this.driver.actions({ bridge: true }); await actions.keyDown(key).keyUp(key).perform(); } async executeScript(script, args = []) { return this.driver.executeScript(script, ...args); } async scrollToElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); await this.driver.executeScript('arguments[0].scrollIntoView();', element); } async scrollToTop() { await this.driver.executeScript('window.scrollTo(0, 0);'); } async scrollToBottom() { await this.driver.executeScript('window.scrollTo(0, document.body.scrollHeight);'); } async scrollToCoordinates(x, y) { await this.driver.executeScript(`window.scrollTo(${x}, ${y});`); } async scrollByPixels(x, y) { await this.driver.executeScript(`window.scrollBy(${x}, ${y});`); } async submitForm(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const form = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); await form.submit(); } async focusElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); await this.driver.executeScript('arguments[0].focus();', element); } async blurElement(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); await this.driver.executeScript('arguments[0].blur();', element); } async selectCheckbox(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const checkbox = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); if (!(await checkbox.isSelected())) { await checkbox.click(); } } async unselectCheckbox(params) { const locator = LocatorFactory.createLocator(params.by, params.value); const checkbox = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000); if (await checkbox.isSelected()) { await checkbox.click(); } } async takeScreenshot() { return this.driver.takeScreenshot(); } } //# sourceMappingURL=actionService.js.map