Skip to content

Wait events #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 12, 2022
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Once that is done, feed the JSON into the recorder script to generate the [WPT c
- `change` (maps to `execAndWait`)
- `keydown` (maps to `execAndWait`)
- `keyup` (maps to `execAndWait`)
- `waitForElement` (maps to `waitFor`)
- `waitForExpression` (maps to `waitFor`)
- `doubleClick` (maps to `execAndWait`)
- `scroll` (maps to `execAndWait`)

## Resources
- [Sample JSON recordings](/sample-recordings)
Expand Down
84 changes: 54 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,45 @@ let convert = function (flow) {

let isKeyDown = false;

//first, is it a valid step?
function isValid(stepType) {
if (stepMap[stepType]) {
return true;
} else {
return false;
}
}
function addViewport(step) {
wptScript += `setViewportSize ${step.width} ${step.height}\n`;
}

function addNavigate(url) {
wptScript += "setEventName Navigate\n";
wptScript += "navigate " + url + "\n";
}

function addClick(selectors) {
wptScript += "setEventName Click\n";
//for now, let's skip any aria/ until we figure somethign out there
for (let index = 0; index < selectors.length; index++) {
const selector = selectors[index];
selectors.forEach((selector) => {
if (!selector[0].startsWith("aria/")) {
wptScript +=
'execAndWait document.querySelector("' + selector + '").click();\n';
break;
wptScript += 'execAndWait document.querySelector("' + selector + '").click();\n';
}
}
});
}

function addChange(selectors, value) {
if (isKeyDown) {
wptScript += "setEventName KeyDown\n";

for (let index = 0; index < selectors.length; index++) {
const selector = selectors[index];
selectors.forEach((selector) => {
if (!selector[0].startsWith("aria/")) {
wptScript +=
'execAndWait document.querySelector("' + selector + '").click();\n';
break;
wptScript += 'execAndWait document.querySelector("' + selector + '").click();\n';
}
}
});
} else {
wptScript += "setEventName Change\n";
//for now, let's skip any aria/ until we figure somethign out there
for (let index = 0; index < selectors.length; index++) {
const selector = selectors[index];
selectors.forEach((selector) => {
if (!selector[0].startsWith("aria/")) {
wptScript +=
'execAndWait document.querySelector("' +
selector +
'").value = "' +
value +
'";\n';
break;
'execAndWait document.querySelector("' + selector + '").value = "' + value + '";\n';
}
}
});
}
}

function addKeyDown(assertedEvents) {
//Because some keydown events are returning url's as assertedEvents
if (assertedEvents) {
Expand All @@ -100,9 +83,38 @@ let convert = function (flow) {
isKeyDown = true;
}
}

function addKeyUp() {
isKeyDown = false;
}

function addWaitForElement(selectors) {
selectors.forEach((selector) => {
wptScript += "setEventName WaitForElement\n";
wptScript += `waitFor document.querySelector("${selector}")\n`;
});
}

function addWaitFor(step) {
wptScript += "setEventName WaitForExpression\n";
wptScript += "waitFor " + step.expression + "\n";
}

function doubleClick(selectors) {
wptScript += "setEventName doubleClick\n";
//for now, let's skip any aria/ until we figure somethign out there
selectors.forEach((selector) => {
if (!selector[0].startsWith("aria/")) {
wptScript += `execAndWait document.querySelector('${selector}').dispatchEvent(new MouseEvent('dblclick'))\n`;
}
});
}

function scroll(step) {
wptScript += "setEventName Scroll\n";
wptScript += `execAndWait window.scrollBy(${step.x},${step.y})\n`;
}

function addScriptLine(step) {
switch (step.type) {
case "setViewport":
Expand All @@ -123,6 +135,18 @@ let convert = function (flow) {
case "keyUp":
addKeyUp();
break;
case "waitForElement":
addWaitForElement(step.selectors);
break;
case "waitForExpression":
addWaitFor(step);
break;
case "doubleClick":
doubleClick(step.selectors);
break;
case "scroll":
scroll(step);
break;
}
}

Expand Down