CucumberJVM: 07 - DataDriven with Maps

 


Feature

Feature: Deal data creation

Scenario: Free CRM Create a new deal scenario

    #Using a HashMap
    Given user is already on Login Page
    When title of login page is Free CRM
    Then user enters username and password
        | username | password |
        | naveenk | test@123 |
    Then user clicks on login button
    Then user is on home page
    Then user moves to new deal page
    Then user enters contact details
    #FORMAT = ctrl-shift-f
        | title      | amount | probability | commission |
        | test deal1 | 1000 | 50 | 10 |
        | test deal2 | 2000 | 60 | 20 |
        | test deal3 | 3000 | 70 | 30 |
    Then close the browser

 

 

 


Definitions

package stepDefinitions;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import cucumber.api.DataTable;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class DealStepWithMapDefinition {

        WebDriver driver;
        WebElement el;
        
        
        
        @Given("^user is already on Login Page$")
        public void user_is_already_on_Login_Page() {
            System.setProperty("webdriver.chrome.driver", "W:\\w\\java\\lib\\SeleniumWebDrivers\\chromedriver\\2.39\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.get("https://www.freecrm.com");
        }
        
        @When("^title of login page is Free CRM$")
        public void title_of_login_page_is_Free_CRM() {
            String title = driver.getTitle();
            System.out.println(title);
            Assert.assertEquals("Free CRM software in the cloud powers sales and customer service", title);
        }
        
        
//        @Then("^user enters \"([^\"]*)\" and \"([^\"]*)\"$")
//        public void user_enters_username_and_password(String username, String password) {
//            driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
//            driver.findElement(By.name("password")).sendKeys(password);
//        }    

        @Then("^user enters username and password$")
        public void user_enters_username_and_password(DataTable credentials){
//            Map<String, String> data = (Map<String, String>) credentials.asMaps(String.class, String.class); //map returns map of <String, String>, then pipe to Map<S,S>
            
            for(Map<String, String> data: credentials.asMaps(String.class, String.class)) {
                driver.findElement(By.xpath("//input[@name='username']")).sendKeys(data.get("username"));
                driver.findElement(By.name("password")).sendKeys(data.get("password"));                
            }
        }

        @Then("^user clicks on login button$")
        public void user_clicks_on_login_button() {
            //Xpath.click technique
            el = driver.findElement(By.xpath("//input[@type='submit']"));
            el.click();
            
            // might have to use JavaScript technique
//            el = driver.findElement(By.xpath("//input[@type='submit']"));
//            JavascriptExecutor js = (JavascriptExecutor)driver;
//            js.executeScript("arguments[0].click", el);
        }
        
        @And("^user is on home page$")
        public void user_is_on_home_page() {
            String title = driver.getTitle();
            System.out.println("HomePage title is :" + title);
            Assert.assertEquals("CRMPRO", title);
        }

        @Then("^user moves to new deal page$")
        public void user_moves_to_new_deal_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(.,'Deals')]"))).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 Deal')]"))).build().perform();

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

        }

        @Then("^user enters contact details$")
        public void user_enters_contact_details(DataTable dealData){
            
            for ( Map<String,String> data : dealData.asMaps(String.class, String.class)){
                driver.findElement(By.id("title")).sendKeys(data.get("title"));
                driver.findElement(By.id("amount")).sendKeys(data.get("amount"));
                driver.findElement(By.id("probability")).sendKeys(data.get("probability"));
                driver.findElement(By.id("commission")).sendKeys(data.get("commission"));
//                driver.findElement(By.xpath("//input[@type='submit' and @value='Save']")).click();
            }
        }

        @Then("^close the browser$")
        public void close_the_browser(){
            driver.quit();
        }
    }

Test Runner

package uc.atmtransaction.Runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/main/java/Features/dealsmap.feature"
        ,glue= {"stepDefinitions"}
        ,format= {"pretty","html:test-output","json:json-output/cucmber.json","junit:xml-output/cucumber.xml"}
        ,strict=false
        ,monochrome=true
        ,dryRun=false
        )

public class TestRunner {

}