diff --git a/src/build-code/buildTemplate.ts b/src/build-code/buildTemplate.ts index 79f9e822d..93075c2f1 100644 --- a/src/build-code/buildTemplate.ts +++ b/src/build-code/buildTemplate.ts @@ -45,15 +45,17 @@ export const buildImports = ({ if (device) { if (useTypeScript) { - imports += 'import { devices } from "playwright";\n'; + imports += 'import { Browser, Page, devices } from "playwright";\n'; } else { imports += 'const { devices } = require("playwright");\n'; } } - if (useTypeScript) { + if (useTypeScript && !device) { imports += 'import { Browser, Page } from "playwright";\nimport qawolf from "qawolf";\n'; + } else if (useTypeScript) { + imports += 'import qawolf from "qawolf";\n'; } else { imports += 'const qawolf = require("qawolf");\n'; } diff --git a/test/build-code/__snapshots__/buildTemplate.test.ts.snap b/test/build-code/__snapshots__/buildTemplate.test.ts.snap index 34eeee33d..6886b4bfa 100644 --- a/test/build-code/__snapshots__/buildTemplate.test.ts.snap +++ b/test/build-code/__snapshots__/buildTemplate.test.ts.snap @@ -7,8 +7,7 @@ const selectors = require(\\"./selectors/myScript.json\\");" `; exports[`buildImports imports when typescript 2`] = ` -"import { devices } from \\"playwright\\"; -import { Browser, Page } from \\"playwright\\"; +"import { Browser, Page, devices } from \\"playwright\\"; import qawolf from \\"qawolf\\"; const selectors = require(\\"./selectors/myScript.json\\"); const device = devices[\\"iPhone 11\\"];" @@ -178,3 +177,56 @@ test('myName', async () => { await qawolf.create(); });" `; + +exports[`buildTemplate builds test template 4`] = ` +"import { Browser, Page } from \\"playwright\\"; +import qawolf from \\"qawolf\\"; +const selectors = require(\\"./selectors/myName.json\\"); + +let browser: Browser; +let page: Page; + +beforeAll(async () => { + browser = await qawolf.launch(); + const context = await browser.newContext(); + await qawolf.register(context); + page = await context.newPage(); +}); + +afterAll(async () => { + await qawolf.stopVideos(); + await browser.close(); +}); + +test('myName', async () => { + await page.goto(\\"www.qawolf.com\\"); + await qawolf.create(); +});" +`; + +exports[`buildTemplate builds test template 5`] = ` +"import { Browser, Page, devices } from \\"playwright\\"; +import qawolf from \\"qawolf\\"; +const selectors = require(\\"./selectors/myName.json\\"); +const device = devices[\\"iPhone 7\\"]; + +let browser: Browser; +let page: Page; + +beforeAll(async () => { + browser = await qawolf.launch(); + const context = await browser.newContext({ ...device }); + await qawolf.register(context); + page = await context.newPage(); +}); + +afterAll(async () => { + await qawolf.stopVideos(); + await browser.close(); +}); + +test('myName', async () => { + await page.goto(\\"www.qawolf.com\\"); + await qawolf.create(); +});" +`; diff --git a/test/build-code/buildTemplate.test.ts b/test/build-code/buildTemplate.test.ts index c7763f4f9..88e59c09d 100644 --- a/test/build-code/buildTemplate.test.ts +++ b/test/build-code/buildTemplate.test.ts @@ -88,6 +88,19 @@ describe('buildTemplate', () => { statePath: 'admin.json', }); expect(template).toMatchSnapshot(); + + template = buildTemplate({ + ...options, + useTypeScript: true, + }); + expect(template).toMatchSnapshot(); + + template = buildTemplate({ + ...options, + useTypeScript: true, + device: 'iPhone 7', + }); + expect(template).toMatchSnapshot(); }); });