2

I am using Protractor and Jasmine to create end-to-end tests for a webpage. To test this initial page, I have abstracted made some common utility functions to create about 20 test cases for this particular page. An example of this is shown.

describe('User is on the page and', function () {
  beforeEach(function () {
    browser.get('http://test.test');
  });

  it('page title of the page should be blahblah', function () {
    expect(browser.getTitle()).toEqual('blahblah');
  });

  it('main header of the page should be test', function () {
    expect(element(by.css('#entry-header h1')).getText()).toEqual('test');
  });

  ...
}

The issue is I now have 6 other pages have extremely similar functionality. I don't like the thought of duplicating all these tests and maintaining them for each page, so I'm tempted to abstract out these test cases so that they could be reused across the different pages by passing in page specifics as parameters.

However, this also seems like a bad practice. Test cases are no longer descriptive, they are functions with passed parameters. The test cases also won't be as readable, as they would have to deal with some slightly different logic across the pages. Also, changes to the test cases might actually cause regressions for other pages.

Is there an accepted best practice or pattern for this use case?

  • The process you are describing is called refactoring. There are many good reasons to refactor anything, and you have hit on a few of them. Testing is inherently repetitive and exhaustive. Use the tools of the trade to reduce both. – BobDalgleish Sep 27 '16 at 16:54
  • You might be interested in the Page Object Pattern. Though this isn't the forum for software recommendations, I also wrote Jasmine Data Driven Tests that might provide some additional tools. – Greg Burghardt Feb 24 '17 at 13:12
1

Separate the things that vary from the things that don't.

It is bad to rewrite the same tests for multiple pages.

It is bad to try to write a test to fit every page.

What you need is a way to associate the test with the pages they are valid for that doesn't involve rewriting them.

That way if test A is valid on pages 1, 4, and 7 it's only tested on 1, 4, and 7. If many of the same tests are valid on 1, 4, and 7 consider aggregating the calls to those tests together. That doesn't mean you have to define them together on a page. It means call them together. That way A can be reused on 9 once 9 exists even if B doesn't work on 9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.