vendredi 8 juin 2018

How to reuse puppeteer tests

I'm currently working with Puppeteer and Jest for end-to-end testing, for my tests to work I always need to run a login tests, but I don't know and haven't been able to find out how to export my tests so I can reuse them.

To conclude: I'm looking for a way to reuse all of my tests inside the describe by exporting them to a different file and reusing them in a beforeAll in the new files.

The complete set of login tests is below:

describe("homepage and login tests", homepageTests = () => {

    test("front page loads", async (done) => {
        await thePage.goto('http://localhost:3000');
        expect(thePage).toBeDefined();
        done();
    });

    test("Login button is present", async (done) => {
        theLoginButton = await thePage.$("#login-button");
        expect(theLoginButton).toBeDefined();
        done();
    })

    test("Login works", async (done) => {
        //the following code runs inside the popup
        await theBrowser.on('targetcreated', async (target) => {
            const thePopupPage = await target.page();
            if (thePopupPage === null) return;

            //get the input fields
            const usernameField = await thePopupPage.waitFor('input[name=login]');
            const passwordField = await thePopupPage.waitFor("input[name=password]");
            const submitButton = await thePopupPage.waitFor('input[name=commit]');

            //validate input fields
            expect(usernameField).not.toBeNull();
            expect(passwordField).not.toBeNull();
            expect(submitButton).not.toBeNull();

            //typing and clicking
            await thePopupPage.waitFor(300)
            await usernameField.type("USER");
            await passwordField.type("PASSWORD");
            await submitButton.click();
            done();
        })

        try {
            //wait for login button on homepage
            theLoginButton = await thePage.waitFor('#login-button');
            expect(theLoginButton).toBeDefined();

            //click on login
            await thePage.waitFor(200);
            await theLoginButton.click();

        } catch (e) { console.log(e) }
    })

    test("Arrive on new page after login", async () => {
        //resultsButton is only shown for logged in users.
        const resultsButton = await thePage.$("#resultsButton");
        expect(resultsButton).toBeDefined();
    })

Aucun commentaire:

Enregistrer un commentaire