add firefox driver

This commit is contained in:
romanov73 2018-02-16 22:40:50 +04:00
parent 22bad673df
commit dce6fd4ccd
9 changed files with 107 additions and 90 deletions

View File

@ -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();
}
}

Binary file not shown.

Binary file not shown.

View File

@ -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());
}
}

View 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;
}
}

View 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");
}
}

View 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;
}
}

View File

@ -1,8 +0,0 @@
package utils;
public class SystemUtils {
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
}

View File

@ -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();
}
}