CucumberJVM: 05 - More Logic with Examples:, driver.switchTo() and action.moveToElement()

Another feature file with more test data

Feature: Free CRM Create Contacts

Scenario Outline: Free CRM Login Test Scenario

Given user is already on Login Page
When title of login page is Free CRM
Then user enters "<username>" and "<password>"
Then user clicks on login button
Then user is on home page
Then user moves to new contact page
Then user enters contact details "<firstname>" and "<lastname>" and "<position>"
Then close the browser

Examples:
    | username | password | firstname | lastname | position |
    | naveenk  | test@123 | ucFirst1  | ucLast1  | manager  |
    | naveenk  | test@123 | ucFirst2  | ucLast2  | qa       |

Definitions implementation

 

    // adding for the contacts.feature sample
    @Then("^user moves to new contact page$")
    public void user_moves_to_new_contact_page(){
        //move to frame ( view frame source )

//        wasn't working
//        driver.switchTo().frame("manpanel");
        
        el = driver.findElement(By.xpath("//frame[@name='mainpanel']"));
        driver.switchTo().frame(el);
        
        //mouse over the menu item
        Actions action = new Actions(driver); //new action
        // findElement.... build action to MOVE to element....
        action.moveToElement(driver.findElement(By.xpath("//a[contains(.,'Contacts')]"))).build().perform();

//         wasn't working
//        driver.findElement(By.xpath("//a[contains(.,'New Contact']")).click();
        
        // THEN click
        action.moveToElement(driver.findElement(By.xpath("//a[contains(.,'New Contact')]"))).build().perform();

        el = driver.findElement(By.xpath("//a[contains(.,'New Contact')]"));
        el.click();
    }

    @Then("^user enters contact details \"([^\"]*)\" and \"([^\"]*)\" and \"([^\"]*)\"$")
    public void user_enters_contacts_details(String firstname, String lastname, String position){
        driver.findElement(By.id("first_name")).sendKeys(firstname);
        driver.findElement(By.id("surname")).sendKeys(lastname);
        driver.findElement(By.id("company_position")).sendKeys(position);
        driver.findElement(By.xpath("//input[@type='submit' and @value='Save']")).click();
    }   

TestRunner simply updated to the contacts.feature file(for demo)

@RunWith(Cucumber.class) //add Junit4+
@CucumberOptions(
        features =  "src/main/java/Features/contacts.feature"
        ,glue= {"stepDefinitions"}
        ,format= {"pretty","html:test-output", "json:json-output/cucumber.json","junit:junit-xml-output/cucumber.xml"}
        ,monochrome = true
        ,strict = true //true = fail test suite if ANY test step not defined
        ,dryRun = false //true = check feature-definition-runner setup is valid... NO test logic executed
        )

Output

Console

Feature: Free CRM Create Contacts

  Scenario Outline: Free CRM Login Test Scenario                                     # src/main/java/Features/contacts.feature:3
    Given user is already on Login Page
    When title of login page is Free CRM
    Then user enters "<username>" and "<password>"
    Then user clicks on login button
    Then user is on home page
    Then user moves to new contact page
    Then user enters contact details "<firstname>" and "<lastname>" and "<position>"
    Then close the browser

    Examples:
Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 10646
Only local connections are allowed.
Jun 12, 2018 5:44:18 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Free CRM software in the cloud powers sales and customer service
HomePage title is :CRMPRO

  Scenario Outline: Free CRM Login Test Scenario                            # src/main/java/Features/contacts.feature:17
    Given user is already on Login Page                                     # LoginStepDefinition.user_is_already_on_Login_Page()
    When title of login page is Free CRM                                    # LoginStepDefinition.title_of_login_page_is_Free_CRM()
    Then user enters "naveenk" and "test@123"                               # LoginStepDefinition.user_enters_username_and_password(String,String)
    Then user clicks on login button                                        # LoginStepDefinition.user_clicks_on_login_button()
    Then user is on home page                                               # LoginStepDefinition.user_is_on_home_page()
    Then user moves to new contact page                                     # LoginStepDefinition.user_moves_to_new_contact_page()
    Then user enters contact details "ucFirst1" and "ucLast1" and "manager" # LoginStepDefinition.user_enters_contacts_details(String,String,String)
    Then close the browser                                                  # LoginStepDefinition.close_the_browser()
Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 34648
Only local connections are allowed.
Jun 12, 2018 5:44:28 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Free CRM software in the cloud powers sales and customer service
HomePage title is :CRMPRO

  Scenario Outline: Free CRM Login Test Scenario                       # src/main/java/Features/contacts.feature:18
    Given user is already on Login Page                                # LoginStepDefinition.user_is_already_on_Login_Page()
    When title of login page is Free CRM                               # LoginStepDefinition.title_of_login_page_is_Free_CRM()
    Then user enters "naveenk" and "test@123"                          # LoginStepDefinition.user_enters_username_and_password(String,String)
    Then user clicks on login button                                   # LoginStepDefinition.user_clicks_on_login_button()
    Then user is on home page                                          # LoginStepDefinition.user_is_on_home_page()
    Then user moves to new contact page                                # LoginStepDefinition.user_moves_to_new_contact_page()
    Then user enters contact details "ucFirst2" and "ucLast2" and "qa" # LoginStepDefinition.user_enters_contacts_details(String,String,String)
    Then close the browser                                             # LoginStepDefinition.close_the_browser()

2 Scenarios (2 passed)
16 Steps (16 passed)
0m20.910s

 

Junit

Feature: Free CRM Create Contacts
 
Scenario Outline: Free CRM Login Test Scenario
 
  1. Given user is already on Login Page
  2. When title of login page is Free CRM
  3. Then user enters "<username>" and "<password>"
  4. Then user clicks on login button
  5. Then user is on home page
  6. Then user moves to new contact page
  7. Then user enters contact details "<firstname>" and "<lastname>" and "<position>"
  8. Then close the browser
Examples:
 
username password firstname lastname position
naveenk test@123 ucFirst1 ucLast1 manager
naveenk test@123 ucFirst2 ucLast2 qa
Scenario Outline: Free CRM Login Test Scenario
 
  1. Given user is already on Login Page
  2. When title of login page is Free CRM
  3. Then user enters "naveenk" and "test@123"
  4. Then user clicks on login button
  5. Then user is on home page
  6. Then user moves to new contact page
  7. Then user enters contact details "ucFirst1" and "ucLast1" and "manager"
  8. Then close the browser
Scenario Outline: Free CRM Login Test Scenario
 
  1. Given user is already on Login Page
  2. When title of login page is Free CRM
  3. Then user enters "naveenk" and "test@123"
  4. Then user clicks on login button
  5. Then user is on home page
  6. Then user moves to new contact page
  7. Then user enters contact details "ucFirst2" and "ucLast2" and "qa"
  8. Then close the browser