How do I debug a Testcafe browser running in a testcafe/testcafe docker container?

孤者浪人 提交于 2021-01-27 13:20:41

问题


Got a test for a React app's login function in a Page class:

  async login(t) {
    console.log('starting login...');
    debugger;
    this.logBrowserMessages(t);

    await this.loginModal({ visibilityCheck: true });
    await t
      .expect(this.loginModal.visible)
      .ok()
    // then log the user in etc... 

And the test, which passes when running locally but fails in the container

test.requestHooks(mock)('user can log in', async t => {

  await page.login(t);

  // Make sure test user's orders render.
  await t.expect(page.orders.childNodeCount).gt(1, { timeout: 10000 });

The test passes locally but fails (the login doesn't work) when running in the container.

When I run the test locally it debugs as expected:

testcafe chrome --inspect-brk=9230 ./tests/login.test.js --skip-js-errors

The process node_modules/testcafe/lib/cli shows up in my chrome://inspect/#devices, as expected.

However, when I run testcafe in the testcafe docker container it won't debug with any of these commands:

docker run --expose 9230 -p 9230:9230 -e BASIC_AUTH_USER -e BASIC_AUTH_PASS -e NODE_ENV=docker -e TEST_USER -e TEST_PASS --shm-size=1gb -v `pwd`:/tests -v `pwd`/screenshots/docker:/screenshots testcafe/testcafe 'chromium --no-sandbox' --inspect-brk=0.0.0.0:9230 -S -s '/screenshots' --skip-js-errors /tests/**/login.test.js

docker run --expose 9230 -p 9230:9230 -e \"NODE_PATH=/opt:/opt/testcafe/node_modules:/tests/node_modules\" -e BASIC_AUTH_USER -e BASIC_AUTH_PASS -e NODE_ENV=docker -e TEST_USER -e TEST_PASS --shm-size=1gb -v `pwd`:/tests -v `pwd`/screenshots/docker:/screenshots testcafe/testcafe 'chromium --no-sandbox' --inspect-brk=0.0.0.0:9230 -S -s '/screenshots' --skip-js-errors /tests/**/login.test.js

docker run --expose 9230 -p 9230:9230 -e BASIC_AUTH_USER -e BASIC_AUTH_PASS -e NODE_ENV=docker -e TEST_USER -e TEST_PASS --shm-size=1gb -v `pwd`:/tests -v `pwd`/screenshots/docker:/screenshots testcafe/testcafe 'chromium --no-sandbox' --inspect-brk=127.0.0.1:9230 -S -s '/screenshots' --skip-js-errors /tests/**/login.test.js

docker run --expose 9230 -p 9230:9230 -e \"NODE_PATH=/opt:/opt/testcafe/node_modules:/tests/node_modules\" -e BASIC_AUTH_USER -e BASIC_AUTH_PASS -e NODE_ENV=docker -e TEST_USER -e TEST_PASS --shm-size=1gb -v `pwd`:/tests -v `pwd`/screenshots/docker:/screenshots testcafe/testcafe --inspect-brk=0.0.0.0:9230 --browsers 'chromium --no-sandbox' -S -s '/screenshots' --skip-js-errors /tests/**/login.test.js

docker run --expose 9230 -p 9230:9230 -e \"NODE_PATH=/opt:/opt/testcafe/node_modules:/tests/node_modules\" -e BASIC_AUTH_USER -e BASIC_AUTH_PASS -e NODE_ENV=docker -e TEST_USER -e TEST_PASS --shm-size=1gb -v `pwd`:/tests -v `pwd`/screenshots/docker:/screenshots testcafe/testcafe 'chromium --no-sandbox' -S -s '/screenshots' --skip-js-errors --inspect-brk=9230 /tests/**/login.test.js

Is there some limitation to the container that I'm using that I'm not aware of? I've debugged Node processes running in Docker containers before, I know it can be done. But it was a while ago. Am I missing something?


回答1:


You can use the NODE_OPTIONS environment variable to enable remote debugging:

docker run -it --rm -p 9229:9229 -e NODE_OPTIONS="--inspect-brk=0.0.0.0:9229" -v /host/path/to/tests:/tests testcafe/testcafe 'chromium --no-sandbox' /tests/test.js

If you don't use docker-machine, you can open http://localhost:9229/json in your browser and navigate to a DevTools URL specified in the devtoolsFrontendUrl property.

Otherwise, use the docker-machine ip command to get the IP address of your Docker VM and open http://${DOCKER_MACHINE_IP}:9229/json to get a DevTools URL.




回答2:


On the off-chance you are developing on a Mac, one possibility is that you need to specify the host as host.docker.internal rather than 127.0.0.1 in order for the running services on the host to be visible to the container. That could be one of the discrepancies between the local and containerized test runs.

https://docs.docker.com/docker-for-mac/networking/



来源:https://stackoverflow.com/questions/55503435/how-do-i-debug-a-testcafe-browser-running-in-a-testcafe-testcafe-docker-containe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!