-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCartPage.java
More file actions
86 lines (69 loc) · 2.9 KB
/
Copy pathCartPage.java
File metadata and controls
86 lines (69 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.deepakkhatri.qa.pages;
import com.deepakkhatri.qa.data.Product;
import com.deepakkhatri.qa.pages.components.HeaderComponent;
import io.qameta.allure.Step;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.util.List;
public class CartPage extends BasePage {
private static final By ITEM = By.cssSelector("[data-test='inventory-item']");
private static final By ITEM_NAME = By.cssSelector("[data-test='inventory-item-name']");
private static final By ITEM_PRICE = By.cssSelector("[data-test='inventory-item-price']");
private static final By ITEM_QUANTITY = By.cssSelector("[data-test='item-quantity']");
private static final By CHECKOUT_BUTTON = By.cssSelector("[data-test='checkout']");
private static final By CONTINUE_SHOPPING = By.cssSelector("[data-test='continue-shopping']");
private final HeaderComponent header;
public CartPage(WebDriver driver) {
super(driver);
this.header = new HeaderComponent(driver);
}
@Override
protected By pageMarker() {
return CHECKOUT_BUTTON;
}
public HeaderComponent header() {
return header;
}
public int itemCount() {
return driver.findElements(ITEM).size();
}
public List<String> itemNames() {
return driver.findElements(ITEM_NAME).stream()
.map(WebElement::getText)
.map(String::trim)
.toList();
}
private WebElement lineItem(Product product) {
return allElements(ITEM).stream()
.filter(item -> item.getText().contains(product.name()))
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"Product not in the cart: " + product.name()));
}
public int quantityOf(Product product) {
return Integer.parseInt(lineItem(product).findElement(ITEM_QUANTITY).getText().trim());
}
public long priceOf(Product product) {
return parseMoneyToCents(lineItem(product).findElement(ITEM_PRICE).getText());
}
@Step("Remove {products} from the cart")
public CartPage remove(Product... products) {
for (Product product : products) {
By removeButton = By.cssSelector("[data-test='remove-" + product.slug() + "']");
// The line item disappearing is the proof the click landed.
clickUntil(removeButton,
ExpectedConditions.invisibilityOfElementLocated(removeButton));
}
return this;
}
@Step("Proceed to checkout")
public void proceedToCheckout() {
clickUntil(CHECKOUT_BUTTON, ExpectedConditions.urlContains("checkout-step-one"));
}
@Step("Continue shopping")
public void continueShopping() {
clickUntil(CONTINUE_SHOPPING, ExpectedConditions.urlContains("inventory"));
}
}