Testing templates in Protractor?

前端 未结 2 2101
栀梦
栀梦 2021-02-13 15:27

What\'s the best way to write assertions that should apply across every page in a site?

I\'m testing to see if an element exists in the footer of my site, so the element

2条回答
  •  情书的邮戳
    2021-02-13 15:36

    First of all, for writing cleaner tests and having a better understanding of what your target site consists of, apply the Page Object pattern and split the parts of your web pages into different page objects. For instance, footer, header can and should be separate page objects that would be reused across different web pages of your site.

    More on the topic:

    • Using Page Objects to Organize Tests
    • Protractor Page Objects (Tutorial, very detailed)
    • PageObject (Martin Fowler)
    • Using Page Objects to Overcome Protractor's Shortcomings

    As far as I understand the question, to follow the "DRY" principle you want to have some sort of the "shared" jasmine specs that you can define once and run in multiple test suites.

    This is exactly what DRYing up Jasmine Specs with Shared Behavior article is describing. The idea is rather simple - define a function with your test suites inside and call it from other test suites. Example:

    • create a function which accepts a context - page object - and contains the footer specific reusable tests:

      function testFooter(footer) {
      
          describe("(shared)", function () {
      
              describe("should show footer with necessary information", function () {
                  it("should show copyright", function () {
                      expect(footer.copyright.getText()).toEqual('Copyright 2014');
                  });
              });
      
          });
      }
      
    • call the function from other test suites passing the context - footer page object:

      var FooterPage = require('./footer.po.js');
      
      describe('Contacts page', function () {
          var scope = {};
      
          beforeEach(function () {
              browser.get('/contacts/');
              browser.waitForAngular();
              scope.page = new ContactsPage();
          });
      
          // other contacts page specific suites 
          // ...
      
          testFooter(new FooterPage());
      
      });
      

    You may need to adjust and improve the code to make it work, but the idea stays the same: define once and reuse. Passing around page objects just makes it a lot cleaner and transparent.

    Also see:

    • What's a good way to reuse test code using Jasmine?
    • Protractor Page Objects Inheritance

提交回复
热议问题