How to re-use groups of tests in Protractor - angularjs

I'm working on a wizard-style application using AngularJS, and using ProtractorJS for E2E testing. I've defined PageObjects representing various screens and objects on the page.
In writing e2e tests for the app, I've noticed that several of the tests move through several wizard pages in the same way, and I'd like to define reusable sets of tests that I could invoke as part of larger test suites. I've attempted to do this by creating functions which have a describe(... it (...); it(...)) sequence inside but my issue is passing a reference to the new PageObject out of the function, since the test suites are executed after the function has already returned.

Related

Visualization of unit tests

I recently tried TDD methodology and i really liked it. You can write some tests for specified unit, imitate different behavior, data and mock object that allows you to check only small piece of code without need of running entire application. But I have some questions about unit visualization.
Suppose we have a simple chat application with homepage, lobby and chat widget components (p. 1).
When you are working on chat widget component (for example), you can write unit test for it and don't care about other components. But what if want to see widget render results? It is so annoying to run entire application, go to lobby page, switch to chat widget tab every time I changed my code.
Are there are any practices to run render unit tests? Does it depend on technology stack?
My frontend stack: React, Redux, Jest + React testing library.
If a test shows you rendered content, than it is not a unit test. The result of a unit test must be binary (failure or success). If you have to look at test output to figure out if it was successful, it is not a unit test.
What you are looking for it not unit tests but UI test. For the Web context selenium comes to mind. It is used the define scenarios for poking at your UI and asserting on outcomes. You can also use it to automate the process of
"run entire application, go to lobby page, switch to chat widget tab every time I changed my code".

Apex Lightning upload fail because missing test class

I have EntertainmentController, and `EntertainmentControllerTestClass. Inside test class, I add test method refer to each method in the controller, all of them are empty methods so they all pass when run test. But when I upload to package application, I got the message:
Upload Failed:
No test methods found in the Apex code included in the package. At least 75% test coverage is required.
Apex Tests have to actually test your business classes. Having empty tests serves no purpose. You must write Unit Tests that validate the code in your EntertainmentController class. The Unit Tests must result in 75% of the code in the EntertainmentController being executed without failures during test execution.
Also, when you add your Apex Classes to your package, you have to add both your EntertainmentController and EntertainmentControllerTestClass.
Testing Apex

Angular Unit/E2E testing with protractor and jasmine

I am writing an angular application, whereby my controller calls an API, that returns live data which I then display on my html doc.
I am using Protractor for my end to end tests, and jasmine for unit testing.
I am mocking my API call, to ensure the API is not called.
My question is whether I should be testing the API call with protractor, and check whether my html doc is updated following the GET request, or whether I should test the API call when conducting my unit tests with jasmine.
I have a feeling that the answer is that I should be testing this API call with both my unit and end to end tests, but am hoping someone on SO can provide clarity.
The main goal of unit testing is to test that your code (be it JavaScrip or otherwise) is doing what it should. Each test should be done against data that static or contrived and should never be run against an API. Static data gives you the control you need. If your code needs to branch when X equals 7, you can purposely set that value and verify that your code does indeed branch. When you run against an API you do not have that control. Even if you are the one that controls the API, doing unit testing against it is a bad habit to get into.
End to end testing is completely different. Here we are not testing that the code works on a granular level (we already did that in our unit tests) we are testing that the application works as a whole. When a specific button is clicked in the application, did the expected things happen? Do all of the expected elements appear on the page? You still need to be testing against known data, and doing that is just as crucial as in unit testing, but here you get to see how your app reacts against when running. Did a particular screen take too long to load? Did a button click not give you what you expected? This kind of testing lets you click through your application as a user would (except much faster.)
You should run both kinds of tests on your app. Unit tests should be run during the build process, and end to end tests should be run once the build completes.

How to test angular views accessible after a login?

I am using protractor to test my angular app, where most of the views are accessible for authenticated users only.
Currently, I placed the login action in beforeEach:
beforeEach(function() {
browser.get(site + '/account/login');
element(by.id('Username')).sendKeys('testuser');
element(by.id('Password')).sendKeys('letmein');
element(by.css('[value="Log In"]')).click();
});
The problem is for every it('should....') test, the webdriver visits the login page and perform the same log in ritual over and over again.
I guess there must be a better way to do it. How can we set up correctly so that the webdriver login only once and then perform all the it('should....') tests?
This can be done via the onPrepare field of the protractor configuration file.
An example configuration file with login is provided in Protractor itself.
onPrepare: function() {
browser.driver.get(env.baseUrl + '/login.html');
browser.driver.findElement(by.id('username')).sendKeys('Jane');
browser.driver.findElement(by.id('password')).sendKeys('1234');
...
}
Alternatively, you can make use of the fact that Jasmine tests are ordered. Thus, instead of logging in before every test, you can have your first it login.
I typically organize my Jasmine tests into larger suites, with the first it signing in, and the last it signing off.
The obvious disadvantage is that your tests are not entirely independent, but in return test execution is substantially faster. You can even argue that doing multiple things after logging in is more realistic than logging on and off for every action.
Yet another solution is to use the Jasmine 2.x beforeAll and afterAll notations of (see this answer). This does require that you use the newest version of Protractor, as pre 1.5 versions of Protractor only worked with Jasmine 1.x.

Possible to get unique browser instances in e2e specs written for protractor?

I'm using protractor to run some end-2-end tests for an Angular application, and from what I can tell, all the specs I specify in my config file end up sharing the same instance of protractor's browser.
Normally this wouldn't be a problem, but I am also using http-backend-proxy to set up mock responses to my application's ajax calls. This module uses the browser instance, and it seems that setting up mock responses in one spec pollutes the browser instance for subsequent specs, causing all the specs to use the mock response strategy defined in the first spec.
Obviously this creates some problems for test isolation.
Has anyone dealt with something similar?
[edit]
I did not find a way to create multiple browser instances, but did find a way to deregister http-backend-proxy responses between tests, which accomplishes the test isolation I was looking for. For future reference to anyone that runs into this same problem, I was using:
proxy.onLoad.when()
setups to handle my mock responses. You can deregister these with:
proxy.onLoad.reset()
If you place these in beforeEach() and afterEach() blocks, respectively, you can accomplish isolation both between tests within a given spec, and between separate spec files.

Resources