This commit is contained in:
brow3c
2021-05-15 23:53:56 +09:00
parent 9598d9b6fe
commit 586b0f15c6
4 changed files with 77 additions and 6 deletions

View File

@@ -0,0 +1,60 @@
const logger = require("logops")
, { Builder, Capabilities, WebDriver, By } = 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) {
this._getDriver(params.sessionId).then(driver => {
driver.get(params.uri).then(() => {
let username = driver.findElement(By.name(param.name.username))
let password = driver.findElement(By.name(param.name.password))
// let username = driver.findElement(By.xpath(param.xpath.username))
// let password = driver.findElement(By.xpath(param.xpath.password))
let submit = driver.findElement(By.xpath(param.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