Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.yourpackage.tests;
  2. /**
  3.  * Created by wcn on 4/2/2014.
  4.  */
  5.  
  6. import org.apache.commons.io.FileUtils;
  7. import org.openqa.selenium.OutputType;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.chrome.ChromeDriver;
  10. import org.openqa.selenium.firefox.FirefoxDriver;
  11. import org.openqa.selenium.firefox.FirefoxProfile;
  12. import org.openqa.selenium.ie.InternetExplorerDriver;
  13. import org.openqa.selenium.remote.CapabilityType;
  14. import org.openqa.selenium.remote.DesiredCapabilities;
  15. import org.testng.annotations.AfterMethod;
  16. import org.testng.annotations.BeforeMethod;
  17. import org.testng.annotations.Parameters;
  18.  
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.concurrent.TimeUnit;
  22.  
  23.  
  24. public class YourTest {
  25.     protected WebDriver driver;
  26.     protected static String startUrl;
  27.  
  28.     @BeforeMethod
  29.     @Parameters("browser")
  30.     public void prepareTest(String browser){
  31.  
  32.         startUrl = "https://yourUrl.com";
  33.  
  34.         if(browser.equalsIgnoreCase("FF"))
  35.         {
  36.             System.out.println("Firefox driver would be used");
  37.             final String firebugPath = "C:\\firebug.xpi";
  38.             final String firepathPath = "C:\\firepath.xpi";
  39.             FirefoxProfile profile = new FirefoxProfile();
  40.             try {
  41.                 profile.addExtension(new File(firebugPath));
  42.                 profile.addExtension(new File(firepathPath));
  43.                 profile.setPreference("dom.max_script_run_time", 60);
  44.             } catch (IOException e) {
  45.                 e.printStackTrace();
  46.             }
  47.             driver = new FirefoxDriver(profile);
  48.         }
  49.         else if(browser.equalsIgnoreCase("IE"))
  50.         {
  51.             System.out.println("Ie webdriver would be used");
  52.             System.setProperty("webdriver.ie.driver", "c:\\IEDriverServer.exe");
  53.             DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
  54.             capab.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
  55.             driver = new InternetExplorerDriver(capab);
  56.         }
  57.         else if(browser.equalsIgnoreCase("CH"))
  58.         {
  59.             System.out.println("Chrome webdriver would be used");
  60.             System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
  61.             driver = new ChromeDriver();
  62.         }
  63.  
  64.         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  65.         driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
  66.         driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
  67.     }
  68.  
  69.     @AfterMethod
  70.     public void closeBrowser(){
  71.         takeScreenshot();
  72.         driver.close();
  73.         driver.quit();
  74.     }
  75.  
  76.     private void takeScreenshot() {
  77.         File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  78.         try {
  79.  
  80.             FileUtils.copyFile(scrFile, new File("target\\screenshot_" + this.getClass().getName() + ".jpg"));
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.     }
  85.  
  86.     protected void setImplicitlyWaitTimeout(long seconds){
  87.         driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
  88.     }
  89. }