add firefox driver
This commit is contained in:
parent
22bad673df
commit
dce6fd4ccd
@ -1,54 +1,7 @@
|
||||
package ru.ulstu.tis;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
import org.openqa.selenium.support.ui.ExpectedCondition;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
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";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Main m = new Main();
|
||||
WebDriver driver;
|
||||
System.setProperty(DRIVER_TYPE, m.getDriverExecutable());
|
||||
|
||||
driver = new ChromeDriver();
|
||||
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
|
||||
|
||||
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<Boolean>() {
|
||||
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());
|
||||
|
||||
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
private String getDriverExecutable() {
|
||||
return Main.class.getClassLoader().getResource(String.format(DRIVER_LOCATION, WINDOWS_CHROME_DRIVER)).getFile();
|
||||
}
|
||||
}
|
||||
|
BIN
src/main/resources/drivers/geckodriver
Normal file
BIN
src/main/resources/drivers/geckodriver
Normal file
Binary file not shown.
BIN
src/main/resources/drivers/geckodriver.exe
Normal file
BIN
src/main/resources/drivers/geckodriver.exe
Normal file
Binary file not shown.
@ -1,53 +1,51 @@
|
||||
import context.ChromeContext;
|
||||
import context.Context;
|
||||
import context.FirefoxContext;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
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 static WebDriver driver;
|
||||
|
||||
private static Context context;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
System.setProperty(DRIVER_TYPE, TestingUtils.getDriverExecutablePath());
|
||||
driver = new ChromeDriver();
|
||||
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
|
||||
context = new FirefoxContext();
|
||||
context.start();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void quit() {
|
||||
context.close();;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResultPageHeader() {
|
||||
driver.get(APP_URL);
|
||||
context.getDriver().get(APP_URL);
|
||||
String searchString = "QA automation";
|
||||
|
||||
SearchPage page = PageFactory.initElements(driver, SearchPage.class);
|
||||
SearchPage page = PageFactory.initElements(context.getDriver(), SearchPage.class);
|
||||
page.setSearchString(searchString);
|
||||
page.clickSubmitButton();
|
||||
|
||||
System.out.println("Page title is: " + driver.getTitle());
|
||||
System.out.println("Page title is: " + context.getDriver().getTitle());
|
||||
|
||||
|
||||
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
|
||||
(new WebDriverWait(context.getDriver(), 10)).until(new ExpectedCondition<Boolean>() {
|
||||
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();
|
||||
System.out.println("Page title is: " + context.getDriver().getTitle());
|
||||
}
|
||||
}
|
||||
|
24
src/test/java/context/ChromeContext.java
Normal file
24
src/test/java/context/ChromeContext.java
Normal file
@ -0,0 +1,24 @@
|
||||
package context;
|
||||
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
|
||||
public class ChromeContext extends Context {
|
||||
private final static String WINDOWS_DRIVER = "chromedriver.exe";
|
||||
private final static String LINUX_DRIVER = "chromedriver";
|
||||
private final static String DRIVER_TYPE = "webdriver.chrome.driver";
|
||||
|
||||
@Override
|
||||
protected void createDriver() {
|
||||
driver = new ChromeDriver();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverExecutable(boolean isWindows) {
|
||||
return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverType() {
|
||||
return DRIVER_TYPE;
|
||||
}
|
||||
}
|
42
src/test/java/context/Context.java
Normal file
42
src/test/java/context/Context.java
Normal file
@ -0,0 +1,42 @@
|
||||
package context;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public abstract class Context {
|
||||
private final static String DRIVER_LOCATION = "drivers/%s";
|
||||
protected WebDriver driver;
|
||||
|
||||
public WebDriver getDriver() {
|
||||
if (driver != null) {
|
||||
return driver;
|
||||
}
|
||||
throw new IllegalStateException("WebDriver is not initialized");
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.setProperty(getDriverType(), getDriverExecutablePath());
|
||||
createDriver();
|
||||
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
protected abstract void createDriver();
|
||||
|
||||
protected abstract String getDriverType();
|
||||
|
||||
protected abstract String getDriverExecutable(boolean windows);
|
||||
|
||||
private String getDriverExecutablePath() {
|
||||
return Context.class.getClassLoader().getResource(
|
||||
String.format(DRIVER_LOCATION, getDriverExecutable(isWindows()))).getFile();
|
||||
}
|
||||
|
||||
private boolean isWindows() {
|
||||
return System.getProperty("os.name").toLowerCase().contains("windows");
|
||||
}
|
||||
}
|
24
src/test/java/context/FirefoxContext.java
Normal file
24
src/test/java/context/FirefoxContext.java
Normal file
@ -0,0 +1,24 @@
|
||||
package context;
|
||||
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
public class FirefoxContext extends Context {
|
||||
private final static String WINDOWS_DRIVER = "geckodriver.exe";
|
||||
private final static String LINUX_DRIVER = "geckodriver";
|
||||
private final static String DRIVER_TYPE = "webdriver.gecko.driver";
|
||||
|
||||
@Override
|
||||
protected void createDriver() {
|
||||
driver = new FirefoxDriver();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverExecutable(boolean isWindows) {
|
||||
return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverType() {
|
||||
return DRIVER_TYPE;
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package utils;
|
||||
|
||||
public class SystemUtils {
|
||||
|
||||
public static boolean isWindows() {
|
||||
return System.getProperty("os.name").toLowerCase().contains("windows");
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user