Skip to content

Commit

Permalink
Merge pull request #804 from qawolf/feat-bring-page-to-front
Browse files Browse the repository at this point in the history
feat: bring page to front before acting on it
  • Loading branch information
aldeed committed Aug 31, 2020
2 parents 949d755 + 7d5d44e commit 6dbc0a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/build-code/buildStepLines.ts
Expand Up @@ -4,6 +4,7 @@ import { isUndefined } from 'util';
export type StepLineBuildContext = {
initializedFrames: Map<string, string>;
initializedPages: Set<number>;
visiblePage: number;
};

/**
Expand Down Expand Up @@ -59,22 +60,36 @@ export const buildStepLines = (
step: Step,
buildContext: StepLineBuildContext = {
initializedFrames: new Map<string, string>(),
initializedPages: new Set(),
initializedPages: new Set([0]),
visiblePage: 0,
},
): string[] => {
const lines: string[] = [];

const { frameIndex, frameSelector, page } = step.event;
const { initializedFrames, initializedPages } = buildContext;

// The page variable is the word "page" followed by 1-based index, but just "page" for first page.
const pageVariableName = getStepPageVariableName(step);
if (page > 0 && !initializedPages.has(page)) {

// If we haven't done anything on this page yet, add a `qawolf.waitForPage` call.
// Otherwise, if we were doing steps on a different page and have now switched back
// to this one, add a `bringToFront` call. Otherwise no extra page-waiting line is needed.
if (!initializedPages.has(page)) {
lines.push(
`const ${pageVariableName} = await qawolf.waitForPage(page.context(), ${page});`,
);
initializedPages.add(page);
buildContext.visiblePage = page;
} else if (buildContext.visiblePage !== page) {
lines.push(
`await ${pageVariableName}.bringToFront();`,
);
buildContext.visiblePage = page;
}

// If the step occurred within an iframe on the page, use the frame variable as the
// context for the step. If this is the first use of this frame variable, create it first.
let frameVariableName: string;
if (frameSelector) {
frameVariableName = initializedFrames.get(frameIndex + frameSelector);
Expand All @@ -91,6 +106,7 @@ export const buildStepLines = (
}
}

// Now add the line for what the user actually did (click, scroll, fill, etc.)
lines.push(buildExpressionLine(step, frameVariableName));

return lines;
Expand Down
4 changes: 3 additions & 1 deletion src/build-code/buildVirtualCode.ts
Expand Up @@ -7,7 +7,9 @@ export const buildVirtualCode = (steps: Step[]): VirtualCode => {

const buildContext: StepLineBuildContext = {
initializedFrames: new Map<string, string>(),
initializedPages: new Set(),
// page 0 is initialized and brought to front in "before"
initializedPages: new Set([0]),
visiblePage: 0,
};

steps.forEach((step) => {
Expand Down
33 changes: 30 additions & 3 deletions test/build-code/buildStepLines.test.ts
Expand Up @@ -25,7 +25,7 @@ describe('escapeSelector', () => {
});

describe('buildStepLines', () => {
test('consecutive steps on different pages', () => {
test('step on new page, after step on a different page', () => {
const lines = buildStepLines({
...baseStep,
event: {
Expand All @@ -43,6 +43,31 @@ describe('buildStepLines', () => {
`);
});

test('step on existing page, after step on a different page', () => {
const lines = buildStepLines(
{
...baseStep,
event: {
...baseStep.event,
page: 1,
},
index: 1,
},
{
initializedFrames: new Map<string, string>(),
initializedPages: new Set([0, 1]),
visiblePage: 0,
},
);

expect(lines).toMatchInlineSnapshot(`
Array [
"await page2.bringToFront();",
"await page2.click('[data-qa=\\"test-input\\"]');",
]
`);
});

test('iframe step', () => {
const lines = buildStepLines({
...baseStep,
Expand Down Expand Up @@ -78,7 +103,8 @@ describe('buildStepLines', () => {
},
{
initializedFrames,
initializedPages: new Set(),
initializedPages: new Set([0]),
visiblePage: 0,
},
);

Expand All @@ -105,7 +131,8 @@ describe('buildStepLines', () => {
},
{
initializedFrames,
initializedPages: new Set(),
initializedPages: new Set([0]),
visiblePage: 0,
},
);

Expand Down

0 comments on commit 6dbc0a3

Please sign in to comment.