2
votes

I am using protractor jasmine for e2e test of an angular single page application. Consider the following code snippet.

describe('Search', function(){
  it('Should Open the search modal popup', function() {
     //code
     expect(modalOpened).toBe(true)
  })

  it('Search should return results', function(){

  })
  it('Search should not return results', function() {

  })
})

In the above the example if Should Open the search modal popup fails then specs below Should Open the search modal popup and Should Open the search modal popup will also fail, because the modal itself is not open. So there is no point in running the below specs. Can I conditionally run the last two specs? Like only if the first spec has passed, the below specs should run.

1

1 Answers

1
votes

It looks like you are getting Unit tests and E2E tests mixed up. Protractor, a subset of Selenium, is meant to run your Integration or End to End tests only. Each E2E spec should be integrating your units of code together (hence, Integration tests), and testing that your chosen browser allows each functionality to happen (e.g. click search and a modal shows with results). Moreover, you would NOT check for a property of modalOpened to be true, you WOULD check to ensure the modal was present in css, by className or other, along with your results.

Luckily, Protractor provides asynch Promise-based events. When you click on your search icon, you can do this:

it('Should Open the search modal popup and show results', function() {

    element(by.id('search')).click().then(
        function() {
             // now check for modal to be displayed

             // now check results are displayed
        }
    );
});

Sources:

https://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.click

https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.then