个人写的一个selenium的base类,应该所有使用selenium的同事都会使用到:
package com.hx.baserunner; import static java.io.File.separator; import java.io.File; import java.io.FileInputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Map; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.openqa.selenium.Capabilities; import org.openqa.selenium.OutputType; import org.openqa.selenium.Proxy; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.UnreachableBrowserException; import org.testng.ITestContext; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; import org.testng.annotations.DataProvider; import org.testng.annotations.Parameters; import com.hx.dataproviders.ExcelDataProivderLoginSheet; import com.hx.utility.HostUtil; public class BaseSeleniumDriver { public WebDriver driver=null; public String proxyserver, browser, hubUrl; private static final Logger log=Logger.getLogger(BaseSeleniumDriver.class); @BeforeSuite //@Parameters({ "excelpath"}) public void beforeSuite() throws Exception{ // Properties p = new Properties(); // FileInputStream conf = new FileInputStream(configfile); // p.load(conf); String excelpath=System.getProperty("user.dir")+"\resources\TestData.xls"; log.debug("excel path is "+excelpath); String hostname=HostUtil.getFQDN(); log.debug("the running host is :"+hostname); Map<String,String> mapdata=ExcelDataProivderLoginSheet.getSpecifySheet(excelpath,hostname); // hubUrl = p.getProperty("hubUrl"); // hubUrl="http://127.0.0.1:4444/wd/hub"; // browser = p.getProperty("browser"); browser=mapdata.get("Browser_Type").trim().toLowerCase(); // testUrl = p.getProperty("testUrl"); // log.info("The Page URL is:"+testUrl); proxyserver=mapdata.get("proxy_url").trim(); log.debug("the browser type is :"+browser); log.debug("the remote run host hub is :"+hubUrl); log.debug("the browser's proxy server is :"+proxyserver); DesiredCapabilities capability=new DesiredCapabilities(); //common settings capability.setCapability("cssSelectorsEnabled", true); capability.setCapability("takesScreenshot", true); capability.setCapability("javascriptEnabled", true); capability.setCapability("ACCEPT_SSL_CERTS", true); capability.setBrowserName(browser); //proxy settings // if(!proxyserver.equals("")) // { // log.debug(" the current proxy is not null ,we will set the proxy server for this host,proxy server is :"+proxyserver); // capability.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyserver)); // capability.setCapability(CapabilityType.PROXY, new Proxy().setNoProxy("localhost")); // log.debug("the proxy had been set correctly now "); // } //use different browser if (hubUrl == null || hubUrl.trim().isEmpty()) { log.debug("the blow testing is for the local server testing"); // if no hubUrl specified, run the tests on localhost if (browser == null || browser.trim().isEmpty()) { // if no browser specified, use IE //capability =DesiredCapabilities.internetExplorer(); log.debug(" the browser we used is IE "); capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); capability.setCapability("ignoreProtectedModeSettings",true); capability.setCapability("enablePersistentHover", false); //prevent frozen driver = new InternetExplorerDriver(capability); log.debug("Start the IE driver now "); } else { if (browser.trim().equalsIgnoreCase("firefox")) { FirefoxProfile p = new FirefoxProfile(); p.setPreference("webdriver.log.file", "log/firefox_startup.log"); driver = new FirefoxDriver(capability); log.debug("Start the firefox driver now "); } else if (browser.trim().equalsIgnoreCase("chrome")) { driver = new ChromeDriver(capability); log.debug("start the chrome driver now "); } else { driver = new InternetExplorerDriver(capability); log.debug("start the IE driver now "); } } } else { log.debug("we will run the remote host for the testing "); // DesiredCapabilities capability=null; if (browser.toLowerCase().trim().equals("ie")) { //frozen windows capability =DesiredCapabilities.internetExplorer(); log.debug(" the browser we used is IE "); capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); capability.setCapability("ignoreProtectedModeSettings",true); capability.setCapability("enablePersistentHover", false); //prevent frozen } else if(browser.toLowerCase().trim().equals("firefox")) { capability =DesiredCapabilities.firefox(); log.debug("the browser we used is firefox"); //if need the proxy } else { capability =DesiredCapabilities.chrome(); log.debug("the browser we used is none"); } //driver = new RemoteWebDriver(new URL(hubUrl), capability); driver = new RemoteWebDriver(capability); Capabilities actualCapabilities = ((RemoteWebDriver) driver).getCapabilities(); } //the driver need to wait time driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @AfterMethod public void afterMethod(ITestResult result, ITestContext context) throws Exception { Throwable t = result.getThrowable(); log.debug("the throwable object is :"+t); //if the testNG met error or exception if ((!result.isSuccess())||t instanceof WebDriverException || t instanceof AssertionError) { log.error("WebDriverException or Assert Exception"); // get filename Calendar cal = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); // concat prefix with current time and return String filename = result.getTestClass().getName() + "." + result.getMethod().getMethodName() + "." + sf.format(cal.getTime()) + ".png"; log.debug("we met the error ,we will generate a screenshot file for this error, file name is "+filename); // WebDriver augmentedDriver = new Augmenter().augment(driver); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String path=new File(".").getAbsolutePath(); String screenshotpath=path.substring(0, path.length()-1); // create a new file FileUtils.copyFile(scrFile, new File(screenshotpath+"log"+separator + filename)); log.debug("the screenshot file in this file path:"+screenshotpath+"log"+separator + filename); Reporter.setCurrentTestResult(result); //Reporter.log("<a href="" + filename + "">Screenshot</a>"); } else { log.debug("This test method is working fine ,we marked it as passed"); } } @AfterSuite public void afterSuite() { driver.quit(); log.debug("quit the driver now "); } }