Skip to content

Commit eba656d

Browse files
Fix code style (#2)
* Init : [Improve code] * Done : [Fix Code Style]
1 parent 0860592 commit eba656d

File tree

18 files changed

+475
-204
lines changed

18 files changed

+475
-204
lines changed

README.md

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# Homework 06 | Celvine Adi Putra
22

3-
___
4-
5-
#### JDK Version
3+
### JDK Version
64

75
JDK : 20
86

9-
#### Dependency
7+
### Dependency
108

11-
| No | Group Id | Artifact Id | Version |
12-
|----|-------------------------|-------------------|---------|
13-
| 1 | org.seleniumhq.selenium | selenium-java | 4.10.0 |
14-
| 2 | org.junit.jupiter | junit-jupiter-api | 5.10.0 |
15-
| 3 | io.github.bonigarcia | webdrivermanager | 5.4.1 |
9+
| No | Group Id | Artifact Id | Version |
10+
|----|-------------------------|------------------------|---------|
11+
| 1 | org.seleniumhq.selenium | selenium-java | 4.10.0 |
12+
| 2 | org.junit.jupiter | junit-jupiter-api | 5.10.0 |
13+
| 3 | io.github.bonigarcia | webdrivermanager | 5.5.3 |
14+
| 4 | io.cucumber | cucumber-java | 7.14.0 |
15+
| 5 | io.cucumber | cucumber-junit | 7.14.0 |
16+
| 6 | org.junit.platform | junit-platform-console | 1.10.0 |
1617

1718
### Test Information
1819

@@ -30,3 +31,52 @@ https://www.saucedemo.com/
3031
| locked_out_user | secret_sauce |
3132
| problem_user | secret_sauce |
3233
| error_user | secret_sauce |
34+
35+
### Report
36+
37+
#### Login Feature
38+
39+
![](/home/celvine/Downloads/code/Tugas_06_CelvineAdiPutra/report/LoginReport.png)
40+
41+
```gherkin
42+
Feature: Login feature
43+
44+
Scenario Outline: User successfully logs in with valid credentials
45+
Given The user opens the web page or app
46+
When The user enters <username> as username
47+
And The user enters <password> as password
48+
And The user clicks the login button
49+
Then The user should be logged in successfully
50+
Examples:
51+
| username | password |
52+
| standard_user | secret_sauce |
53+
54+
Scenario Outline: User fails to log in with invalid credentials
55+
Given The user opens the web page or app
56+
When The user enters <username> as username
57+
And The user enters <password> as password
58+
And The user clicks the login button
59+
Then The user should see an authentication error message
60+
Examples:
61+
| username | password |
62+
| example | 123 |
63+
```
64+
65+
#### Add to Cart
66+
67+
![](/home/celvine/Downloads/code/Tugas_06_CelvineAdiPutra/report/AddToCartReport.png)
68+
69+
```gherkin
70+
Feature: Add Product to Cart
71+
72+
Background:
73+
Given The user is logged in
74+
75+
Scenario: User successfully adds a product to the cart
76+
When The user clicks the add to cart button
77+
Then The text on the add to cart button should change to remove
78+
Then The number of items in the cart icon should be 1
79+
When The user click the add to cart button other product
80+
Then The text on the add to cart button other product should change to remove
81+
Then The number of items in the cart icon should be 2
82+
```

report/AddToCartReport.png

98.4 KB
Loading

report/LoginReport.png

131 KB
Loading

src/test/java/config/Auth.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ public class Auth {
88

99
public Auth() {
1010
this.standardUser = new User("standard_user", "secret_sauce");
11-
this.fakeUser = new User("", "secret_sauce");
11+
this.fakeUser = new User("fake_user", "fake_password");
12+
}
13+
14+
public User getStandardUser() {
15+
return this.standardUser;
1216
}
1317

1418
public String getUserNameStandardUser() {

src/test/java/config/SetupDriver.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,42 @@
55
import org.openqa.selenium.chrome.ChromeDriver;
66

77
public class SetupDriver {
8-
protected final Config config;
9-
10-
public WebDriver webDriver;
8+
private final Config config;
9+
private WebDriver webDriver;
1110

1211
public SetupDriver() {
13-
this.config = new Config();
12+
config = new Config();
1413

15-
setUpTest();
14+
setChromeDriver();
15+
setMaxScreen();
16+
}
1617

18+
private void setChromeDriver() {
19+
WebDriverManager.chromedriver().setup();
1720
webDriver = new ChromeDriver();
21+
}
22+
23+
private void setMaxScreen() {
1824
webDriver.manage().window().maximize();
25+
}
1926

20-
openUrl();
27+
public void openDefaultApp() {
28+
webDriver.get(config.getBaseUrl());
2129
}
2230

23-
void setUpTest() {
24-
WebDriverManager.chromedriver().setup();
31+
public WebDriver getWebDriver() {
32+
return this.webDriver;
2533
}
2634

27-
void openUrl() {
28-
webDriver.get(this.config.getBaseUrl());
35+
public Config getConfig() {
36+
return config;
2937
}
3038

3139
public String getCurrentUrl() {
3240
return this.webDriver.getCurrentUrl();
3341
}
3442

35-
public String getCurrentTitle() {
36-
return this.webDriver.getTitle();
43+
public SetupDriver getSetUpDriver() {
44+
return this;
3745
}
3846
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package config.elements;
2+
3+
import config.utils.ConvertStringToKebabCase;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
8+
import java.util.List;
9+
10+
public class ElementFormCart {
11+
private final WebDriver webDriver;
12+
private WebElement cartList;
13+
private List<WebElement> itemInCartList;
14+
15+
private WebElement selectedItem;
16+
17+
private String productName;
18+
19+
public ElementFormCart(WebDriver webDriver) {
20+
this.webDriver = webDriver;
21+
}
22+
23+
public ElementFormCart cartList() {
24+
this.cartList = this.webDriver.findElement(By.cssSelector("#cart_contents_container"));
25+
return this;
26+
}
27+
28+
public ElementFormCart getItemInCartList() {
29+
this.itemInCartList = this.cartList.findElements(By.cssSelector(".cart_item"));
30+
return this;
31+
}
32+
33+
public ElementFormCart getIndexOfCartList(int index) {
34+
this.selectedItem = this.itemInCartList.get(index);
35+
return this;
36+
}
37+
38+
public int getSizeOfCart() {
39+
return this.itemInCartList.size();
40+
}
41+
42+
public String getItemName() {
43+
String productName = this.selectedItem.findElement(By.cssSelector(".cart_item_label>a>.inventory_item_name")).getText();
44+
this.productName = new ConvertStringToKebabCase(productName).convert();
45+
return this.productName;
46+
}
47+
48+
public void remove() {
49+
this.getItemName();
50+
this.webDriver.findElement(By.id("remove-" + this.productName)).click();
51+
}
52+
}

src/test/java/config/elements/ElementInventory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public ElementInventory(WebDriver webDriver) {
1111
this.webDriver = webDriver;
1212
}
1313

14+
public WebElement shoppingCartContainer() {
15+
return this.webDriver.findElement(By.cssSelector(""));
16+
}
17+
1418
public WebElement buttonAddToCart(String productName) {
1519
return this.webDriver.findElement(By.id("add-to-cart-" + productName));
1620
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package config.utils;
2+
3+
public class ConvertStringToKebabCase {
4+
private final String text;
5+
6+
public ConvertStringToKebabCase(String input) {
7+
input = input.toLowerCase();
8+
text = input.replaceAll(" ", "-");
9+
}
10+
11+
public String convert() {
12+
return text;
13+
}
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main;
2+
3+
import config.SetupDriver;
4+
import config.elements.ElementInventory;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.openqa.selenium.WebElement;
7+
8+
public class AddToProductCart {
9+
private final SetupDriver setupDriver;
10+
11+
private final ElementInventory elementInventory;
12+
13+
public AddToProductCart(SetupDriver setupDriver) {
14+
this.setupDriver = setupDriver;
15+
16+
this.elementInventory = new ElementInventory(this.setupDriver.getWebDriver());
17+
}
18+
19+
public void addToCartFirstProduct() {
20+
WebElement pSauceLabsBackpack = this.elementInventory.buttonAddToCart("sauce-labs-backpack");
21+
pSauceLabsBackpack.click();
22+
}
23+
24+
public void addToCartTextWillBeChangeToRemove() {
25+
WebElement pSauceLabsBackpack = this.elementInventory.buttonRemoveFromCart("sauce-labs-backpack");
26+
Assertions.assertTrue(pSauceLabsBackpack.isDisplayed());
27+
}
28+
29+
public void cartNumberWillBePlusOne() {
30+
WebElement shoppingCart = this.elementInventory.shoppingCart();
31+
Assertions.assertEquals("1", shoppingCart.getText());
32+
}
33+
34+
public void userClickAddToCartOtherProductButton() {
35+
WebElement pSauceLabsBikeLight = this.elementInventory.buttonAddToCart("sauce-labs-bike-light");
36+
pSauceLabsBikeLight.click();
37+
}
38+
39+
public void addToCartTextForOtherProductWillBeChangeToRemove() {
40+
WebElement pSauceLabsBikeLight = this.elementInventory.buttonRemoveFromCart("sauce-labs-bike-light");
41+
Assertions.assertTrue(pSauceLabsBikeLight.isDisplayed());
42+
}
43+
44+
public void cartNumberWillBePlusTwo() {
45+
WebElement shoppingCart = this.elementInventory.shoppingCart();
46+
Assertions.assertEquals("2", shoppingCart.getText());
47+
}
48+
49+
public void verifyFirstProductButton() {
50+
System.out.println("RUN ME TOO");
51+
}
52+
}

src/test/java/main/Cart.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main;
2+
3+
import config.SetupDriver;
4+
import config.elements.ElementFormCart;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class Cart {
8+
private final SetupDriver setupDriver;
9+
private final ElementFormCart elementFormCart;
10+
11+
public Cart(SetupDriver setupDriver) {
12+
this.setupDriver = setupDriver;
13+
this.elementFormCart = new ElementFormCart(this.setupDriver.getWebDriver());
14+
}
15+
16+
public void userNavigateToCartPage() {
17+
this.setupDriver.getWebDriver().navigate().to(this.setupDriver.getConfig().currentUrl("cart.html"));
18+
}
19+
20+
public void assertAtLeastXProductsInShoppingCart() {
21+
this.elementFormCart.cartList();
22+
23+
int size = this.elementFormCart.getItemInCartList().getSizeOfCart();
24+
25+
Assertions.assertTrue(size >= 2);
26+
}
27+
28+
public void clickRemoveButtonForFirstItem() {
29+
this.elementFormCart.getItemInCartList().getIndexOfCartList(0).remove();
30+
}
31+
32+
public void assertProductInShoppingCartIsX() {
33+
this.elementFormCart.cartList();
34+
35+
int size = this.elementFormCart.getItemInCartList().getSizeOfCart();
36+
37+
Assertions.assertTrue(size >= 1);
38+
}
39+
40+
public void assertShoppingCartIsEmpty() {
41+
this.elementFormCart.cartList();
42+
43+
int size = this.elementFormCart.getItemInCartList().getSizeOfCart();
44+
45+
Assertions.assertTrue(size == 0);
46+
}
47+
}

0 commit comments

Comments
 (0)