Skip to content

Commit

Permalink
Fix switchTo to accept regex (#1151)
Browse files Browse the repository at this point in the history
* #1147 Fix switchTo to accept regex

- update empty string check
- add tests for regex switchTo
- update wait for readystate to timeout on navigation timeout instead of default.

* Bump up version to 1.0.7

* Revert "Bump up version to 1.0.7"

This reverts commit 3f0628a.
  • Loading branch information
NivedhaSenthil committed Mar 24, 2020
1 parent eb45616 commit 919f776
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/doActionAwaitingNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ const waitForNavigation = (timeout, promises = []) => {
);
},
defaultConfig.retryInterval,
defaultConfig.retryTimeout,
timeout,
)
.then(resolve)
.catch(reject);
.catch(() => reject('Timedout'));
})
.catch(reject);
const timeoutId = setTimeout(() => reject('Timedout'), timeout);
Expand Down
2 changes: 1 addition & 1 deletion lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ module.exports.switchTo = async arg => {
'The "targetUrl" argument must be of type string or regex. Received type ' + typeof arg,
);
}
if (arg.trim() == '') {
if (typeof arg === 'string' && arg.trim() === '') {
throw new Error(
'Cannot switch to tab or window. Hint: The targetUrl is empty. Please use a valid string or regex',
);
Expand Down
12 changes: 12 additions & 0 deletions test/unit-tests/switchTo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const taiko = rewire('../../lib/taiko');
chai.use(chaiAsPromised);

describe('switchTo', () => {
let argument;
before(async () => {
taiko.__set__('validate', () => {});
taiko.__set__('targetHandler.getCriTargets', arg => {
argument = arg;
return { matching: [] };
});
});

it('should throw error if no url specified', async () => {
Expand All @@ -25,4 +30,11 @@ describe('switchTo', () => {
'Cannot switch to tab or window. Hint: The targetUrl is empty. Please use a valid string or regex',
);
});

it('should accept regex and call targetHandler with RegExp', async () => {
await expect(taiko.switchTo(/http(s):\/\/www.google.com/)).to.eventually.rejectedWith(
'No tab(s) matching /http(s):\\/\\/www.google.com/ found',
);
await expect(argument).to.deep.equal(new RegExp(/http(s):\/\/www.google.com/));
});
});

0 comments on commit 919f776

Please sign in to comment.