Skip to content

Commit

Permalink
Default waitForNavigation true for click
Browse files Browse the repository at this point in the history
  • Loading branch information
NivedhaSenthil committed Jul 17, 2018
1 parent 9c8c509 commit 9cadd78
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ module.exports.title = async () => {
return result.result.value;
};

const setOptions = (options,x,y) =>{
options.waitForNavigation = options.waitForNavigation === undefined || options.waitForNavigation === null ?
true : options.waitForNavigation;
options.timeout = options.timeout || 5000;
options.x = x;
options.y = y;
options.button = options.button || 'left';
options.clickCount = options.clickCount || 1;
return options;
};

/**
* Fetches an element with the given selector, scrolls it into view if needed, and then clicks in the center of the element. If there's no element matching selector, the method throws an error.
*
Expand All @@ -158,32 +169,29 @@ module.exports.click = click;

async function click(selector, options={}) {
validate();

const e = await element(selector, options);
await scrollTo(e);
// const type = await p.evaluate(e => e.type, e);
// assertType(e, () => type !== 'file', 'Unsupported operation, use `attach` on file input field');
const {x, y} = await boundingBox.boundingBoxCenter(dom,e);
const option = {
x: x,
y: y,
button: options.button || 'left',
clickCount: options.clickCount || 1
};

options = setOptions(options,x,y);

Promise.resolve().then(() => {
option.type = 'mousePressed';
return input.dispatchMouseEvent(option);
options.type = 'mousePressed';
return input.dispatchMouseEvent(options);
}).then(() => {
option.type = 'mouseReleased';
return input.dispatchMouseEvent(option);
options.type = 'mouseReleased';
return input.dispatchMouseEvent(options);
}).catch((err) => {
throw new Error(err);
});

if (options.waitForNavigation){
await page.loadEventFired();
await waitForNavigation(options.timeout);
}

return { description: 'Clicked ' + description(selector, true) };
}

Expand Down Expand Up @@ -1067,7 +1075,18 @@ module.exports.into = e => e;
* @param {number|time}
* @return {promise}
*/
module.exports.waitFor = (time) => new Promise(resolve => setTimeout(resolve, time));
module.exports.waitFor = waitFor;

function waitFor(time){
return new Promise(resolve => setTimeout(resolve, time));
}

const waitForNavigation = (timeout) =>{
return Promise.race([
page.loadEventFired(),
waitFor(timeout)
]);
};

const element = async (selector, options, tag) => (await elements(selector, options, tag))[0];

Expand Down

0 comments on commit 9cadd78

Please sign in to comment.