Skip to content

Commit

Permalink
add waitUntilVisible()
Browse files Browse the repository at this point in the history
  • Loading branch information
dotneet committed Mar 13, 2018
1 parent acfc2af commit a2fbfe8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,33 @@ class Document {
return result
}

async waitUntilVisible (selector) {
let check = null
let startTime = Date.now()
await new Promise((resolve, reject) => {
check = () => {
setTimeout(async () => {
try {
const now = Date.now()
if (now - startTime > this.chromy.options.waitTimeout) {
reject(new WaitTimeoutError('waitUntilVisible() timeout', selector))
return
}
const result = await this.visible(selector)
if (result) {
resolve(result)
} else {
check()
}
} catch (e) {
reject(e)
}
}, this.chromy.options.waitFunctionPollingInterval)
}
check()
})
}

async type (expr, value) {
await this.evaluate('document.querySelector(\'' + escapeSingleQuote(expr) + '\').focus()')
const characters = value.split('')
Expand Down
19 changes: 19 additions & 0 deletions test/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ describe('wait', function() {
done(e)
})
})
it('wait until visible', (done) => {
const chromy = new Chromy()
chromy.chain()
.goto('file://' + process.env.PWD + '/test_pages/wait.html')
.evaluate(() => {
setTimeout(() => {
let div = document.createElement('div')
div.innerText = 'text'
div.classList.add('newdiv')
document.body.appendChild(div)
}, 500)
})
.waitUntilVisible("div.newdiv")
.end()
.then(_ => done())
.catch(e => {
done(e)
})
})
it('raise TimeoutError wait(function)', (done) => {
const chromy = new Chromy({waitTimeout: 100})
chromy.chain()
Expand Down

0 comments on commit a2fbfe8

Please sign in to comment.