【问题标题】:How to reuse puppeteer tests如何重用 puppeteer 测试
【发布时间】:2018-11-18 05:37:57
【问题描述】:

我目前正在使用 Puppeteer 和 Jest 进行端到端测试,为了使我的测试正常工作,我总是需要运行登录测试,但我不知道也无法找出如何导出我的测试,以便我可以重复使用它们。

总结:我正在寻找一种方法来重用我在 describe 中的所有测试,方法是将它们导出到不同的文件并在新文件的 beforeAll 中重用它们。

完整的登录测试集如下:

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();
    })

【问题讨论】:

    标签: testing jestjs puppeteer end-to-end


    【解决方案1】:

    创建一个单独的文件名 test.js

    //test.js

    export async function fn1(args){
       // your commands
    }
    

    //file.test.js

    import {fn1} from 'test.js'
    
    describe('test 1 ', () => {
    test("test", async () => {
    
          try {
            await fn1(args);          
          } catch (err) {
            console.log('There are some unexpected errors: ' + err);
          }
    
      },5000);
    });
    

    我有同样的问题,上面的方法对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2019-10-16
      • 2020-08-02
      相关资源
      最近更新 更多