Add cucumber tests for the login

For this we had to add ids to some components in the frontend, and
put in feature files aswell as step definitions in Java
This commit is contained in:
Jonny 2019-06-05 11:05:42 +02:00
parent bba9f9ff5d
commit 07d10fcc55
4 changed files with 121 additions and 6 deletions

View File

@ -0,0 +1,91 @@
package re.turnie.seleniumtests.steps;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import re.turnie.seleniumtests.CucumberStepDefinition;
import re.turnie.seleniumtests.SystemConstants;
import java.util.List;
public class LoginStepDefinitions extends CucumberStepDefinition {
@Given("^User navigates to Login Page$")
public void goToLogin() {
checkDriverAvailability();
driver.navigate().to(SystemConstants.FRONTEND_URL + "/login");
}
@Given("^a user is registered as \"(.*)\" with email \"(.*)\" and password \"(.*)\"$")
public void assureRegistrationOfUser(String username, String email, String password) {
checkDriverAvailability();
}
@When("^I enter email \"(.*)\"$")
public void enterEmail(String email) {
checkDriverAvailability();
driver.findElement(By.id("email-input")).sendKeys(email);
}
@When("^I enter password \"(.*)\"$")
public void enterPassword(String password) {
checkDriverAvailability();
driver.findElement(By.id("password-input")).sendKeys(password);
}
@When("^I click on login$")
public void clickOnLogin() {
checkDriverAvailability();
driver.findElement(By.id("login-button")).click();
}
@Then("^Login should show error messages$")
public void checkErrorMessagesShowing() {
checkDriverAvailability();
WebElement errorlist = new WebDriverWait(driver, 1)
.until(ExpectedConditions.presenceOfElementLocated(By.id("error-list")));
List<WebElement> errortags = errorlist.findElements(By.tagName("li"));
if(errortags.isEmpty()) {
System.out.println("Test Failed");
} else {
System.out.println("Test Passed");
}
}
@Then("^the user \"(.*)\" should be logged in$")
public void checkForUserBeingLoggedIn(String username) {
checkDriverAvailability();
WebElement usernameButton = new WebDriverWait(driver, 1)
.until(ExpectedConditions.presenceOfElementLocated(By.id("profile-button")));
if(usernameButton != null && usernameButton.getText().equals(username)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
}
@Then("^the user should be redirected to index$")
public void checkForRedirectionToIndex() {
checkDriverAvailability();
String currentURL = driver.getCurrentUrl();
if(currentURL.equalsIgnoreCase(SystemConstants.FRONTEND_URL + "/")) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
}
}

View File

@ -0,0 +1,24 @@
Feature: Login
Background: User is on login page
Given User navigates to Login Page
Scenario: Logging in with wrong credentials will give error
When I enter email "unregistereduser@gmail.com"
And I enter password "asdf1234"
And I click on login
Then Login should show error messages
Scenario: User with correct credentials will be logged in
Given a user is registered as "GherkinTestUser" with email "gherkintestuser@gmail.com" and password "asdf1234"
When I enter email "gherkintestuser@gmail.com"
And I enter password "asdf1234"
And I click on login
Then the user "GherkinTestUser" should be logged in
Scenario: User with correct credentials will be redirected to index after login
Given a user is registered as "GherkinTestUser" with email "gherkintestuser@gmail.com" and password "asdf1234"
When I enter email "gherkintestuser@gmail.com"
And I enter password "asdf1234"
And I click on login
Then the user should be redirected to index

View File

@ -28,7 +28,7 @@ class LoginErrorList extends React.Component {
render() {
const {error, errorMessages} = this.props;
if (error) {
return (<ul className='mt-3 error-box'>
return (<ul id="error-list" className='mt-3 error-box'>
{errorMessages.map((message, index) => <li key={index}>
{message}
</li>)}
@ -81,15 +81,15 @@ class LoginForm extends React.Component {
return (<Form onSubmit={this.tryLogin.bind(this)}>
<FormGroup>
<Label for="username">E-Mail-Adresse</Label>
<Input type="email" name="username" value={this.state.email}
<Input id="email-input" type="email" name="username" value={this.state.email}
onChange={this.handleEmailInput.bind(this)}/>
</FormGroup>
<FormGroup>
<Label for="password">Passwort</Label>
<Input type="password" name="password" value={this.state.password}
<Input id="password-input" type="password" name="password" value={this.state.password}
onChange={this.handlePasswordInput.bind(this)}/>
</FormGroup>
<input type="submit" className="btn btn-lg btn-success w-100 shadow-sm" value="Anmelden"/>
<input id="login-button" type="submit" className="btn btn-lg btn-success w-100 shadow-sm" value="Anmelden"/>
<VisibleLoginErrorList/>
<LoginSuccessRedirect/>
</Form>);

View File

@ -68,9 +68,9 @@ class InvisibleLoginLogoutButtons extends React.Component {
if (isSignedIn) {
return (<ButtonGroup className="nav-item">
<Button outline color="success" href="/profile"
<Button id="profile-button" outline color="success" href="/profile"
className="navbar-btn my-2 my-sm-0 px-5">{username}</Button>
<Button outline color="success" onClick={this.logout.bind(this)}
<Button id="logout-button" outline color="success" onClick={this.logout.bind(this)}
className="navbar-btn my-2 my-sm-0 px-5">Logout</Button>
</ButtonGroup>);
} else {