diff --git a/lib/helper/WebDriverIO.js b/lib/helper/WebDriverIO.js index d1a54f63a..458380b31 100644 --- a/lib/helper/WebDriverIO.js +++ b/lib/helper/WebDriverIO.js @@ -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); } /** diff --git a/test/data/app/controllers.php b/test/data/app/controllers.php index e97ff807d..86e369535 100755 --- a/test/data/app/controllers.php +++ b/test/data/app/controllers.php @@ -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'; + } +} diff --git a/test/data/app/index.php b/test/data/app/index.php index f8311b23f..2c67f1631 100755 --- a/test/data/app/index.php +++ b/test/data/app/index.php @@ -37,6 +37,7 @@ '/unset-cookie' => 'unsetCookie', '/external_url' => 'external_url', '/iframe' => 'iframe', + '/dynamic' => 'dynamic', ); glue::stick($urls); diff --git a/test/data/app/view/dynamic.php b/test/data/app/view/dynamic.php new file mode 100644 index 000000000..762f88460 --- /dev/null +++ b/test/data/app/view/dynamic.php @@ -0,0 +1,12 @@ + + + +
Static text
+ + + + diff --git a/test/unit/helper/WebDriverIO_test.js b/test/unit/helper/WebDriverIO_test.js index 81f4b9673..3f2486a63 100644 --- a/test/unit/helper/WebDriverIO_test.js +++ b/test/unit/helper/WebDriverIO_test.js @@ -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')); @@ -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'); + + }); + }); + + }); + });