Skip to content
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

[Question] Playwright and TypeScript #2978

Closed
randomactions opened this issue Jul 16, 2020 · 7 comments
Closed

[Question] Playwright and TypeScript #2978

randomactions opened this issue Jul 16, 2020 · 7 comments

Comments

@randomactions
Copy link

Hello,

I am sorry for asking such a stupid question, but is there an example of tests written with Playwright in TS? I would really appreciate sharing this.

@arjunattam
Copy link
Contributor

Thanks @randomactions. All examples in our docs will work in TypeScript (since it is a superset of JS). Are you looking for something in particular?

@randomactions
Copy link
Author

Hi Arjun,

I am new to TS and would like to see how people implement page object model with TS. I am also wondering how to run TS tests with Mocha or Jest. Do I have to compile them to JS first?

@randomactions
Copy link
Author

One of the things I am struggling with is which type "page" should be assigned.

When it is an object, then I get "property "goto" does not exist on type object":

class mainPage {
    constructor(public page: object) {
        this.page = page;
    }

    public async navigateTo(pageURL: string): Promise<void> {
        await this.page.goto(BASE_URL + pageURL);
       };

@mxschmitt
Copy link
Member

One of the things I am struggling with is which type "page" should be assigned.

When it is an object, then I get "property "goto" does not exist on type object":

class mainPage {
    constructor(public page: object) {
        this.page = page;
    }

    public async navigateTo(pageURL: string): Promise<void> {
        await this.page.goto(BASE_URL + pageURL);
       };

Playwright provides types for you which you can use:

import type { Browser, Page, BrowserContext } from 'playwright' // or -chromium etc.

@randomactions
Copy link
Author

randomactions commented Jul 24, 2020

Max, thanks a lot! This helps!

Another thing that I might have missed in the documentation is -- does TS need to be compiled to JS with tsc first, or Playwright can manage running TS without compiling?

@mxschmitt
Copy link
Member

Max, thanks a lot! This helps!

Another thing that I might have missed in the documentation is -- does TS need to be compiled to JS with tsc first, or Playwright can manage running TS without compiling?

Playwright is a library which is written in TypeScript and provides a JavaScript bundle and optional types which Node and your editor like Visual Studio Code can consume. So if you write JavaScript and use type annotations by JSDoc, then you don't need to compile it with tsc.
But if you are writing TypeScript, it seems like you do, then you need tsc or ts-node to compile your TypeScript first to JavaScript and run it then afterwards.

@randomactions
Copy link
Author

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants