Skip to content

Commit

Permalink
refactorÐ: add the BaseModel and the @findby annotation (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
08AlimDuiL authored Dec 13, 2023
1 parent 3a64662 commit 8aebbce
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
12 changes: 4 additions & 8 deletions src/test/java/practice/pineapple/model/FeedbackPage.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
36 changes: 13 additions & 23 deletions src/test/java/practice/pineapple/model/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
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;
import java.util.List;

public class HomePage extends BasePage {

@FindBy(xpath = "//h1")
private List<WebElement> 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);
}

Expand All @@ -28,37 +31,24 @@ public WebElement getHederAboutTheStudio() {
return getDriver().findElement(HEDER_ABOUT_THE_STUDIO);
}

public List<WebElement> getAllHeaders() {

return getDriver().findElements(ALL_HEDERS);
}

public WebElement getLinkToReviews() {

WebElement link = getDriver().findElement(LINK_TO_REVIEWS);

return link;
}

public String getTextHederAboutTheStudio() {

return getText(getHederAboutTheStudio());
}

public List<String> getTextAllHeaders() {

List<String> allHeaders = new ArrayList<>();
List<String> 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());
}
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/practice/pineapple/model/MainPage.java
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/practice/pineapple/model/base/BaseModel.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 2 additions & 10 deletions src/test/java/practice/pineapple/model/base/BasePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/practice/pineapple/runner/order/BaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ private static void initProperties() {
}

static Properties getProperties() {

return properties;
}

static boolean isServerRun() {

return System.getenv("CI_RUN") != null;
}

Expand Down
1 change: 0 additions & 1 deletion src/test/java/practice/pineapple/tests/HomeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ public void testAllHeadersOnThePage() {
.getTextAllHeaders();

Assert.assertEquals(headersActualResult, headersExpectedResult);

}
}

0 comments on commit 8aebbce

Please sign in to comment.