Skip to content

Commit

Permalink
test: create/update tests for nav steps
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed authored and jperl committed Sep 21, 2020
1 parent 3059fcc commit a948953
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
120 changes: 119 additions & 1 deletion test/build-code/buildStepLines.test.ts
@@ -1,6 +1,7 @@
import {
buildStepLines,
escapeSelector,
StepLineBuildContext,
} from '../../src/build-code/buildStepLines';
import { Action } from '../../src/types';
import { baseStep } from './fixtures';
Expand Down Expand Up @@ -37,7 +38,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page2 = await qawolf.waitForPage(page.context(), 1);",
"const page2 = await qawolf.waitForPage(context, 1, { waitUntil: \\"domcontentloaded\\" });",
"await page2.click('[data-qa=\\"test-input\\"]');",
]
`);
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"const frame = await (await page.waitForSelector(\\"#frameId\\")).contentFrame();",
"await frame.click('[data-qa=\\"test-input\\"]');",
]
Expand Down Expand Up @@ -149,6 +151,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.click('[data-qa=\\"test-input\\"]');",
]
`);
Expand All @@ -163,6 +166,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.fill('[data-qa=\\"test-input\\"]', \\"hello\\");",
]
`);
Expand All @@ -177,6 +181,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.press('[data-qa=\\"test-input\\"]', \\"Enter\\");",
]
`);
Expand All @@ -194,6 +199,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await qawolf.scroll(page, '[data-qa=\\"test-input\\"]', { x: 100, y: 200 });",
]
`);
Expand All @@ -208,6 +214,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.selectOption('[data-qa=\\"test-input\\"]', \\"spirit\\");",
]
`);
Expand All @@ -223,6 +230,7 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.type('[data-qa=\\"test-input\\"]', \\"spirit\\");",
]
`);
Expand All @@ -237,9 +245,119 @@ describe('buildStepLines', () => {

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.type('[data-qa=\\"test-input\\"]', null);",
]
`);
});
});

test('goto step', () => {
const buildContext: StepLineBuildContext = {
initializedFrames: new Map<string, string>(),
initializedPages: new Set([]),
visiblePage: 0,
};

// Initial page
let lines = buildStepLines(
{
action: 'goto' as Action,
event: {
name: 'goto',
page: 0,
time: Date.now(),
},
index: 0,
value: 'http://localhost:5000',
},
buildContext,
);

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await context.newPage();",
"await page.goto(\\"http://localhost:5000\\", { waitUntil: \\"domcontentloaded\\" });",
]
`);

// Manual new tab
lines = buildStepLines(
{
action: 'goto' as Action,
event: {
name: 'goto',
page: 1,
time: Date.now(),
},
index: 1,
value: 'https://google.com',
},
buildContext,
);

expect(lines).toMatchInlineSnapshot(`
Array [
"const page2 = await context.newPage();",
"await page2.bringToFront();",
"await page2.goto(\\"https://google.com\\", { waitUntil: \\"domcontentloaded\\" });",
]
`);
});

test('back step', () => {
const buildContext: StepLineBuildContext = {
initializedFrames: new Map<string, string>(),
initializedPages: new Set([]),
visiblePage: 0,
};

const lines = buildStepLines(
{
action: 'goBack' as Action,
event: {
name: 'goBack',
page: 0,
time: Date.now(),
},
index: 0,
},
buildContext,
);

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.goBack({ waitUntil: \\"domcontentloaded\\" });",
]
`);
});

test('reload step', () => {
const buildContext: StepLineBuildContext = {
initializedFrames: new Map<string, string>(),
initializedPages: new Set([]),
visiblePage: 0,
};

const lines = buildStepLines(
{
action: 'reload' as Action,
event: {
name: 'reload',
page: 0,
time: Date.now(),
},
index: 0,
},
buildContext,
);

expect(lines).toMatchInlineSnapshot(`
Array [
"const page = await qawolf.waitForPage(context, 0, { waitUntil: \\"domcontentloaded\\" });",
"await page.reload({ waitUntil: \\"domcontentloaded\\" });",
]
`);
});
});
29 changes: 28 additions & 1 deletion test/create-code/ContextEventCollector.test.ts
Expand Up @@ -15,6 +15,7 @@ describe('ContextEventCollector', () => {
let browser: Browser;
let context: BrowserContext;
let events: ElementEvent[];
let windowEvents: ElementEvent[];

beforeAll(async () => {
browser = await launch();
Expand All @@ -23,9 +24,13 @@ describe('ContextEventCollector', () => {

const collector = await ContextEventCollector.create(context);
collector.on('elementevent', (event) => events.push(event));
collector.on('windowevent', (event) => windowEvents.push(event));
});

beforeEach(() => (events = []));
beforeEach(() => {
events = [];
windowEvents = [];
});

afterAll(() => browser.close());

Expand Down Expand Up @@ -184,4 +189,26 @@ describe('ContextEventCollector', () => {
(events.filter((e) => isKeyEvent(e)) as KeyEvent[]).map((e) => e.value),
).toEqual(['s', 's', 'u', 'u', 'p', 'p', 'Tab', 'Tab', 'y', 'y', 'o', 'o']);
});

it('collects back button press and new tab with typed address', async () => {
const page = await context.newPage();

await page.goto(TEST_URL);
await page.goto(`${TEST_URL}text-inputs`);
await page.goBack();
await page.close();

expect(windowEvents.pop().name).toBe('goBack');
expect(windowEvents.pop().name).toBe('goto');
});

it('collects reload button', async () => {
const page = await context.newPage();

await page.goto(TEST_URL);
await page.reload();
await page.close();

expect(windowEvents.pop().name).toBe('reload');
});
});

0 comments on commit a948953

Please sign in to comment.