Webdriver Ui自动化测试,使用工厂方法生产不同浏览器的webdriver实例



定义浏览器驱动实例的接口:

package ec.qa.autotest.ui.utility.base;


/**
 * 实例化目标浏览器接口
 * @author xin.wang
 *
 */
public interface IBrowserDriverBase {
	
	public void configBrowser() throws Exception;
	
	public <T> T getWebDriver()throws Exception;
}



实现以上接口的子类

 ChromeDriver:

package ec.qa.autotest.ui.testbase;

import java.io.File;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

import ec.qa.autotest.ui.constants.CommonConstants;
import ec.qa.autotest.ui.utility.base.IBrowserDriverBase;

public class ChromeBrowserDriver implements IBrowserDriverBase {

	private ChromeDriverService chromeService = null;

	public void configBrowser() throws Exception {
		System.setProperty(CommonConstants.CHROME_DRIVER_FIELD, CommonConstants.CHROME_DRIVER_PATH);
		chromeService = new ChromeDriverService.Builder()
				.usingDriverExecutable(new File(CommonConstants.CHROME_DRIVER_PATH)).usingAnyFreePort().build();
		chromeService.start();
	}

	@SuppressWarnings("unchecked")
	public <T> T getWebDriver() throws Exception {
		configBrowser();
		return (T) new ChromeDriver(chromeService);
	}
}



IEDriver :

package ec.qa.autotest.ui.testbase;

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import ec.qa.autotest.ui.constants.CommonConstants;
import ec.qa.autotest.ui.utility.base.IBrowserDriverBase;

public class IEBrowserDriver implements IBrowserDriverBase {
	
	private DesiredCapabilities dcIE;

	public void configBrowser() throws Exception {
		System.setProperty(CommonConstants.IE_DRIVER_FIELD, CommonConstants.IE_DRIVER_PATH);
		dcIE = DesiredCapabilities.internetExplorer();
		dcIE.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		dcIE.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
	}

	@SuppressWarnings("unchecked")
	public <T> T getWebDriver() throws Exception {
		return (T) new InternetExplorerDriver(dcIE);
	}
}



FireFoxDriver:

package ec.qa.autotest.ui.testbase;

import org.openqa.selenium.firefox.FirefoxDriver;

import ec.qa.autotest.ui.utility.base.IBrowserDriverBase;

public class FireFoxBrowserDriver implements IBrowserDriverBase {

	public void configBrowser() throws Exception {
		// TODO Auto-generated method stub
	}

	@SuppressWarnings("unchecked")
	public <T> T getWebDriver() throws Exception {
		// TODO Auto-generated method stub
		return (T) new FirefoxDriver();
	}
}


定义工厂接口:

package ec.qa.autotest.ui.utility.base;

/**
 * @author xin.wang Webdriver 工厂接口
 */
public interface IWebdriverFactory {
	
	public <T> void setDriver(T driver)throws Exception;

	public <T> T getDriver(String classPackageName) throws Exception;
}


定义具体工厂类:

package ec.qa.autotest.ui.testbase;

import ec.qa.autotest.ui.utility.base.IBrowserDriverBase;
import ec.qa.autotest.ui.utility.base.IWebdriverFactory;

/**
 * @author xin.wang
 * @see 用于配置和生产基于不同浏览器的webdriver。
 */
public class DriverFactory  implements IWebdriverFactory {

	/**
	 * @author xin.wang
	 * @param classPackageName
	 *            浏览器类型
	 * @throws Exception
	 * @see 根据指定的浏览器获取并初始化webdriver对象实例
	 **/

	public <T> T getDriver(String classPackageName) throws Exception {
		Object obj = Class.forName(classPackageName).newInstance();
		return ((IBrowserDriverBase)obj).getWebDriver();
	}

	public <T> void setDriver(T driver) {
		// TODO Auto-generated method stub
	}
}


至此你可以在测试过程中 在设当位置初始化webdriver:

@BeforeMethod
	private void configDriver(ConfigDriverParameters cp) throws Exception {
		RemoteWebDriver webDriver;
		IWebdriverFactory iw = new DriverFactory();
		webDriver = iw.getDriver(browserType);
		new InitPageObject(this);
		webDriver.manage().timeouts().implicitlyWait(cp.getSerachElementTime(), TimeUnit.SECONDS);
		webDriver.manage().window().maximize();
		webDriver.manage().timeouts().pageLoadTimeout(cp.getPageLoadTime(), TimeUnit.SECONDS);
	}


browserType 在配置文件中:

#启动目标浏览器驱动运行测试
#Browser.Type=ec.qa.autotest.ui.testbase.IEBrowserDriver  
Browser.Type=ec.qa.autotest.ui.testbase.ChromeBrowserDriver
#Browser.Type=ec.qa.autotest.ui.testbase.FireFoxBrowserDriver


你可能感兴趣的:(java,泛型,java设计模式,自动化测试,webdriver)