Я хотел бы добиться этого и сохранить код как можно более кратким.
Каков шаг для достижения этого, но без for цикл?
Я попробовал метод Collections.singletonList(), но он не работает должным образом.
Ниже приведены мои объекты данных.
Код: Выделить всё
public class ProductDetails {
private String productName;
private String productModal;
private double productPrice;
private int productQuantity;
private double totalProductPrice;
}
Код: Выделить всё
public class ProductDetailsList {
List productDetailsList;
}
Код: Выделить всё
package Pages.shoppingCart;
import Base.BasePage;
import dataObjects.ProductDetails;
import dataObjects.ProductDetailsList;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.ArrayList;
import java.util.List;
public class ShoppingCartPage extends BasePage {
public ShoppingCartPage(WebDriver driver) {
super(driver);
}
@FindBy(css = "#content > form > div > table > tbody > tr")
private List products;
@FindBy(css = "#content > form > div > table > tbody > tr > td.text-left > a")
private List productNames;
@FindBy(css = "#content > form > div > table > tbody > tr > td.text-left:nth-child(3)")
private List modals;
@FindBy(xpath = "//input[contains(@name, 'quantity')]")
private List quantity;
@FindBy(xpath = "//input[contains(@name, 'quantity')]/parent::div/parent::td/following-sibling::td[1]")
private List unitPrices;
@FindBy(xpath = "//input[contains(@name, 'quantity')]/parent::div/parent::td/following-sibling::td[2]")
private List Total;
@FindBy(xpath = "//a[text()='Checkout']")
private WebElement checkoutButton;
public ProductDetailsList getProductDetails() {
ProductDetailsList shoppingCartPageProductDetailsList = new ProductDetailsList();
shoppingCartPageProductDetailsList.setProductDetailsList(new ArrayList());
for (int i = 0; i < products.size(); i++) {
String nameOfProduct = selenium.getText(productNames.get(i));
String productModal = selenium.getText(modals.get(i));
String unitQuantity = selenium.getElementAttributeValue(quantity.get(i), "value");
int productQuantity = Integer.parseInt(unitQuantity);
String price = selenium.getText(unitPrices.get(i));
String productPrice = price.replaceAll("[^\\d.]", "");
double convertedProductPrice = Double.parseDouble(productPrice);
String totalPriceOfProduct = selenium.getText(Total.get(i));
String totalPrice = totalPriceOfProduct.replaceAll("[^\\d.]", "");
double TotalProductPrice = Double.parseDouble(totalPrice);
ProductDetails shoppingCartPageProductDetails = new ProductDetails();
shoppingCartPageProductDetails.setProductName(nameOfProduct);
shoppingCartPageProductDetails.setProductModal(productModal);
shoppingCartPageProductDetails.setProductQuantity(productQuantity);
shoppingCartPageProductDetails.setProductPrice(convertedProductPrice);
shoppingCartPageProductDetails.setTotalProductPrice(TotalProductPrice);
shoppingCartPageProductDetailsList.getProductDetailsList().add(shoppingCartPageProductDetails);
}
return shoppingCartPageProductDetailsList;
}
public void clickOnCheckoutButton() throws InterruptedException {
selenium.clickOn(checkoutButton);
}
}
Код: Выделить всё
package stepDefinitions.orderConfirmation;
import Pages.accountSuccess.AccountSuccessPage;
import Pages.checkout.CheckoutPage;
import Pages.confirmOrder.ConfirmOrderPage;
import Pages.desktopAndMonitor.MonitorsPage;
import Pages.home.HeaderPage;
import Pages.home.TopCategoriesLeftSideBar;
import Pages.notificationPopup.ViewCartNotificationPopup;
import Pages.notificationPopup.WishListNotificationPopup;
import Pages.orderHistory.OrderHistoryPage;
import Pages.productQuickView.ProductQuickViewPopup;
import Pages.registration.RegistrationPage;
import Pages.shoppingCart.ShoppingCartPage;
import Pages.success.OrderSuccessPage;
import Pages.wishlist.MyWishListPage;
import dataFactory.CheckoutData;
import dataFactory.ProductData;
import dataFactory.RegistrationData;
import dataObjects.*;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
import utility.Constants;
import utility.DriverManager;
public class OrderConfirmationSteps {
RegistrationPage registrationPage = new RegistrationPage(DriverManager.getDriver());
AccountSuccessPage accountSuccessPage = new AccountSuccessPage(DriverManager.getDriver());
HeaderPage headerPage = new HeaderPage(DriverManager.getDriver());
TopCategoriesLeftSideBar topCategoriesLeftSideBar = new TopCategoriesLeftSideBar(DriverManager.getDriver());
MonitorsPage monitorsPage = new MonitorsPage(DriverManager.getDriver());
CheckoutPage checkoutPage = new CheckoutPage(DriverManager.getDriver());
ConfirmOrderPage confirmOrderPage = new ConfirmOrderPage(DriverManager.getDriver());
OrderSuccessPage orderSuccessPage = new OrderSuccessPage(DriverManager.getDriver());
OrderHistoryPage orderHistoryPage = new OrderHistoryPage(DriverManager.getDriver());
ShoppingCartPage shoppingCartPage = new ShoppingCartPage(DriverManager.getDriver());
ProductQuickViewPopup productQuickViewPopup = new ProductQuickViewPopup(DriverManager.getDriver());
ViewCartNotificationPopup viewCartNotificationPopup = new ViewCartNotificationPopup(DriverManager.getDriver());
WishListNotificationPopup wishListNotificationPopup = new WishListNotificationPopup(DriverManager.getDriver());
MyWishListPage myWishListPage = new MyWishListPage(DriverManager.getDriver());
RegistrationDetails registrationDetails;
CheckoutDetails checkoutDetails;
ConfirmOrderDetails confirmOrderPageDetails;
OrderHistoryDetails orderHistoryDetails;
ProductDetails monitorsPageProductDetails;
ProductDetails quickViewPopupProductDetails;
ProductDetailsList shoppingCartPageProductDetailsList;
ProductDetails shoppingCartPageProductDetails;
ProductDetails checkoutPageProductDetails;
ProductDetails confirmOrderPageProductDetails;
ProductDetails wishListNotificationPopupDetails;
ProductDetailsList wishListPageProductDetailsList;
ProductDetails wishListPageProductDetails;
@Given("The user is on the registration page")
public void the_user_is_on_the_registration_page() {
headerPage.clickOnOptionFromMyAccountDropdown("Register");
}
@When("The user registers successfully")
public void the_user_registers_successfully() throws InterruptedException {
registrationDetails = new RegistrationData().userRegistrationData();
registrationPage.fillRegistrationForm(registrationDetails);
registrationPage.clicksOnContinueButton();
}
@Then("The user should see a success message for account creation")
public void the_user_should_see_a_success_message_for_account_creation() {
Assert.assertEquals("The success message is not displayed.", accountSuccessPage.getSuccessMessage(), Constants.accountSuccessMessage);
}
@When("The user selects {string} category from the Shop by Category section")
public void the_user_selects_category_from_the_shop_by_category_section(String categoryName) throws InterruptedException {
headerPage.clickOnShopByCategoryOption();
topCategoriesLeftSideBar.clickOnCategoryOption(categoryName);
}
@Then("The user should navigate to the Monitors page")
public void the_user_should_navigate_to_the_monitors_page() {
Assert.assertEquals("Monitors page is not displayed"). Constants.monitorsPageTitle, DriverManager.getDriver().getTitle());
}
@When("The user clicks the Quick View button after selecting a product from the list")
public void the_user_clicks_the_quick_view_button_after_selecting_a_product_from_the_list() throws InterruptedException {
monitorsPageProductDetails = new ProductData().productData();
monitorsPage.selectProductAndClickOnQuickViewButton(monitorsPageProductDetails);
}
@Then("The user should verify the product details match between the Monitors page and the Quick View modal")
public void the_user_should_verify_the_product_details_match_between_the_monitors_page_and_the_quick_view_modal() {
quickViewPopupProductDetails = productQuickViewPopup.getProductData();
Assert.assertEquals("The product name on the Monitors page and Quick View page page doesn't same.", monitorsPageProductDetails.getProductName(), quickViewPopupProductDetails.getProductName());
Assert.assertEquals("The product price on the Monitors page and Quick View page page doesn't same.", monitorsPageProductDetails.getProductPrice(), quickViewPopupProductDetails.getProductPrice(), Constants.delta);
}
@When("The user increases the product quantity and adds the product to the cart")
public void the_user_increases_the_product_quantity_and_adds_the_product_to_the_cart() throws InterruptedException {
int randomNumber = productQuickViewPopup.generateRandomNumber();
quickViewPopupProductDetails.setProductQuantity(randomNumber);
quickViewPopupProductDetails.setTotalProductPrice(quickViewPopupProductDetails.getProductPrice() * randomNumber);
productQuickViewPopup.increaseTheProductQuantityByClickingOnPlusButton(randomNumber);
productQuickViewPopup.clickOnAddToCartButton();
}
@When("The user clicks the {string} button from the notification popup")
public void the_user_clicks_the_button_from_the_notification_popup(String buttonName) throws InterruptedException {
viewCartNotificationPopup.clickOnViewCartButton(buttonName);
}
@Then("The user should navigate to the Shopping Cart")
public void the_user_should_navigate_to_the_shopping_cart() {
Assert.assertEquals("Shopping cart page is not displayed.", Constants.shoppingCartTitle, DriverManager.getDriver().getTitle());
}
@And("The user should verify that the product details on the Quick View popup and in the Shopping cart are the same.")
public void the_user_should_verify_that_the_product_details_on_the_quick_view_popup_and_in_the_shopping_cart_are_the_same() {
shoppingCartPageProductDetailsList = shoppingCartPage.getProductDetails();
shoppingCartPageProductDetails = shoppingCartPageProductDetailsList.getProductDetailsList().get(0);
Assert.assertEquals("The product name on the Quick View page and the Shopping Cart page doesn't match.")quickViewPopupProductDetails.getProductName(), shoppingCartPageProductDetails.getProductName());
Assert.assertEquals("The product modal on the Quick View page and the Shopping Cart page don't match."), quickViewPopupProductDetails.getProductModal(), shoppingCartPageProductDetails.getProductModal());
Assert.assertEquals("The product quantity on the Quick View page and the Shopping Cart page isn't same."), quickViewPopupProductDetails.getProductQuantity(), shoppingCartPageProductDetails.getProductQuantity());
Assert.assertEquals("The product price on the Quick View page and the Shopping Cart page isn't same,", quickViewPopupProductDetails.getProductPrice(), shoppingCartPageProductDetails.getProductPrice(), Constants.delta);
Assert.assertEquals("The total product price on the Quick View page and the Shopping Cart page isn't same,", quickViewPopupProductDetails.getTotalProductPrice(), shoppingCartPageProductDetails.getTotalProductPrice(), Constants.delta);
}
}
/>Цель: я хочу сократить строку кода.
Утверждение должно быть выполнено в минимальной строке кода.
Подробнее здесь: https://stackoverflow.com/questions/789 ... of-objects