From 8aebbce54eaf9bc5e06566b591e0d048f952e7cd Mon Sep 17 00:00:00 2001 From: Liudmila Portnova Date: Wed, 13 Dec 2023 11:00:07 +0300 Subject: [PATCH] =?UTF-8?q?refactor=C3=90:=20add=20the=20BaseModel=20and?= =?UTF-8?q?=20the=20@FindBy=20annotation=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pineapple/model/FeedbackPage.java | 12 ++--- .../practice/pineapple/model/HomePage.java | 36 +++++-------- .../practice/pineapple/model/MainPage.java | 13 ++--- .../pineapple/model/base/BaseModel.java | 51 +++++++++++++++++++ .../pineapple/model/base/BasePage.java | 12 +---- .../pineapple/runner/order/BaseUtils.java | 2 + .../practice/pineapple/tests/HomeTests.java | 1 - 7 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 src/test/java/practice/pineapple/model/base/BaseModel.java diff --git a/src/test/java/practice/pineapple/model/FeedbackPage.java b/src/test/java/practice/pineapple/model/FeedbackPage.java index db7f2c1..cc94553 100644 --- a/src/test/java/practice/pineapple/model/FeedbackPage.java +++ b/src/test/java/practice/pineapple/model/FeedbackPage.java @@ -1,26 +1,22 @@ package practice.pineapple.model; -import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; import practice.pineapple.model.base.BasePage; public class FeedbackPage extends BasePage { - private final By HEDER_REVIEWS = By.xpath("//h1"); + @FindBy(xpath = "//h1") + private WebElement heder1; public FeedbackPage(WebDriver driver) { super(driver); } - public WebElement getHederReviews() { - - return getDriver().findElement(HEDER_REVIEWS); - } - public String getTextHederReviews() { - return getText(getHederReviews()); + return getText(heder1); } } diff --git a/src/test/java/practice/pineapple/model/HomePage.java b/src/test/java/practice/pineapple/model/HomePage.java index 4f2fdac..130cda0 100644 --- a/src/test/java/practice/pineapple/model/HomePage.java +++ b/src/test/java/practice/pineapple/model/HomePage.java @@ -3,6 +3,7 @@ import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; import practice.pineapple.model.base.BasePage; import java.util.ArrayList; @@ -10,16 +11,18 @@ public class HomePage extends BasePage { + @FindBy(xpath = "//h1") + private List allHeaders; + + @FindBy(xpath = "//section/a[@href='/reviews']/div/h6") + private WebElement linkToReviews; + private final By HEDER_ABOUT_THE_STUDIO = By.xpath( "//section[@class='_about_ze3bk_1']/div/h1" ); - private final By ALL_HEDERS = By.xpath("//h1"); - private final By LINK_TO_REVIEWS = By.xpath("//section/a[@href='/reviews']"); - public HomePage(WebDriver driver) { - // ни чего своего делать не нужно, так как драйвер,который нам передадут - // нужно передать в конструктор BasePage + super(driver); } @@ -28,18 +31,6 @@ public WebElement getHederAboutTheStudio() { return getDriver().findElement(HEDER_ABOUT_THE_STUDIO); } - public List getAllHeaders() { - - return getDriver().findElements(ALL_HEDERS); - } - - public WebElement getLinkToReviews() { - - WebElement link = getDriver().findElement(LINK_TO_REVIEWS); - - return link; - } - public String getTextHederAboutTheStudio() { return getText(getHederAboutTheStudio()); @@ -47,18 +38,17 @@ public String getTextHederAboutTheStudio() { public List getTextAllHeaders() { - List allHeaders = new ArrayList<>(); + List headers = new ArrayList<>(); - for (WebElement h1 : getAllHeaders()) { - allHeaders.add(h1.getText()); + for (WebElement h1 : allHeaders) { + headers.add(h1.getText()); } - return allHeaders; + return headers; } public FeedbackPage clickLinkToReviews() { - - getDriver().findElement(By.xpath("//section/a[@href='/reviews']/div/h6")).click(); + linkToReviews.click(); return new FeedbackPage(getDriver()); } diff --git a/src/test/java/practice/pineapple/model/MainPage.java b/src/test/java/practice/pineapple/model/MainPage.java index c664f83..56d6ead 100644 --- a/src/test/java/practice/pineapple/model/MainPage.java +++ b/src/test/java/practice/pineapple/model/MainPage.java @@ -1,27 +1,22 @@ package practice.pineapple.model; -import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; import practice.pineapple.model.base.BasePage; public class MainPage extends BasePage { - private final By MENU_REVIEWS = By.xpath("//nav/ul/li/a[@href='/reviews']"); + @FindBy(xpath = "//nav/ul/li/a[@href='/reviews']") + private WebElement menuReviews; public MainPage(WebDriver driver) { super(driver); } - public WebElement getMenuReviews() { - - return getDriver().findElement(MENU_REVIEWS); - } - public FeedbackPage clickMenuReviews() { - - getMenuReviews().click(); + menuReviews.click(); return new FeedbackPage(getDriver()); } diff --git a/src/test/java/practice/pineapple/model/base/BaseModel.java b/src/test/java/practice/pineapple/model/base/BaseModel.java new file mode 100644 index 0000000..e309366 --- /dev/null +++ b/src/test/java/practice/pineapple/model/base/BaseModel.java @@ -0,0 +1,51 @@ +package practice.pineapple.model.base; + + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; + +abstract class BaseModel { + + private WebDriverWait wait2; + private WebDriverWait wait5; + private WebDriverWait wait10; + + private final WebDriver driver; + + public BaseModel(WebDriver driver) { + this.driver = driver; + PageFactory.initElements(driver, this); + } + + protected WebDriver getDriver() { + + return driver; + } + + public WebDriverWait getWait2() { + if (wait2 == null) { + wait2 = new WebDriverWait(getDriver(), Duration.ofSeconds(2)); + } + + return wait2; + } + + public WebDriverWait getWait5() { + if (wait5 == null) { + wait5 = new WebDriverWait(getDriver(), Duration.ofSeconds(5)); + } + + return wait5; + } + + public WebDriverWait getWait10() { + if (wait10 == null) { + wait10 = new WebDriverWait(getDriver(), Duration.ofSeconds(10)); + } + + return wait10; + } +} diff --git a/src/test/java/practice/pineapple/model/base/BasePage.java b/src/test/java/practice/pineapple/model/base/BasePage.java index 6de7a4c..fd2a432 100644 --- a/src/test/java/practice/pineapple/model/base/BasePage.java +++ b/src/test/java/practice/pineapple/model/base/BasePage.java @@ -3,19 +3,11 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -public abstract class BasePage { - - private final WebDriver driver; +public abstract class BasePage extends BaseModel { public BasePage(WebDriver driver) { - this.driver = driver; - } - - //чтобы этот метод был доступен только наследникам - protected WebDriver getDriver() { - //будем использовать - return driver; + super(driver); } public String getText(WebElement element) { diff --git a/src/test/java/practice/pineapple/runner/order/BaseUtils.java b/src/test/java/practice/pineapple/runner/order/BaseUtils.java index 367f049..b0618fc 100644 --- a/src/test/java/practice/pineapple/runner/order/BaseUtils.java +++ b/src/test/java/practice/pineapple/runner/order/BaseUtils.java @@ -54,10 +54,12 @@ private static void initProperties() { } static Properties getProperties() { + return properties; } static boolean isServerRun() { + return System.getenv("CI_RUN") != null; } diff --git a/src/test/java/practice/pineapple/tests/HomeTests.java b/src/test/java/practice/pineapple/tests/HomeTests.java index ac26306..5f045bc 100644 --- a/src/test/java/practice/pineapple/tests/HomeTests.java +++ b/src/test/java/practice/pineapple/tests/HomeTests.java @@ -37,6 +37,5 @@ public void testAllHeadersOnThePage() { .getTextAllHeaders(); Assert.assertEquals(headersActualResult, headersExpectedResult); - } }