Basic Framework consists of
- Feature file ( gherkin business logic
- Definition file ( test logic )
- Test Runner ( invoking class )
Feature: /src/main/java/Features/login.feature
Feature: Free CRM Login Feature Scenario: 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 And user is on the home page
Definitions: /src/main/java/stepDefinitions/LoginStepDefinition.java
package stepDefinitions; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; 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 LoginStepDefinition { 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 username and password$") public void user_enters_username_and_password() { driver.findElement(By.xpath("//input[@name='username']")).sendKeys("naveenk"); driver.findElement(By.name("password")).sendKeys("test@123"); } @Then("^user clicks on login button$") public void user_clicks_on_login_button() { //Xpath.click technique WebElement loginBtn = driver.findElement(By.xpath("//input[@type='submit']")); loginBtn.click(); // might have to use JavaScript technique // WebElement loginBtn = driver.findElement(By.xpath("//input[@type='submit']")); // JavascriptExecutor js = (JavascriptExecutor)driver; // js.executeScript("arguments[0].click", loginBtn); } @And("^user is on the home page$") public void user_is_on_the_home_page() { String title = driver.getTitle(); System.out.println("HomePage title is :" + title); Assert.assertEquals("CRMPRO", title); driver.quit(); } }
Runner: /src/main/java/Runner/TestRunner.java
package Runner; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) //add Junit4+ @CucumberOptions( features = "src/main/java/Features/login.feature" ,glue= {"stepDefinitions"} ) public class TestRunner { }
TestRunner @CucumberOptions: format=
- Console output is not user friendly with basic options
@RunWith(Cucumber.class) //add Junit4+ @CucumberOptions( features = "src/main/java/Features/login.feature" ,glue= {"stepDefinitions"} //,format= {"pretty","html:test-output"} )
- If 5th test fails:
Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 25812 Only local connections are allowed. Jun 12, 2018 1:45:43 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 Failed scenarios: src/main/java/Features/login.feature:3 # Scenario: Free CRM Login Test Scenario 1 Scenarios (1 failed) 5 Steps (1 failed, 4 passed) 0m7.507s org.junit.ComparisonFailure: expected:<CRMPRO[123]> but was:<CRMPRO[]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at stepDefinitions.LoginStepDefinition.user_is_on_the_home_page(LoginStepDefinition.java:56) at ?.And user is on the home page(src/main/java/Features/login.feature:9)
- Using format = option, console output is formatted in a more digestable way
@RunWith(Cucumber.class) //add Junit4+ @CucumberOptions( features = "src/main/java/Features/login.feature" ,glue= {"stepDefinitions"} ,format= {"pretty","html:test-output"} )
Feature: Free CRM Login Feature Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 6248 Only local connections are allowed. Jun 12, 2018 1:42:53 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 Login Test Scenario # src/main/java/Features/login.feature:3 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 username and password # LoginStepDefinition.user_enters_username_and_password() Then user clicks on login button # LoginStepDefinition.user_clicks_on_login_button() And user is on the home page # LoginStepDefinition.user_is_on_the_home_page() org.junit.ComparisonFailure: expected:<CRMPRO[123]> but was:<CRMPRO[]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at stepDefinitions.LoginStepDefinition.user_is_on_the_home_page(LoginStepDefinition.java:56) at ?.And user is on the home page(src/main/java/Features/login.feature:9)
junit creates a report in the "/test-output" folder
Feature: Free CRM Login Feature
Scenario: 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
- And user is on the home page
org.junit.ComparisonFailure: expected:<CRMPRO[123]> but was:<CRMPRO[]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at stepDefinitions.LoginStepDefinition.user_is_on_the_home_page(LoginStepDefinition.java:56) at ✽.And user is on the home page(src/main/java/Features/login.feature:9)
- Log in to post comments
Tags