1

I am new to Specflow and need a way to reuse scenarios in multiple feature files.

I have a web application consisting of multiple pages that each share a many items such as footer content. Say my footer contains 3 links:

Help | Feedback | FAQ

I have a scenario to test the "Help" link functionality:

Scenario: Help link
    Given I am on page1
    When  I click on the link containing text "Help"
    Then  I see the help popup

As the "Given" statement specifies which page to open, the scenario is tied to that page. In order to test the same help link functionality on page2, page3, page4, etc., I would need to:

1) Copy the scenario into another feature file

2) Change the given statement to reference the correct page

3) Repeat for all pages containing the help link!

This seems like an awful lot of duplication and there must be a better way to do this.

David Talbot
  • 141
  • 9

2 Answers2

2

You can use a Scenario Outline, which is basically a data driven test:

Scenario Outline: Help link
    Given I am on <Page>
    When I click on the link containing text "Help"
    Then I see the help popup

Examples:
    | Page       |
    | Home       |
    | Contact Us |
    | About Us   |
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Thanks Greg, that is useful, but it would mean I would need to copy the examples block under each of my shared scenarios. I guess what I need is a background outline like this person suggested http://stackoverflow.com/questions/13141894/how-to-test-for-same-feature-with-multiple-backgrounds-in-cucumber – David Talbot Jan 13 '15 at 19:44
  • So you want to run an entire Feature against multiple pages? – Greg Burghardt Jan 13 '15 at 19:53
  • For all the common elements, yes. I could have separate feature files for header/footer/nav/etc. which are common to multiple pages. Why copy and paste for every page? – David Talbot Jan 13 '15 at 20:13
  • I see what you mean. There is a hole in Cucumber/SpecFlow for these cross cutting concerns regarding testing. Though to be honest, I don't see the point of testing the "help" link on a bunch of pages unless that help link is contextual. – Greg Burghardt Jan 13 '15 at 21:02
  • On the application I am working on, the common elements can be configured per page to have different look and feel. Potentially, one page could have a help link, and another could have a HELP link. I need to test the configuration has been done correctly. But if copy/paste is the only way, I'll have to just do it. – David Talbot Jan 14 '15 at 06:36
  • 2
    BoroDrummer If I were you I would make a feature file for testing the help link and put this Scenario Outline in it. Feature files don't have to be grouped by webpage. If it makes more sense to group the scenarios by functionality then go for it! Also for the sake of test execution time, don't be averse to having a test that checks the help page is in the correct place and then have another test that checks all of the help links point at the correct href. If there are a lot of pages that you're checking the link on then you could save a lot of time not having to open the page over and over. – alannichols Jan 14 '15 at 11:56
  • 1
    @alannichols, I like your thinking, but unfortunately it's not as simple as that. In this case, the help link creates an overlay window which is dynamically inserted HTML via javascript. I agree minimizing page loads though. – David Talbot Jan 14 '15 at 16:24
  • @BoroDrummer Ahh so you'd have to assert on something different for each page? – alannichols Jan 16 '15 at 10:49
1

You can't test everything when you work at this level. In fact you can only test a fraction of whats possible. So what you have to do is think about what benefit you get for testing the footer on each other page, and see if this is worth the cost of

  1. Writing the tests
  2. Running the tests

Secondly you don't need to repeat all the tests for each page. If the links work on one page then they will work on every other page, so you could test the links once, and then subsequently check that the footer appears on other pages e.g.

Feature: Footer checks

Scenario: Footer appears on pages
  Given a selection of pages
  When each page is visited
  Then each page should have the footer

and have your selection of pages be a random small sample from all the pages.

diabolist
  • 3,990
  • 1
  • 11
  • 15