const logger = require("logops") , { Builder, Capabilities, WebDriver, By, until, Session, Actions } = require('selenium-webdriver') , _http = require('selenium-webdriver/http') , { Options } = require('selenium-webdriver/chrome'); class NavigateService { constructor() { this.config = { browser: 'chrome', hub: 'http://spiduler-chrome:4444/wd/hub', capabilities: Capabilities.chrome(), browserArgs: ['--no-sandbox', '--headless', '--disable-dev-shm-usage'] } } rowing(params) { return this[params.anchor](params) } page(params) { return this._getDriver(params).then(driver => driver.findElement(By.css('body')).then(body => body.getAttribute('innerHTML'))) } login(params) { return this._getDriver(params).then(driver => { return driver.get(params.uri).then(() => driver.wait(until.elementLocated(By.css(params.css.password)), 5000)).then(async () => { let username = driver.findElement(By.css(params.css.username)) let password = driver.findElement(By.css(params.css.password)) let submit = driver.findElement(By.css(params.css.submit)) driver.actions().mouseMove(username).click().build().perform() username.sendKeys(params.key.username) password.sendKeys(params.key.password) // logger.info({ currentUrl: await driver.getCurrentUrl(), sessionId: await driver.getSession() }, 'aaaaa') return submit.click() // WebElement // return driver.findElement(By.css('body')) }).then(async body => { // return body.getAttribute('innerHTML') return { currentUrl: await driver.getCurrentUrl(), sessionId: await driver.getSession() } }) }) } _getDriver(params) { return params.sessionId ? this._getDriverExists(params) : this._getNewDriver() } async _getDriverExists(params) { return new WebDriver(new Session(params.sessionId, this.config.capabilities), new _http.Executor(Promise.resolve(this.config.hub) .then(url => new _http.HttpClient(url, null, null)))) } _getNewDriver() { let opts = new Options; opts.addArguments(this.config.browserArgs) return new Builder() .usingServer(this.config.hub) .withCapabilities(this.config.capabilities) .forBrowser(this.config.browser) .setChromeOptions(opts) .build(); } } module.exports = new NavigateService