From 22bad673dfc00df32d050974ebbb5de02a964a32 Mon Sep 17 00:00:00 2001 From: romanov73 Date: Fri, 16 Feb 2018 17:00:44 +0400 Subject: [PATCH] add test --- src/main/java/ru/ulstu/tis/Main.java | 1 + src/main/java/ru/ulstu/tis/SearchPage.java | 4 ++- src/main/resources/drivers/chromedriver | Bin src/test/java/YandexSearch.java | 39 ++++++++++++++------- src/test/java/utils/SystemUtils.java | 8 +++++ src/test/java/utils/TestingUtils.java | 16 +++++++++ 6 files changed, 54 insertions(+), 14 deletions(-) mode change 100644 => 100755 src/main/resources/drivers/chromedriver create mode 100644 src/test/java/utils/SystemUtils.java create mode 100644 src/test/java/utils/TestingUtils.java diff --git a/src/main/java/ru/ulstu/tis/Main.java b/src/main/java/ru/ulstu/tis/Main.java index ddb8d7b..9df29fd 100644 --- a/src/main/java/ru/ulstu/tis/Main.java +++ b/src/main/java/ru/ulstu/tis/Main.java @@ -21,6 +21,7 @@ public class Main { Main m = new Main(); WebDriver driver; System.setProperty(DRIVER_TYPE, m.getDriverExecutable()); + driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); diff --git a/src/main/java/ru/ulstu/tis/SearchPage.java b/src/main/java/ru/ulstu/tis/SearchPage.java index 07690d8..8310c7f 100644 --- a/src/main/java/ru/ulstu/tis/SearchPage.java +++ b/src/main/java/ru/ulstu/tis/SearchPage.java @@ -8,8 +8,10 @@ import org.openqa.selenium.support.FindBys; public class SearchPage { @FindBy(xpath = "//*[@id='text']") private WebElement inputField; + @FindBy(xpath="/html/body/table/tbody/tr[2]/td/form/div[2]/button") - private WebElement startSearchButton; + private WebElement startSearchButton; + @FindBys(@FindBy(xpath = "//div[@class='main__content']//a")) private List links; diff --git a/src/main/resources/drivers/chromedriver b/src/main/resources/drivers/chromedriver old mode 100644 new mode 100755 diff --git a/src/test/java/YandexSearch.java b/src/test/java/YandexSearch.java index ea6554d..584defb 100644 --- a/src/test/java/YandexSearch.java +++ b/src/test/java/YandexSearch.java @@ -3,38 +3,51 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; -import ru.ulstu.tis.Main; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.WebDriverWait; +import ru.ulstu.tis.SearchPage; +import utils.TestingUtils; import java.util.concurrent.TimeUnit; public class YandexSearch { - + private final static String APP_URL = "http://ya.ru"; private final static String DRIVER_TYPE = "webdriver.chrome.driver"; - private final static String DRIVER_LOCATION = "drivers/%s"; - private final static String WINDOWS_CHROME_DRIVER = "chromedriver.exe"; - private final static String LINUX_CHROME_DRIVER = "chromedriver"; - private static WebDriver driver; @BeforeAll public static void setup() { - System.setProperty(DRIVER_TYPE, getDriverExecutable()); + System.setProperty(DRIVER_TYPE, TestingUtils.getDriverExecutablePath()); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); } @Test - public void testGetFreeEmployeesNextUnits() { + public void testResultPageHeader() { + driver.get(APP_URL); + String searchString = "QA automation"; + + SearchPage page = PageFactory.initElements(driver, SearchPage.class); + page.setSearchString(searchString); + page.clickSubmitButton(); + + System.out.println("Page title is: " + driver.getTitle()); + + + (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { + public Boolean apply(WebDriver d) { + return d.getTitle().toLowerCase().startsWith("qa"); + } + }); + System.out.println("Page contains result? " + page.isResultContainsText(searchString)); + + System.out.println("Page title is: " + driver.getTitle()); } @AfterAll public static void quit() { - driver.quit(); } - - private static String getDriverExecutable() { - return YandexSearch.class.getClassLoader().getResource(String.format(DRIVER_LOCATION, WINDOWS_CHROME_DRIVER)).getFile(); - } } diff --git a/src/test/java/utils/SystemUtils.java b/src/test/java/utils/SystemUtils.java new file mode 100644 index 0000000..b4b8241 --- /dev/null +++ b/src/test/java/utils/SystemUtils.java @@ -0,0 +1,8 @@ +package utils; + +public class SystemUtils { + + public static boolean isWindows() { + return System.getProperty("os.name").toLowerCase().contains("windows"); + } +} diff --git a/src/test/java/utils/TestingUtils.java b/src/test/java/utils/TestingUtils.java new file mode 100644 index 0000000..a0505da --- /dev/null +++ b/src/test/java/utils/TestingUtils.java @@ -0,0 +1,16 @@ +package utils; + +import static utils.SystemUtils.isWindows; + +public class TestingUtils { + private final static String DRIVER_LOCATION = "drivers/%s"; + private final static String WINDOWS_CHROME_DRIVER = "chromedriver.exe"; + private final static String LINUX_CHROME_DRIVER = "chromedriver"; + + public static String getDriverExecutablePath() { + return TestingUtils.class.getClassLoader().getResource( + String.format(DRIVER_LOCATION, isWindows() + ? WINDOWS_CHROME_DRIVER + : LINUX_CHROME_DRIVER)).getFile(); + } +}