- package eurostar.test;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.ie.InternetExplorerDriver;
- import org.openqa.selenium.remote.Augmenter;
- import org.openqa.selenium.remote.DesiredCapabilities;
- import org.openqa.selenium.remote.RemoteWebDriver;
- import org.openqa.selenium.support.events.EventFiringWebDriver;
- import org.testng.Assert;
- import org.testng.Reporter;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.BeforeMethod;
- import eurostar.utils.Config;
- public class BaseTest {
- protected static String WEB_SERVER = null;
- protected static int BROWSER_VERSION = 0;
- protected static String BROWSER = null;
- protected static String SELENIUM_HOST = null;
- protected static int SELENIUM_PORT = 0;
- protected static String DATAPOOL = null;
- protected static String FLB = null;
- public static String SREENSHOTPATH = null;
- private static Map verificationFailuresMap = new HashMap();
- public static Logger APP_LOGS = Logger.getLogger("devpinoyLogger");
- public static WebDriver driver;
- @BeforeClass(alwaysRun = true)
- public void suiteSetup() throws Exception {
- PropertyConfigurator.configure(System.getProperty("user.dir")+"/log4j.properties");
- Config.init();
- WEB_SERVER = Config.CONFIG.getProperty("baseTestUrl");
- BROWSER_VERSION = Integer.parseInt(Config.CONFIG.getProperty("browserVersion"));
- BROWSER = Config.CONFIG.getProperty("browser");
- SELENIUM_HOST = Config.CONFIG.getProperty("host");
- SELENIUM_PORT = Integer.parseInt(Config.CONFIG.getProperty("port"));
- DATAPOOL = Config.CONFIG.getProperty("datapool");
- FLB = Config.CONFIG.getProperty("turnFlbOn");
- SREENSHOTPATH = Config.CONFIG.getProperty("screenshotPath");
- DesiredCapabilities capabilities;
- WebDriver wrappedDriver;
- EventFiringWebDriver eventFiringWebDriver;
- if (BROWSER.equalsIgnoreCase("firefox")) {
- capabilities = DesiredCapabilities.firefox();
- } else if (BROWSER.equalsIgnoreCase("internetExplorer")) {
- capabilities = DesiredCapabilities.internetExplorer();
- capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
- } else if (BROWSER.equalsIgnoreCase("chrome")) {
- capabilities = DesiredCapabilities.chrome();
- }
- else {
- throw new RuntimeException("Browser type unsupported");
- }
- wrappedDriver = new RemoteWebDriver(new URL("http://" + SELENIUM_HOST + ":" + SELENIUM_PORT + "/wd/hub"),capabilities);
- wrappedDriver = (RemoteWebDriver)new Augmenter().augment(wrappedDriver);
- wrappedDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- driver = wrappedDriver;
- }
- @BeforeMethod(alwaysRun = true)
- public void loadWebApplication() {
- driver.get(WEB_SERVER);
- }
- @AfterMethod(alwaysRun = true)
- public void deleteAllCookies() {
- driver.manage().deleteAllCookies();
- }
- @AfterClass(alwaysRun = true)
- public void suiteTearDown() {
- driver.quit();
- }
- public static void assertTrue(boolean condition) {
- Assert.assertTrue(condition);
- }
- public static void assertFalse(boolean condition) {
- Assert.assertFalse(condition);
- }
- public static void assertEquals(Object actual, Object expected) {
- Assert.assertEquals(actual, expected);
- }
- public static void verifyTrue(boolean condition) {
- try {
- assertTrue(condition);
- } catch(Throwable e) {
- addVerificationFailure(e);
- }
- }
- public static void verifyFalse(boolean condition) {
- try {
- assertFalse(condition);
- } catch(Throwable e) {
- addVerificationFailure(e);
- }
- }
- public static void verifyEquals(Object actual, Object expected) {
- try {
- assertEquals(actual, expected);
- } catch(Throwable e) {
- addVerificationFailure(e);
- }
- }
- public static List getVerificationFailures() {
- List verificationFailures = (List) verificationFailuresMap.get(Reporter.getCurrentTestResult());
- return verificationFailures == null ? new ArrayList() : verificationFailures;
- }
- private static void addVerificationFailure(Throwable e) {
- List verificationFailures = getVerificationFailures();
- verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures);
- verificationFailures.add(e);
- }
- }