curiousToKnow
curiousToKnow

Reputation: 79

How to parametrise a test?

I am working on creating automation test using Geb, Spock , Groovy, Selenium and page object model. This is more of a navigation test which I am working on.

In my test, I have the exact same steps (given, when, then) I follow, but in each test I am navigating to a different page. Then I verify if I have landed on correct page.

Since the steps are same and only the pages are changing, I am wondering if there is a way to use 1 test script (def) and iterate it multiple times using different parameters and perform page verifications. I am not sure if its doable, but if it is, can someone help me out?

Here is my current Geb spec with multiple feature methods, basically all following the same steps:

class NavSpec extends someTest{

    def cleanupSpec() {}
    def setup() {}
    def cleanup() {}

   
    def "Test1"() {
        given:
        TestPage testPage = to TestPage
        when: "Do 1 thing"
        testPage.clickLink1()
        Test_Link1 test_Link1 = at Test_Link1
        def cpage = test_Link1.cpd()
        then: "You are on Page1"
        !cpage  || {at page1}
    }

     def "test2"() {
        given:
        TestPage testPage = to TestPage
        when: "Do 2 thing"
        testPage.clickLink2()
        Test_Link2 test_Link2 = at Test_Link2
        def cpage = test_Link2.cpd()
        then: "You are on Page2"
        !cpage  || {at page2}
    }
}

Any help would be really appreciated.

Upvotes: 0

Views: 50

Answers (1)

kriegaex
kriegaex

Reputation: 67317

I have answered several of your questions, and of course Stack Overflow is a place to ask and find answers. But it seems to me that you never really thoroughly read either of

Please try to reserve some time to do that, because all your questions are quite basic and answered in those valuable bodies of documentation. You will learn a lot, it is worth the effort. Thank you.

What you want is a parametrised Spock test, using either data tables or data pipes.

import spock.lang.Unroll

class TestNavigationInitialTestSpec extends LoginBaseTestSpec {
  @Unroll("test portal page #number")
  def "navigate to test portal pages"() {
    given: "starting at home page"
    to Test_HomePage

    when: "clicking on navigation link"
    page."$clickLink"()

    then: "we are on target page"
    at targetPage
    !page.crashPageDisplayed()

    where:
    number | clickLink    | targetPage
    1      | 'clickLink1' | Test_Link1
    2      | 'clickLink2' | Test_Link2
    3      | 'clickLink3' | Test_Link3
  }
}

Please note how I use the page variable, which is always bound in Geb specs and which points to the current page. At first, it points to the home page, then after at it is updated to point to the sub page you navigated to. That allows me to avoid checking at for the sub page twice, like you did in your sample code.

I also think it is unnecessary to close your browser at the end of the spec, assuming that you run multiple specs which can all use the same browser instance. Closing and reopening browsers slows down your tests.

BTW, please always try to provide a full MCVE when asking questions. In order to actually run the Geb spec above, I had to create 1 base spec, 4 dummy HTML pages and 4 Geb page classes, all matching your sample code. When I had that running, I refactored your 3 feature methods into 1. Next time, please publish that kind of stuff in your question. It is a bit tiresome for me to do your job, only to be able to answer your questions.

Upvotes: 1

Related Questions