61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
const logger = require("logops")
|
|
, { Builder, Capabilities, WebDriver, By, until } = 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',
|
|
browserArgs: ['--no-sandbox', '--headless', '--disable-dev-shm-usage']
|
|
}
|
|
}
|
|
|
|
rowing(params) {
|
|
return this[params.anchor](params)
|
|
}
|
|
|
|
login(params) {
|
|
return this._getDriver(params.sessionId).then(driver => {
|
|
return driver.get(params.uri).then(() => driver.wait(until.elementLocated(By.xpath(params.xpath.username)), 5000)).then(() => {
|
|
// let username = driver.findElement(By.name(params.name.username))
|
|
// let password = driver.findElement(By.name(params.name.password))
|
|
let username = driver.findElement(By.xpath(params.xpath.username))
|
|
let password = driver.findElement(By.xpath(params.xpath.password))
|
|
let submit = driver.findElement(By.xpath(params.xpath.submit))
|
|
username.sendKeys(params.key.username)
|
|
password.sendKeys(params.key.password)
|
|
return submit.click()
|
|
//*[@id="container_sub"]/div[2]/table/tbody/tr/td[1]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[3]/input
|
|
}).then(signed => {
|
|
return { currentUrl: driver.getCurrentUrl(), sessionId: driver.getSession() }
|
|
})
|
|
})
|
|
}
|
|
|
|
_getDriver(params) {
|
|
return params.sessionId ? this._getDriverExists(params) : this._getNewDriver()
|
|
}
|
|
|
|
_getDriverBySessionId(params) {
|
|
return new WebDriver(params.sessionId, new _http.Executor(Promise.resolve(this.config.hub)
|
|
.then(url => new _http.HttpClient(url, null, null))))
|
|
}
|
|
|
|
_getNewDriver() {
|
|
let capabilities = Capabilities.chrome()
|
|
let opts = new Options;
|
|
opts.addArguments(this.config.browserArgs)
|
|
return new Builder()
|
|
.usingServer(this.config.hub)
|
|
.withCapabilities(capabilities)
|
|
.forBrowser(this.config.browser)
|
|
.setChromeOptions(opts)
|
|
.build();
|
|
}
|
|
}
|
|
|
|
module.exports = new NavigateService
|