Code Execution Using PhantomJS When Running Tests Using Karma

I run my tests using Karma on PhantomJS, and run into the problem of running the loop asynchronously. I was wondering if there is anyway interactive debugging code (step by step) when running the tests.

Any help is greatly appreciated.

+5
source share
2 answers

If you use the karma-phantomjs-launcher npm package in your karma tests, you can add the following snippet in the karma.conf.js file to run PhantomJS in debug mode:

 browsers: ['PhantomJS_custom'], // you can define custom flags customLaunchers: { 'PhantomJS_custom': { base: 'PhantomJS', debug: true } } 

Then write the debugger; statement debugger; where you want to put the breakpoint in your code.

Now, when you start the karma test drive, you will see that PhantomJS is waiting for your browser to connect to the debugger port to start the tests.

 29 12 2016 13:14:25.269:INFO [launcher]: Starting browser PhantomJS 29 12 2016 13:14:25.325:INFO [phantomjs.launcher]: ACTION REQUIRED: 29 12 2016 13:14:25.325:INFO [phantomjs.launcher]: 29 12 2016 13:14:25.326:INFO [phantomjs.launcher]: Launch browser at 29 12 2016 13:14:25.326:INFO [phantomjs.launcher]: http://localhost:9000/webkit/inspector/inspector.html?page=2 29 12 2016 13:14:25.326:INFO [phantomjs.launcher]: 29 12 2016 13:14:25.326:INFO [phantomjs.launcher]: Waiting 15 seconds ... 

Point your chrome to the address indicated in the magazines. You will now see the debugger interface maintained by Phantomjs. The code runs in the PhantomJS engine, but you can observe breakpoints, evaluate expressions, and perform many debugging operations from any web-based browser.

+2
source

You can install the debugger statement in one of the test blocks. Then point your browser to any URL where you use karma and open your developer tools. The debugger should fire when it reaches the test block.

Assuming jasmine / mocha with the expectation:

 it('fires a debugger', function () { var bool = true || false; debugger; while(bool) { // uh oh... } }); 

In my experience, the debugger will run both http://localhost:<your-karma-port> and http://localhost:<your-karma-port>/debug.html , but you may have more luck with Debug URLs.

If you cannot see your debugger instructions, there may be a big problem with setting up testing.

+1
source

Source: https://habr.com/ru/post/1201945/


All Articles