Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 4.49 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package eurostar.test;
  2.  
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. import org.apache.log4j.Logger;
  11. import org.apache.log4j.PropertyConfigurator;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.ie.InternetExplorerDriver;
  14. import org.openqa.selenium.remote.Augmenter;
  15. import org.openqa.selenium.remote.DesiredCapabilities;
  16. import org.openqa.selenium.remote.RemoteWebDriver;
  17. import org.openqa.selenium.support.events.EventFiringWebDriver;
  18. import org.testng.Assert;
  19. import org.testng.Reporter;
  20. import org.testng.annotations.AfterClass;
  21. import org.testng.annotations.AfterMethod;
  22. import org.testng.annotations.BeforeClass;
  23. import org.testng.annotations.BeforeMethod;
  24.  
  25. import eurostar.utils.Config;
  26.  
  27. public class BaseTest {
  28.         protected static String WEB_SERVER = null;
  29.         protected static int BROWSER_VERSION = 0;
  30.         protected static  String BROWSER = null;
  31.         protected static  String SELENIUM_HOST = null;
  32.         protected static  int SELENIUM_PORT = 0;
  33.         protected static  String DATAPOOL = null;
  34.         protected static  String FLB = null;
  35.         public static  String SREENSHOTPATH = null;
  36.         private static Map verificationFailuresMap = new HashMap();
  37.         public static Logger APP_LOGS = Logger.getLogger("devpinoyLogger");
  38.  
  39.         public static WebDriver driver;
  40.        
  41.  
  42.  
  43.         @BeforeClass(alwaysRun = true)
  44.         public void suiteSetup() throws Exception {
  45.                 PropertyConfigurator.configure(System.getProperty("user.dir")+"/log4j.properties");
  46.                 Config.init();
  47.                 WEB_SERVER = Config.CONFIG.getProperty("baseTestUrl");
  48.                 BROWSER_VERSION = Integer.parseInt(Config.CONFIG.getProperty("browserVersion"));
  49.                 BROWSER = Config.CONFIG.getProperty("browser");
  50.                 SELENIUM_HOST = Config.CONFIG.getProperty("host");
  51.                 SELENIUM_PORT = Integer.parseInt(Config.CONFIG.getProperty("port"));
  52.                 DATAPOOL = Config.CONFIG.getProperty("datapool");
  53.                 FLB = Config.CONFIG.getProperty("turnFlbOn");
  54.                 SREENSHOTPATH = Config.CONFIG.getProperty("screenshotPath");
  55.  
  56.                 DesiredCapabilities capabilities;
  57.                 WebDriver wrappedDriver;
  58.                 EventFiringWebDriver eventFiringWebDriver;
  59.                 if (BROWSER.equalsIgnoreCase("firefox")) {
  60.                         capabilities = DesiredCapabilities.firefox();
  61.                 } else if (BROWSER.equalsIgnoreCase("internetExplorer")) {
  62.                         capabilities = DesiredCapabilities.internetExplorer();
  63.                         capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  64.                 } else if (BROWSER.equalsIgnoreCase("chrome")) {
  65.                         capabilities = DesiredCapabilities.chrome();
  66.                 }
  67.                 else {
  68.                         throw new RuntimeException("Browser type unsupported");
  69.                 }
  70.                 wrappedDriver = new RemoteWebDriver(new URL("http://" + SELENIUM_HOST + ":" + SELENIUM_PORT + "/wd/hub"),capabilities);
  71.                 wrappedDriver = (RemoteWebDriver)new Augmenter().augment(wrappedDriver);
  72.                 wrappedDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  73.                
  74.                 driver = wrappedDriver;
  75.  
  76.         }
  77.  
  78.         @BeforeMethod(alwaysRun = true)
  79.         public void loadWebApplication() {
  80.                 driver.get(WEB_SERVER);
  81.         }
  82.  
  83.         @AfterMethod(alwaysRun = true)
  84.         public void deleteAllCookies() {
  85.                 driver.manage().deleteAllCookies();
  86.         }
  87.  
  88.         @AfterClass(alwaysRun = true)
  89.         public void suiteTearDown() {
  90.                 driver.quit();
  91.         }
  92.        
  93.         public static void assertTrue(boolean condition) {
  94.                 Assert.assertTrue(condition);
  95.         }
  96.  
  97.         public static void assertFalse(boolean condition) {
  98.                 Assert.assertFalse(condition);
  99.         }
  100.  
  101.         public static void assertEquals(Object actual, Object expected) {
  102.                 Assert.assertEquals(actual, expected);
  103.         }
  104.  
  105.         public static void verifyTrue(boolean condition) {
  106.                 try {
  107.                         assertTrue(condition);
  108.                 } catch(Throwable e) {
  109.                         addVerificationFailure(e);
  110.                 }
  111.         }
  112.  
  113.         public static void verifyFalse(boolean condition) {
  114.                 try {
  115.                         assertFalse(condition);
  116.                 } catch(Throwable e) {
  117.                         addVerificationFailure(e);
  118.                 }
  119.         }
  120.  
  121.         public static void verifyEquals(Object actual, Object expected) {
  122.                 try {
  123.                         assertEquals(actual, expected);
  124.                 } catch(Throwable e) {
  125.                         addVerificationFailure(e);
  126.                 }
  127.         }
  128.  
  129.         public static List getVerificationFailures() {
  130.                 List verificationFailures = (List) verificationFailuresMap.get(Reporter.getCurrentTestResult());
  131.                 return verificationFailures == null ? new ArrayList() : verificationFailures;
  132.         }
  133.  
  134.         private static void addVerificationFailure(Throwable e) {
  135.                 List verificationFailures = getVerificationFailures();
  136.                 verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures);
  137.                 verificationFailures.add(e);
  138.         }
  139.  
  140.  
  141. }