Last active
December 29, 2018 06:32
-
-
Save pragmatictesters/9b34785a80f6248d98b78565ccf47b79 to your computer and use it in GitHub Desktop.
Selenium WebDriver Cucumber Integration Example : Sample Feature File and Step Definition file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: User login feature | |
Sample Feature File with Data Driven Testing (DDT) | |
Scenario: Invalid user login with blank password | |
Given I have accessed the HRM login page | |
When I type username "Admin" | |
And I type password "" | |
And I click login button | |
Then I should see error message "Password cannot be empty" | |
And close the browser window | |
Scenario: Invalid user login with blank username | |
Given I have accessed the HRM login page | |
When I type username "" | |
And I type password "Ptl@#321" | |
And I click login button | |
Then I should see error message "Username cannot be empty" | |
And close the browser window | |
Scenario Outline: Test invalid user login scenarios | |
Given I have accessed the HRM login page | |
When I type username "<username>" #This is a comment | |
And I type password "<password>" | |
And I click login button | |
Then I should see error message "<expectedError>" | |
And close the browser window | |
Examples: | |
| username | password | expectedError | | |
| | Ptl@#321 | Username cannot be empty | | |
| | | Username cannot be empty | | |
| Admin | | Password cannot be empty | | |
| Admin | InvalidPW | Invalid credentials | | |
#This is a comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pragmatic.selenium.hrm.steps; | |
import com.github.javafaker.Faker; | |
import com.pragmatic.selenium.hrm.TestBase; | |
import com.pragmatic.selenium.hrm.pages.LoginPage; | |
import cucumber.api.java.en.And; | |
import cucumber.api.java.en.Given; | |
import cucumber.api.java.en.Then; | |
import cucumber.api.java.en.When; | |
import org.openqa.selenium.support.PageFactory; | |
import org.testng.Assert; | |
/** | |
* Sample step definition file | |
* | |
*/ | |
public class LoginSteps extends TestBase { | |
static LoginPage loginPage; | |
@When("^I type username \"([^\"]*)\"$") | |
public void iTypeUsername(String username) { | |
System.out.println("I have typed username " + username); | |
loginPage = loginPage.typeUsername(username); | |
} | |
@Given("^I have accessed the HRM login page$") | |
public void iHaveAccessedTheHRMLoginPage() { | |
initWebDrivers(); | |
faker = new Faker(); | |
TestBase.browser = "chrome"; | |
launchWebBrowser(); | |
setImplicitWait(); | |
maximizeBrowserWindow(); | |
navaigateToLoginPage(driver); | |
loginPage = PageFactory.initElements(driver, LoginPage.class); | |
} | |
@And("^I type password \"([^\"]*)\"$") | |
public void iTypePassword(String password) { | |
System.out.println("I have typed password" + password); | |
loginPage = loginPage.typePassword(password); | |
} | |
@Then("^I should see error message \"([^\"]*)\"$") | |
public void iShouldSeeErrorMessage(String expectedError) { | |
System.out.println("I can see error message " + expectedError); | |
Assert.assertEquals(loginPage.getError(), expectedError); | |
} | |
@And("^I click login button$") | |
public void iClickLoginButton() { | |
loginPage = loginPage.clickLoginFailure(); | |
} | |
@And("^close the browser window$") | |
public void closeTheBrowserWindow() { | |
driver.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment