2019-07-02 16:12:57 +03:00
|
|
|
const webdriver = require('selenium-webdriver')
|
2019-05-09 13:03:18 +03:00
|
|
|
|
2019-07-02 16:12:57 +03:00
|
|
|
const Twait = 20000
|
2019-05-09 13:03:18 +03:00
|
|
|
|
2019-07-02 16:12:57 +03:00
|
|
|
class Page {
|
2019-05-09 13:03:18 +03:00
|
|
|
constructor(driver) {
|
2019-07-02 16:12:57 +03:00
|
|
|
this.driver = driver
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async waitUntilDisplayed(element, Twaiting) {
|
2019-07-02 16:12:57 +03:00
|
|
|
let counter = Twaiting
|
|
|
|
if (counter === undefined) counter = 180
|
2019-05-09 13:03:18 +03:00
|
|
|
try {
|
|
|
|
do {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.sleep(300)
|
|
|
|
if (await this.isElementDisplayed(element)) return true
|
|
|
|
} while (counter-- > 0)
|
|
|
|
return false
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitUntilDisappear(element, Twaiting) {
|
2019-07-02 16:12:57 +03:00
|
|
|
let counter = Twaiting
|
|
|
|
if (counter === undefined) counter = 180
|
2019-05-09 13:03:18 +03:00
|
|
|
try {
|
|
|
|
do {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.sleep(300)
|
|
|
|
if (!(await this.isElementDisplayed(element))) return true
|
|
|
|
} while (counter-- > 0)
|
|
|
|
return false
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitUntilLocated(element, Twaiting) {
|
2019-07-02 16:12:57 +03:00
|
|
|
let counter = Twaiting
|
|
|
|
if (counter === undefined) counter = 180
|
2019-05-09 13:03:18 +03:00
|
|
|
try {
|
|
|
|
do {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.sleep(300)
|
|
|
|
if (await this.isElementLocated(element)) return true
|
|
|
|
} while (counter-- > 0)
|
|
|
|
return false
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async isElementDisplayed(element) {
|
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
return await this.driver.findElement(element).isDisplayed()
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async isElementLocated(element) {
|
2019-07-02 16:12:57 +03:00
|
|
|
return (await this.driver.findElements(element)).length > 0
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async clickWithWait(element) {
|
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
let field
|
|
|
|
if (element.constructor.name !== 'WebElement') {
|
|
|
|
field = await this.driver.wait(webdriver.until.elementLocated(element), Twait)
|
|
|
|
} else field = element
|
|
|
|
await field.click()
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fillWithWait(element, text) {
|
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
let field
|
|
|
|
if (element.constructor.name !== 'WebElement') {
|
|
|
|
field = await this.driver.wait(webdriver.until.elementLocated(element), Twait)
|
|
|
|
} else field = element
|
|
|
|
await field.sendKeys(text)
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async findWithWait(element) {
|
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.wait(webdriver.until.elementLocated(element), Twait)
|
|
|
|
return await this.driver.findElements(element)
|
|
|
|
} catch (err) {
|
|
|
|
return null
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async switchToNextPage() {
|
2019-07-02 16:12:57 +03:00
|
|
|
let allHandles = []
|
|
|
|
let curHandle
|
2019-05-09 13:03:18 +03:00
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
allHandles = await this.driver.getAllWindowHandles()
|
|
|
|
curHandle = await this.driver.getWindowHandle()
|
2019-05-09 13:03:18 +03:00
|
|
|
if (allHandles.length > 2) {
|
2019-07-02 16:12:57 +03:00
|
|
|
allHandles = [allHandles[0], allHandles[1]]
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
2019-07-02 16:12:57 +03:00
|
|
|
let handle
|
2019-05-09 13:03:18 +03:00
|
|
|
for (let i = 0; i < allHandles.length; i++) {
|
|
|
|
if (curHandle !== allHandles[i]) {
|
2019-07-02 16:12:57 +03:00
|
|
|
handle = allHandles[i]
|
|
|
|
break
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.switchTo().window(handle)
|
|
|
|
await this.driver.sleep(500)
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async refresh() {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.navigate().refresh()
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async getUrl() {
|
2019-07-02 16:12:57 +03:00
|
|
|
return await this.driver.getCurrentUrl()
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async open(url) {
|
2019-07-02 16:12:57 +03:00
|
|
|
await this.driver.get(url)
|
|
|
|
return this.getUrl()
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async clickKey(key, times) {
|
|
|
|
try {
|
2019-07-02 16:12:57 +03:00
|
|
|
const action = this.driver.actions()
|
|
|
|
for (let i = 0; i < times; i++) await action.sendKeys(key).perform()
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
2019-05-09 13:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-07-02 16:12:57 +03:00
|
|
|
Page
|
|
|
|
}
|