0

This script is logging in to a site. This is my first jest test. The last line before the test clicks a li in a ul dropdown list. We then navigate to a new page with url https://uat2.onplanapp.com/#/

As a simple test I am trying to check that this is indeed the url of the new page. When I watch in browser, the script does log in and navigate to the correct page.

Getting error shown below.

Code

/**
 * @name Onplan Login
 * @desc Logs in and test for correct page title
 */
const puppeteer = require('puppeteer');
const assert = require('assert');
let browser;
let page;
beforeAll(async () => {
    browser = await puppeteer.launch({
        headless: false,
        devtools: true
        //slowMo: 50
    });
    const page = await browser.newPage();
    await page.goto('https://uat2.onplanapp.com/#/sheet/139');
    await page.waitFor(500);//was 500
    await page.waitFor('#inputEmail');
    await page.type('#inputEmail', 'xxx@yyy');
    await page.type('#inputPassword', '1111');
    await page.click('button');//Login
    await page.waitForNavigation();
    await page.waitFor(100);//was 500
    // SELECT COMPANY ON NEXT PAGE
    await page.waitFor('#logn-screenv > form > div.row.logn-screenbody.pb-4.pt-3.px-2 > div:nth-child(2) > div > div > div.css-1hwfws3.custom-scrollbar__value-container > div.css-151xaom-placeholder.custom-scrollbar__placeholder');
    await page.click('#logn-screenv > form > div.row.logn-screenbody.pb-4.pt-3.px-2 > div:nth-child(2) > div > div > div.css-1hwfws3.custom-scrollbar__value-container > div.css-151xaom-placeholder.custom-scrollbar__placeholder');//Login

    // SELECT COMPANY 
    await page.waitFor(500);// was 1000

    // click dropdown to show list of companies
    await page.click('#react-select-2-option-2'); //Test company
    await page.waitFor(300);//was 1000

    // click list item to select company
    await page.click('button[class="btn btn-block mt-3"]');
    // now navigate to company page


})

test('url should be correct"', async () => {

    expect.assertions(1);
    const url = await page.url;
    await assert(url === 'https://uat2.onplanapp.com/#/');
});



afterAll(async () => {
    //await browser.close()
})

Error Message

onplan_jest@1.0.0 test F:\Projects\onplan_jest jest

FAIL ./login.test.js (6.165 s) × url should be correct" (2 ms)

● url should be correct"

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.

  at mapper (node_modules/jest-jasmine2/build/queueRunner.js:29:45)

● url should be correct"

TypeError: Cannot read property 'url' of undefined

  43 | 
  44 |     expect.assertions(1);
> 45 |     const url = await page.url;
     |                            ^
  46 |     await assert(url === 'https://uat2.onplanapp.com/#/');
  47 | });
  48 | 

  at Object.<anonymous> (login.test.js:45:28)

● url should be correct"

expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

  42 | test('url should be correct"', async () => {
  43 | 
> 44 |     expect.assertions(1);
     |            ^
  45 |     const url = await page.url;
  46 |     await assert(url === 'https://uat2.onplanapp.com/#/');
  47 | });

  at Object.<anonymous> (login.test.js:44:12)

Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 6.316 s, estimated 7 s Ran all test suites. Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue. End of Error Message

Steven Greenbaum
  • 311
  • 4
  • 17
  • I am the OP. I see that when the error occurs in the terminal the browser has not finished rendering the final page. So I have to figure out why this is happening. – Steven Greenbaum May 09 '20 at 12:12
  • I am the OP. I relaized that my login code should not be in the BeforeAll block. I moved it into the test. Now the test is not failing prematurely. Current error : Timeout - Async callback was not invoked within the 10000 ms timeout specified by jest.setTimeout.Error: – Steven Greenbaum May 10 '20 at 12:41

1 Answers1

0

I was able to get my first test to tun with the following code. Most of the problem was that I had code in beforeAll that I needed to move into the test. Here is the code that worked.

 * @name Onplan Login
 * @desc Logs in and test for correct page title
 */
const puppeteer = require('puppeteer');
const assert = require('assert');
let browser;
beforeAll(async () => {
    browser = await puppeteer.launch({
        headless: false,
        devtools: false,
        slowMo: 50
    });

})

describe('url should be correct"', () => {
    test('url is correct', async () => {
        const page = await browser.newPage();
        await page.goto('https://uat2.onplanapp.com/#/sheet/139');
        await page.waitFor(500);//was 500
        await page.waitFor('#inputEmail');
        await page.type('#inputEmail', 'mayank@onplan.co');
        await page.type('#inputPassword', '123456');
        await page.click('button');//Login
        await page.waitForNavigation();
        await page.waitFor(100);//was 500
        // SELECT COMPANY ON NEXT PAGE
        await page.waitFor('#logn-screenv > form > div.row.logn-screenbody.pb-4.pt-3.px-2 > div:nth-child(2) > div > div > div.css-1hwfws3.custom-scrollbar__value-container > div.css-151xaom-placeholder.custom-scrollbar__placeholder');
        await page.click('#logn-screenv > form > div.row.logn-screenbody.pb-4.pt-3.px-2 > div:nth-child(2) > div > div > div.css-1hwfws3.custom-scrollbar__value-container > div.css-151xaom-placeholder.custom-scrollbar__placeholder');//Login

        // SELECT COMPANY 
        await page.waitFor(500);// was 1000

        // click dropdown to show list of companies
        await page.click('#react-select-2-option-2'); //Test company
        await page.waitFor(300);//was 1000
        await page.click('button[class="btn btn-block mt-3"]');
        //now we go to first page
        const url = await page.url();
        expect(url).toBe('https://uat2.onplanapp.com/#/');

    }, 20000);
});
Steven Greenbaum
  • 311
  • 4
  • 17