From c5e6827ccb35cbea19e12f0451a85544b42d9421 Mon Sep 17 00:00:00 2001 From: anlov Date: Mon, 15 Sep 2025 23:06:15 +0300 Subject: [PATCH] Lesson_15 --- proJect/pom.xml | 30 ++++++-- .../src/main/java/anlov/java/Attribute.java | 5 ++ .../main/java/anlov/java/CommonActions.java | 36 +++++++++ proJect/src/main/java/anlov/java/Config.java | 8 ++ .../src/main/java/anlov/java/Constants.java | 13 ++++ .../src/main/java/anlov/java/Factorial.java | 15 ---- proJect/src/main/java/anlov/java/Main.java | 2 +- .../java/anlov/java/pages/BaseElement.java | 24 ++++++ .../main/java/anlov/java/pages/BasePage.java | 34 ++++++++ .../java/anlov/java/pages/BasketPage.java | 65 ++++++++++++++++ .../main/java/anlov/java/pages/HomePage.java | 77 +++++++++++++++++++ proJect/src/test/java/BaseTest.java | 27 +++++++ proJect/src/test/java/BasketTest.java | 44 +++++++++++ proJect/src/test/java/FactorialTest.java | 34 -------- 14 files changed, 356 insertions(+), 58 deletions(-) create mode 100644 proJect/src/main/java/anlov/java/Attribute.java create mode 100644 proJect/src/main/java/anlov/java/CommonActions.java create mode 100644 proJect/src/main/java/anlov/java/Config.java create mode 100644 proJect/src/main/java/anlov/java/Constants.java delete mode 100644 proJect/src/main/java/anlov/java/Factorial.java create mode 100644 proJect/src/main/java/anlov/java/pages/BaseElement.java create mode 100644 proJect/src/main/java/anlov/java/pages/BasePage.java create mode 100644 proJect/src/main/java/anlov/java/pages/BasketPage.java create mode 100644 proJect/src/main/java/anlov/java/pages/HomePage.java create mode 100644 proJect/src/test/java/BaseTest.java create mode 100644 proJect/src/test/java/BasketTest.java delete mode 100644 proJect/src/test/java/FactorialTest.java diff --git a/proJect/pom.xml b/proJect/pom.xml index 8427783..30eac75 100644 --- a/proJect/pom.xml +++ b/proJect/pom.xml @@ -16,22 +16,36 @@ org.junit.jupiter junit-jupiter-api - 5.9.2 + 5.10.1 test + - org.junit.jupiter - junit-jupiter-engine - 5.9.2 - test + io.github.bonigarcia + webdrivermanager + LATEST - org.junit.jupiter - junit-jupiter-params - 5.9.2 + org.seleniumhq.selenium + selenium-java + LATEST + + + org.testng + testng + 7.9.0 test + + + org.testng + testng + 7.9.0 + compile + + + \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/Attribute.java b/proJect/src/main/java/anlov/java/Attribute.java new file mode 100644 index 0000000..54c340d --- /dev/null +++ b/proJect/src/main/java/anlov/java/Attribute.java @@ -0,0 +1,5 @@ +package anlov.java; + +public enum Attribute { + innerText, +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/CommonActions.java b/proJect/src/main/java/anlov/java/CommonActions.java new file mode 100644 index 0000000..399dd92 --- /dev/null +++ b/proJect/src/main/java/anlov/java/CommonActions.java @@ -0,0 +1,36 @@ +package anlov.java; + +import io.github.bonigarcia.wdm.WebDriverManager; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.edge.EdgeDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.testng.Assert; + +import java.time.Duration; + + +public class CommonActions { + public static WebDriver createDriver() { + WebDriver driver = null; + switch (Config.BROWSER) { + case "win_chrome": + WebDriverManager.chromedriver().setup(); + driver = new ChromeDriver(); + break; + case "win_firefox": + WebDriverManager.firefoxdriver().setup(); + driver = new FirefoxDriver(); + break; + case "win_edge": + WebDriverManager.edgedriver().setup(); + driver = new EdgeDriver(); + break; + default: + Assert.fail("Wrong browser's name:" + Config.BROWSER); + } + driver.manage().window().maximize(); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(Constants.TimeoutVariable.IMPLICIT_WAIT)); + return driver; + } +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/Config.java b/proJect/src/main/java/anlov/java/Config.java new file mode 100644 index 0000000..48aa0e5 --- /dev/null +++ b/proJect/src/main/java/anlov/java/Config.java @@ -0,0 +1,8 @@ +package anlov.java; + +public class Config { + + public static final String BROWSER = "win_chrome"; + public static final boolean CLEAR_COOKIES = true; + public static final boolean QUIT_BROWSER = true; +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/Constants.java b/proJect/src/main/java/anlov/java/Constants.java new file mode 100644 index 0000000..1822ca1 --- /dev/null +++ b/proJect/src/main/java/anlov/java/Constants.java @@ -0,0 +1,13 @@ +package anlov.java; + +public class Constants { + public static class TimeoutVariable { + public static final int IMPLICIT_WAIT = 5; + public static final int EXPLICIT_WAIT = 10; + } + + public static class UrlPage { + public static final String WILDBERRIES_HOME = "https://www.wildberries.ru/"; + } + +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/Factorial.java b/proJect/src/main/java/anlov/java/Factorial.java deleted file mode 100644 index 27a9dcf..0000000 --- a/proJect/src/main/java/anlov/java/Factorial.java +++ /dev/null @@ -1,15 +0,0 @@ -package anlov.java; - -public class Factorial { - - public static long getFactorial(int num) { - if (num < 0) { - throw new IllegalArgumentException("Число должно быть положительным"); - } - int factorial = 1; - for (int i = 1; i <= num; i++) { - factorial *= i; - } - return factorial; - } -} diff --git a/proJect/src/main/java/anlov/java/Main.java b/proJect/src/main/java/anlov/java/Main.java index c14deaa..cef3e3e 100644 --- a/proJect/src/main/java/anlov/java/Main.java +++ b/proJect/src/main/java/anlov/java/Main.java @@ -5,6 +5,6 @@ public class Main { public static void main(String[] args) { - System.out.println(Factorial.getFactorial(3)); + System.out.println(123); } } \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/pages/BaseElement.java b/proJect/src/main/java/anlov/java/pages/BaseElement.java new file mode 100644 index 0000000..1cf4afc --- /dev/null +++ b/proJect/src/main/java/anlov/java/pages/BaseElement.java @@ -0,0 +1,24 @@ +package anlov.java.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; + + +public class BaseElement { + + protected WebDriver driver; + + public BaseElement(WebDriver driver) { + this.driver = driver; + } + + public static class Basket { + private static final By basketLoc = By + .xpath("//div[@id='basketContent']/.//a[@href='/lk/basket']"); + } + + public BaseElement clickBasket() { + driver.findElement(Basket.basketLoc).click(); + return this; + } +} diff --git a/proJect/src/main/java/anlov/java/pages/BasePage.java b/proJect/src/main/java/anlov/java/pages/BasePage.java new file mode 100644 index 0000000..9a4b485 --- /dev/null +++ b/proJect/src/main/java/anlov/java/pages/BasePage.java @@ -0,0 +1,34 @@ +package anlov.java.pages; + +import anlov.java.Attribute; +import anlov.java.Constants; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; + +public class BasePage { + protected WebDriver driver; + + public BasePage(WebDriver driver) { + this.driver = driver; + } + + public void openPage(String url) { + driver.get(url); + } + + public WebElement waitElementVisible(WebElement element) { + new WebDriverWait(driver, Duration.ofSeconds(Constants.TimeoutVariable.IMPLICIT_WAIT)) + .until(ExpectedConditions.visibilityOf(element)); + return element; + } + + public WebElement waitAttribute(WebElement element, Attribute attribute, String str) { + new WebDriverWait(driver, Duration.ofSeconds(Constants.TimeoutVariable.EXPLICIT_WAIT)) + .until(ExpectedConditions.attributeToBe(element, attribute.toString(), str)); + return element; + } +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/pages/BasketPage.java b/proJect/src/main/java/anlov/java/pages/BasketPage.java new file mode 100644 index 0000000..cf9ee9c --- /dev/null +++ b/proJect/src/main/java/anlov/java/pages/BasketPage.java @@ -0,0 +1,65 @@ +package anlov.java.pages; + + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import java.util.List; +import java.util.stream.Collectors; + +public class BasketPage extends BasePage{ + private List goodsOfBucket; + private List pricesOfBucketGoods; + private static class Result { + private static final By summa = By.xpath("//span[text()='Итого']/following-sibling::span/span"); + } + + public BasketPage(WebDriver driver) { + super(driver); + } + + private static class Good { + private static final By goodName = By.xpath("//span[@class='good-info__good-name']"); + private static final By goodPrice = By.xpath("//div[@class='list-item__price-new wallet']"); + } + + public void nameOfGoods() { + goodsOfBucket = driver.findElements(Good.goodName).stream() + .map(WebElement::getText).collect(Collectors.toList()); + pricesOfBucketGoods = driver.findElements(Good.goodPrice).stream() + .map(p -> p.getText().split("₽")[0].replaceAll("\\s", "")) + .collect(Collectors.toList()); + } + + public double getGoodsPrice(int index) { + if (pricesOfBucketGoods == null || pricesOfBucketGoods.isEmpty()) { + throw new RuntimeException("Список цен в корзине пустой! Проверьте, что товары добавлены в корзину"); + } + if (index < 0 || index >= pricesOfBucketGoods.size()) { + throw new RuntimeException("Индекс " + index + " выходит за границы списка цен. Размер списка: " + pricesOfBucketGoods.size()); + } + return Double.parseDouble(pricesOfBucketGoods.get(index)); + } + + public int getCountOfGoods() { + return goodsOfBucket.size(); + } + + public String getGoodsName(int index) { + return goodsOfBucket.get(index); + } + + public double getSum() { + return Double.parseDouble(driver.findElement(Result.summa) + .getText().split("₽")[0].replaceAll("\\s", "")); + } + + public BasketPage wait(int millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return this; + } +} \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/pages/HomePage.java b/proJect/src/main/java/anlov/java/pages/HomePage.java new file mode 100644 index 0000000..b1a8d3a --- /dev/null +++ b/proJect/src/main/java/anlov/java/pages/HomePage.java @@ -0,0 +1,77 @@ +package anlov.java.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.TimeoutException; +import org.openqa.selenium.WebDriver; +import anlov.java.Attribute; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; + +public class HomePage extends BasePage { + private List selectGoods; + private List selectPrices; + + public HomePage(WebDriver driver) { + super(driver); + } + + private static class Goods { + private static final By lowerPrice = By.xpath("//span/ins"); + private static final By productName = By.xpath("//span[@class ='product-card__name']"); + private static final By addToBasket = By.xpath("//article//a[@href='/lk/basket']"); + } + + private static class Size { + private static final By checkSize = By.xpath("//li/label"); + } + + public HomePage addToBasket(int num) { + List productNames = driver.findElements(Goods.productName); + selectGoods = productNames.stream().limit(num).map(p -> p.getAttribute(Attribute.innerText.toString()) + .split("/")[1].trim()).collect(Collectors.toList()); + List lowerPrices = driver.findElements(Goods.lowerPrice); + selectPrices = lowerPrices.stream().limit(num).map(p -> p.getText().split("₽")[0] + .replaceAll("\\s", "")).collect(Collectors.toList()); + + driver.findElements(Goods.addToBasket).stream().limit(num).forEach(this::checkForSize); + return this; + } + + private void checkForSize(WebElement element) { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); + element.click(); + try { + WebElement sizeElement = wait.until( + ExpectedConditions.elementToBeClickable(Size.checkSize) + ); + sizeElement.click(); + } catch (TimeoutException e) { + System.out.println("Окно выбора размера не появилось, товар добавлен без выбора размера"); + } + catch (NoSuchElementException ignored) { + System.out.println("Элемент размера не найден"); + } + } + + public Double getSelectedGoodsPrice(int index) { + return Double.parseDouble(selectPrices.get(index)); + } + + public double getSumOfGoods() { + return selectPrices.stream().mapToDouble(Double::parseDouble).sum(); + } + + public int getCountOfGoods() { + return selectGoods.size(); + } + + public String getSelectedGoodsName(int index) { + return selectGoods.get(index); + } +} \ No newline at end of file diff --git a/proJect/src/test/java/BaseTest.java b/proJect/src/test/java/BaseTest.java new file mode 100644 index 0000000..af660e2 --- /dev/null +++ b/proJect/src/test/java/BaseTest.java @@ -0,0 +1,27 @@ +import anlov.java.CommonActions; +import anlov.java.Config; +import anlov.java.pages.BaseElement; +import anlov.java.pages.BasePage; +import anlov.java.pages.BasketPage; +import anlov.java.pages.HomePage; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import org.testng.annotations.AfterSuite; +import org.testng.asserts.SoftAssert; + +public class BaseTest { + protected static WebDriver driver = CommonActions.createDriver(); + protected BasePage basePage = PageFactory.initElements(driver, BasePage.class); + protected HomePage homePage = PageFactory.initElements(driver, HomePage.class); + protected BasketPage basketPage = PageFactory.initElements(driver, BasketPage.class); + protected BaseElement baseElement = PageFactory.initElements(driver, BaseElement.class); + + protected SoftAssert softAssert = new SoftAssert(); + + @AfterSuite(alwaysRun = true) + public void quit() { + if (Config.QUIT_BROWSER) { + driver.quit(); + } + } +} \ No newline at end of file diff --git a/proJect/src/test/java/BasketTest.java b/proJect/src/test/java/BasketTest.java new file mode 100644 index 0000000..4ea943c --- /dev/null +++ b/proJect/src/test/java/BasketTest.java @@ -0,0 +1,44 @@ +import jdk.jfr.Description; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.TimeoutException; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.testng.Assert; +import java.time.Duration; + +import static anlov.java.Constants.UrlPage.WILDBERRIES_HOME; + +public class BasketTest extends BaseTest { + @Test + @Description("Проверка соответствия товаров в корзине") + public void checkBasket() { + final int COUNT = 3; + basePage.openPage(WILDBERRIES_HOME); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + + try { + WebElement cookieAcceptButton = wait.until( + ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='cookies']//button[contains(text(), 'Окей')]")) + ); + cookieAcceptButton.click(); + } catch (TimeoutException e) { + System.out.println("Cookies banner not found or already closed"); + } + homePage.addToBasket(COUNT); + baseElement.clickBasket(); + basketPage.wait(1500).nameOfGoods(); + + Assert.assertEquals(basketPage.getCountOfGoods(), + homePage.getCountOfGoods(),"It's the difference goods"); + for (int i = 0; i < COUNT; i++) { + softAssert.assertEquals(basketPage.getGoodsName(COUNT - i - 1), + homePage.getSelectedGoodsName(i), "It's the difference names"); + softAssert.assertEquals((basketPage.getGoodsPrice(COUNT - i - 1)), + homePage.getSelectedGoodsPrice(i), "It's the difference prices"); + } + softAssert.assertEquals(basketPage.getSum(), + homePage.getSumOfGoods(), "It's the difference sums"); + } +} \ No newline at end of file diff --git a/proJect/src/test/java/FactorialTest.java b/proJect/src/test/java/FactorialTest.java deleted file mode 100644 index 618d24f..0000000 --- a/proJect/src/test/java/FactorialTest.java +++ /dev/null @@ -1,34 +0,0 @@ -import anlov.java.Factorial; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - - -class FactorialTest { - - @DisplayName("Check factorial if num equals 0") - @Test - void checkFactorial() { - assertEquals(1, Factorial.getFactorial(0)); - } - - @DisplayName("Check factorial if num more 0") - @ParameterizedTest - @ValueSource(ints = {1, 2, 3, 4}) - void checkMoreZero(int num) { - long[] numbers = {1, 2, 6, 24}; - assertEquals(numbers[num - 1], Factorial.getFactorial(num)); - - } - - @Test - void negativeInputShouldThrowException() { - assertThrows(IllegalArgumentException.class, - () -> Factorial.getFactorial(-5), "Число должно быть больше нуля"); - } -}