Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/helper/WebDriverIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,14 +843,28 @@ class WebDriverIO extends Helper {
waitForText(text, sec, context) {
sec = sec || 1;
context = context || 'body';
return this.browser.waitUntil(function () {
this.getText(context).then(function (source) {
if (Array.isArray(source)) {
return source.filter(part => part.indexOf(text) >= 0).length > 0;
return this.browser
.waitUntil(function () {
return this.getText(context).then(function (source) {
if (Array.isArray(source)) {
return source.filter(part => part.indexOf(text) >= 0).length > 0;
}
return source.indexOf(text) >= 0;
});
}, sec * 1000)
.catch((e) => {

if (e.type === 'CommandError') {

return proceedSee.call(this, 'assert', text, context);

} else {

throw e;

}
return source.indexOf(text) >= 0;

});
}, sec * 1000);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/data/app/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,10 @@ function GET() {
header('Set-Cookie: a=; Expires=Thu, 01 Jan 1970 00:00:01 GMT');
}
}

class dynamic {
public function GET()
{
include __DIR__.'/view/dynamic.php';
}
}
1 change: 1 addition & 0 deletions test/data/app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'/unset-cookie' => 'unsetCookie',
'/external_url' => 'external_url',
'/iframe' => 'iframe',
'/dynamic' => 'dynamic',
);

glue::stick($urls);
12 changes: 12 additions & 0 deletions test/data/app/view/dynamic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>

<div id="text">Static text</div>

<script>
setTimeout(function () {
document.querySelector('#text').textContent = 'Dynamic text';
}, 1000);
</script>
</body>
</html>
42 changes: 41 additions & 1 deletion test/unit/helper/WebDriverIO_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let path = require('path');
let fs = require('fs');
let fileExists = require('../../../lib/utils').fileExists;
const dataFile = path.join(__dirname, '../../data/app/db');

let AssertionFailedError = require('../../../lib/assert/error');

function formContents(key) {
let data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
Expand Down Expand Up @@ -520,4 +520,44 @@ describe('WebDriverIO', function () {
.then(() => wd.cancelPopup());
});
});

describe('#waitForText', () => {
it('should wait for text', () => {
return wd.amOnPage('/dynamic')
.then(() => wd.dontSee('Dynamic text'))
.then(() => wd.waitForText('Dynamic text', 2))
.then(() => wd.see('Dynamic text'));
});

it('should wait for text in context', () => {
return wd.amOnPage('/dynamic')
.then(() => wd.dontSee('Dynamic text'))
.then(() => wd.waitForText('Dynamic text', 2, '#text'))
.then(() => wd.see('Dynamic text'));
});

it('should return error if not present', () => {
return wd.amOnPage('/dynamic')
.then(() => wd.waitForText('Nothing here', 0, '#text'))
.catch((e) => {

e.should.be.instanceOf(AssertionFailedError);
e.inspect().should.be.equal('expected element #text to include Nothing here');

});
});

it('should return error if waiting is too small', () => {
return wd.amOnPage('/dynamic')
.then(() => wd.waitForText('Dynamic text', 0.5))
.catch((e) => {

e.should.be.instanceOf(AssertionFailedError);
e.inspect().should.be.equal('expected element body to include Dynamic text');

});
});

});

});