Question

Is there any way to essentially do Unit testing with Protractor/WebDriver? What I'd ideally like to do is isolate a single directive/component, mock its dependencies, inject various bits of data, and walk through it with Protractor in a bunch of browsers.

Is there any way of doing this?

Was it helpful?

Solution

Protractor (and more widely, Selenium) is generally meant for automating the kinds of tests a non-technical end user would be capable of performing themselves. Having said that, there are times when it may be useful to simulate various error conditions on the server. You have a couple options:

  1. onPrepare script: Use the onPrepare script to do as much backend setup work as you can, if it's possible to do so ahead of time. Set the onPrepare property inside of your protractor config file (see sample here) to point to your prepare script, where you can set up records in the database, create files, or whatever else you may need to do.

  2. Use protractor.addMockModule: This allows you to essentially mock your backend API by adding a module to load before Angular whenever Protractor.get is called. See the blog post for a sample (search for "Beyond the basics"):
    http://product.moveline.com/testing-angular-apps-end-to-end-with-protractor.html

  3. Configure test-only routes on the server: As a last resort, you can configure some routes on the server that are only accessible in a dev environment that perform whatever backend processing you might need from within your end-to-end test. For instance, if you're using Node.js with Express.js, you might do something like the below.

Test-only routes:

// Store your server-side e2e methods in this file
var e2eBackend = require('./test/e2eBackend');

if (app.get('env') === "development") {
    app.get('/e2e/simulateCrash', e2eBackend.simulateCrash);
    // etc.
}

OTHER TIPS

Protractor is mainly used to for e2e test. Testing by end user perspective. You can use jasmine frame work for unit testing and protractor e2e test supports jasmine also.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top