Using DataTables isn't really preferred, but here's how to do it
Feature File
Feature: Deal data creation Scenario: Free CRM Create a new deal scenario #Using a Data Table Given user is already on Login Page When title of login page is Free CRM Then user enters username and 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 | test deal | 1000 | 50 | 10 | Then close the browser
Definition File
package stepDefinitions;
import java.util.List;
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 DealStepDefinition {
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){
List<List<String>> data = credentials.raw(); //raw gives list of strings, then pipe to a List.. of List<String>
driver.findElement(By.xpath("//input[@name='username']")).sendKeys(data.get(0).get(0));
driver.findElement(By.name("password")).sendKeys(data.get(0).get(1));
// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
}
@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){
List<List<String>> list = dealData.raw();
driver.findElement(By.id("title")).sendKeys(list.get(0).get(0));
driver.findElement(By.id("amount")).sendKeys(list.get(0).get(1));
driver.findElement(By.id("probability")).sendKeys(list.get(0).get(2));
driver.findElement(By.id("commission")).sendKeys(list.get(0).get(3));
driver.findElement(By.xpath("//input[@type='submit' and @value='Save']")).click();
}
@Then("^close the browser$")
public void close_the_browser(){
driver.quit();
}
}
TestRunner
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/deals.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 {
}
Output
Feature: Deal data creation
Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 23094
Only local connections are allowed.
Jun 12, 2018 7:24:32 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: Free CRM Create a new deal scenario # src/main/java/Features/deals.feature:3
#Using a Data Table
Given user is already on Login Page # DealStepDefinition.user_is_already_on_Login_Page()
When title of login page is Free CRM # DealStepDefinition.title_of_login_page_is_Free_CRM()
Then user enters username and password # DealStepDefinition.user_enters_username_and_password(DataTable)
Then user clicks on login button # DealStepDefinition.user_clicks_on_login_button()
Then user is on home page # DealStepDefinition.user_is_on_home_page()
Then user moves to new deal page # DealStepDefinition.user_moves_to_new_deal_page()
Then user enters contact details # DealStepDefinition.user_enters_contact_details(DataTable)
Then close the browser # DealStepDefinition.close_the_browser()
1 Scenarios (1 passed)
8 Steps (8 passed)
0m10.893s
- Log in to post comments
Tags
