Search code examples
seleniumselenium-webdrivercucumbergherkincucumber-java

How to execute only few random Examples for Scenario Outline?


Scenario Outline: Login to the FaceBook

Given Enter "<emailId>"

And Enter the "<password>"

When Click on Login button

Then Navigated to HomePage

Examples:

| emailId | password |

|abc@***.com| abcd |

|123@***.com| 1234 |

|@#$@***.com| !@#$ |

-> i want to execute the testcase with "123@***.com" only. How can we achieve this?

When i am putting 2 Examples, i am getting an compilation error "missing EOF at 'Examples:'" in the 2nd Examples.

Thanks in Advance.


Solution

  • You can control the execution of succeeding steps using a flag. Scenario Outline: Create

    Given Enter "<emailId>"
    And Enter Password "<password>"
    And Check "<runFlag>"
    When Click on Login button
    Then Navigated to HomePage
    
    Examples: 
      | emailId         | password | runFlag  |
      | abc@***.com     |     abcd | N        |
      | 123@***.com     |     1234 | Y        |
      | @#$@***.com     |     !@#$ | N        |
    

    Then create your own logic using the flag variable to control the execution flow and assertion of upcoming steps

    private static String flag;
    
        @Given("^Enter \"([^\"]*)\"$")
        public void enter(String username) throws Throwable {
            System.out.println("=============================================================");
            System.out.println("Username: " + username);
        }
    
        @Given("^Enter Password \"([^\"]*)\"$")
        public void enter_p(String password) throws Throwable {
            System.out.println("Password: " + password);
        }
    
        @Given("^Check \"([^\"]*)\"$")
        public void check(String runFlag) throws Throwable {
            System.out.println("Run Flag: " + runFlag);
            flag = runFlag;
        }
    
        @When("^Click on Login button$")
        public void click_on_Login_button() throws Throwable {
            if("Y".equalsIgnoreCase(flag)){
                System.out.println("Clicked Login");
            }else{
                System.out.println("Skipped: RunFlag=" + flag);
            }
        }
    
        @Then("^Navigated to HomePage$")
        public void navigated_to_HomePage() throws Throwable {
            if("Y".equalsIgnoreCase(flag)){
                System.out.println("Navigated to homepage");
            }else{
                System.out.println("Skipped: RunFlag=" + flag);
            }
        }