Skip to content

Commit 0860592

Browse files
Update : [Remove duplicate code]
message : 1. Add user to model 2. Remove login state duplicate action
1 parent b90cde0 commit 0860592

File tree

9 files changed

+111
-45
lines changed

9 files changed

+111
-45
lines changed

src/test/java/config/Auth.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package config;
2+
3+
import config.models.User;
4+
5+
public class Auth {
6+
private final User standardUser;
7+
private final User fakeUser;
8+
9+
public Auth() {
10+
this.standardUser = new User("standard_user", "secret_sauce");
11+
this.fakeUser = new User("", "secret_sauce");
12+
}
13+
14+
public String getUserNameStandardUser() {
15+
return this.standardUser.getUserName();
16+
}
17+
18+
public String getPasswordStandardUser() {
19+
return this.standardUser.getPassword();
20+
}
21+
22+
public String getUserNameFakeUser() {
23+
return this.fakeUser.getUserName();
24+
}
25+
26+
public String getPasswordFakeUser() {
27+
return this.fakeUser.getPassword();
28+
}
29+
}

src/test/java/config/Config.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
public class Config {
44
private final String baseUrl = "https://www.saucedemo.com/";
5+
public final Auth auth;
6+
7+
public Config() {
8+
this.auth = new Auth();
9+
}
510

611
public String getBaseUrl() {
712
return this.baseUrl;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config.models;
2+
3+
public class User {
4+
private final String userName, password;
5+
6+
public User(String userName, String password) {
7+
this.userName = userName;
8+
this.password = password;
9+
}
10+
11+
public String getUserName() {
12+
return userName;
13+
}
14+
15+
public String getPassword() {
16+
return password;
17+
}
18+
}

src/test/java/resources/features/addToCart.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Feature: Add Product to Cart
22

33
Background:
4-
Given The user is logged in
4+
Given The user should be logged in
55

66
Scenario: User successfully adds a product to the cart
77
When The user clicks the add to cart button
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
Feature: Login feature
22

3-
Scenario Outline: User successfully logs in with valid credentials
3+
Scenario: User successfully logs in with valid credentials
44
Given The user opens the web page or app
5-
When The user enters <username> as username
6-
And The user enters <password> as password
5+
When The user enters valid as username
6+
And The user enters valid as password
77
And The user clicks the login button
88
Then The user should be logged in successfully
99

10-
Examples:
11-
| username | password |
12-
| standard_user | secret_sauce |
13-
14-
Scenario Outline: User fails to log in with invalid credentials
10+
Scenario: User failed logs in with valid credentials
1511
Given The user opens the web page or app
16-
When The user enters <username> as username
17-
And The user enters <password> as password
12+
When The user enters invalid as username
13+
And The user enters invalid as password
1814
And The user clicks the login button
19-
Then The user should see an authentication error message
20-
21-
Examples:
22-
| username | password |
23-
| example | 123 |
15+
Then The user should be see error message

src/test/java/runner/RunAddToCart.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@CucumberOptions(
99
features = "src/test/java/resources/features",
1010
glue = "stepDef",
11-
plugin = {"html:target/Login_report.html"}
11+
plugin = {"html:target/ReportTesting.html"}
1212
)
13-
public class RunLogin {
13+
public class Runner {
1414
}

src/test/java/stepDef/Login.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import config.SetupDriver;
44
import config.elements.ElementFormLogin;
5-
import io.cucumber.java.After;
65
import io.cucumber.java.en.And;
76
import io.cucumber.java.en.Given;
87
import io.cucumber.java.en.Then;
@@ -25,16 +24,28 @@ public void userAlreadyOpenApp() {
2524
assertEquals("Swag Labs", currentTitle);
2625
}
2726

28-
@When("The user enters (.*) as username$")
29-
public void userInputUserName(String username) {
27+
@When("The user enters valid as username")
28+
public void userInputUserName() {
3029
WebElement userNameField = this.elementFormLogin.userName();
31-
userNameField.sendKeys(username);
30+
userNameField.sendKeys(this.config.auth.getUserNameStandardUser());
3231
}
3332

34-
@And("The user enters (.*) as password$")
35-
public void userInputPassword(String password) {
33+
@And("The user enters valid as password")
34+
public void userInputPassword() {
3635
WebElement passwordElement = this.elementFormLogin.password();
37-
passwordElement.sendKeys(password);
36+
passwordElement.sendKeys(this.config.auth.getPasswordStandardUser());
37+
}
38+
39+
@When("The user enters invalid as username")
40+
public void userInputInValidUserName() {
41+
WebElement userNameField = this.elementFormLogin.userName();
42+
userNameField.sendKeys(this.config.auth.getUserNameFakeUser());
43+
}
44+
45+
@And("The user enters invalid as password")
46+
public void userInputInValidPassword() {
47+
WebElement passwordElement = this.elementFormLogin.password();
48+
passwordElement.sendKeys(this.config.auth.getPasswordFakeUser());
3849
}
3950

4051
@And("The user clicks the login button")
@@ -49,15 +60,15 @@ public void userSuccessfullyLogin() {
4960
Assertions.assertEquals(this.config.currentUrl("inventory.html"), currentUrl);
5061
}
5162

52-
@Then("The user should see an authentication error message")
53-
public void userShouldSeeAnAuthenticationErrorMessage() {
63+
@Then("The user should be see error message")
64+
public void userFailedLogin() {
5465
int errorAlert = this.elementFormLogin.sizeErrorAlert();
5566

5667
assertEquals(1, errorAlert);
5768
}
5869

59-
@After(order = 0)
60-
public void closeBrowser() {
61-
webDriver.close();
62-
}
70+
// @After(order = 0)
71+
// public void closeBrowser() {
72+
// webDriver.close();
73+
// }
6374
}

src/test/java/stepDef/State.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package stepDef;
2+
3+
import config.SetupDriver;
4+
import config.elements.ElementFormLogin;
5+
import io.cucumber.java.en.Given;
6+
import org.openqa.selenium.WebElement;
7+
8+
public class State extends SetupDriver {
9+
private final ElementFormLogin elementFormLogin;
10+
11+
public State() {
12+
this.elementFormLogin = new ElementFormLogin(super.webDriver);
13+
}
14+
15+
@Given("The user should be logged in")
16+
public void login() {
17+
WebElement userNameField = this.elementFormLogin.userName();
18+
WebElement passwordField = this.elementFormLogin.password();
19+
20+
userNameField.sendKeys(this.config.auth.getUserNameStandardUser());
21+
passwordField.sendKeys(this.config.auth.getPasswordStandardUser());
22+
23+
this.elementFormLogin.loginButton().click();
24+
}
25+
}

0 commit comments

Comments
 (0)