0

This is my first jest test with puppeteer. I am trying to log in to a site and test that the title of the first page after log in is correct.

This is being done in headful mode.

I can see that the login is successful. However the test fails and the vsCode terminal shows the error(s) as below. Please advise.

    /**
 * @name Onplan Login
 * @desc Logs in and test for correct page title
 */
const puppeteer = require('puppeteer');


beforeAll(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        devtools: true
    });
    const page = await browser.newPage();

    //await page.setViewport({ width: 4096, height: 1800 })
    await page.goto('https://uat2.onplanapp.com/#/sheet/139');

    await page.waitFor(1000);//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(1000);//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(300);// was 1000
    // Select dropdown

    //select company option


    await page.click('#react-select-2-option-2'); //Test company
    await page.waitFor(100);//was 1000
    await page.click('button[class="btn btn-block mt-3"]');



})
describe('Login', () => {
    test('should be titled "Onplan"', async () => {
        await page.waitFor(500);//was 1000
        expect.assertions(1);
        await expect(page.$('title').resolves.toEqual('Onplan'));
    });
}, 10000)

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

//////////////////// Error Message //////////////////// PS F:\Projects\onplan_jest> npm test

PS F:\Projects\onplan_jest> npm test

onplan_jest@1.0.0 test F:\Projects\onplan_jest jest

FAIL ./login.test.js ● Test suite failed to run

Protocol error: Connection closed. Most likely the page has been closed.

  at Object.assert (node_modules/puppeteer/lib/helper.js:28:15)

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 6.811 s Ran all test suites. (node:501740) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
at F:\Projects\onplan_jest\node_modules\puppeteer\lib\Connection.js:148:63 at new Promise () at CDPSession.send (F:\Projects\onplan_jest\node_modules\puppeteer\lib\Connection.js:147:16) at ExecutionContext._evaluateInternal (F:\Projects\onplan_jest\node_modules\puppeteer\lib\ExecutionContext.js:77:50) at ExecutionContext.evaluateHandle (F:\Projects\onplan_jest\node_modules\puppeteer\lib\ExecutionContext.js:35:21) at ExecutionContext. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) at ElementHandle.evaluateHandle (F:\Projects\onplan_jest\node_modules\puppeteer\lib\JSHandle.js:43:46) at ElementHandle. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) at ElementHandle.$ (F:\Projects\onplan_jest\node_modules\puppeteer\lib\JSHandle.js:347:35) at ElementHandle. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) -- ASYNC -- at ExecutionContext. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:82:19) at ElementHandle.evaluateHandle (F:\Projects\onplan_jest\node_modules\puppeteer\lib\JSHandle.js:43:46) at ElementHandle. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) at ElementHandle.$ (F:\Projects\onplan_jest\node_modules\puppeteer\lib\JSHandle.js:347:35) at ElementHandle. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) at DOMWorld.$ (F:\Projects\onplan_jest\node_modules\puppeteer\lib\DOMWorld.js:96:38) at processTicksAndRejections (internal/process/task_queues.js:97:5) -- ASYNC -- at Frame. (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:82:19) at Page.$ (F:\Projects\onplan_jest\node_modules\puppeteer\lib\Page.js:219:33) at Page.$ (F:\Projects\onplan_jest\node_modules\puppeteer\lib\helper.js:83:27) at Object. (F:\Projects\onplan_jest\login.test.js:48:27) (node:501740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:501740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ERROR: The process "497896" not found. 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. at Object. (F:\Projects\onplan_jest\login.test.js:48:27) (node:501740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:501740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ERROR: The process "497896" not found. 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. (node:501740) UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down

Assignment to constant variable. (node:501740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

///////////// title enter image description here

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Steven Greenbaum
  • 311
  • 4
  • 17
  • 1
    I don't know if this will fix your problem but you are creating the browser and the page in one variable scope and trying to use them later in unrelated scopes. One way to fix this is to put the browser and page variable in the outer scope `var browser = nuil` just after the import and remove the `const` qualifier when you initialize it and similarly for page, maybe. – Aaron Watters May 08 '20 at 19:28
  • thank you@Aaron Watters . I changed the scope of those variables and made some other changes as well. Still not working but getting different/simpler error now error now. I'm not sure what the forum protocol is because I still have an issue, but your suggestion did help. I marked this as a useful answer and will post a new topic for the current situation. Hope that works – Steven Greenbaum May 08 '20 at 22:39
  • 1
    It looks to me like your are not really using jest-puppeteer the way it was designed to be used because you are starting and stopping the browser yourself. Have a look at the tests here for a more conventional approach: https://github.com/AaronWatters/hello_jest – Aaron Watters May 09 '20 at 02:28
  • @Aaron Watters, I will look at your headless test examples from github. Thank you. I got the structure of my code from some examples that I saw. AfterAll- browser.close also shows up in some examples. If I comment out browser.close, I still have my problem. The problem seems to be that the test fails before the browser finishes navigating. My problem does not seem related to stopping the browser. – Steven Greenbaum May 09 '20 at 20:32

0 Answers0