标签:logs selenium nbsp internet drive img lca selenium2 put
通过在xml中配置各浏览器的driver地址,以及本次运行中使用哪个浏览器进行测试
driverIndex代表启动浏览器的序号,0是Chrome,1、2是FireFox,3是IE
driverProperties.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 只需要修改下面的driverIndex 就可以去创建对应index的驱动 --> <driver driverIndex="3"> <!-- 谷歌浏览器配置文件 --> <name value="org.openqa.selenium.chrome.ChromeDriver" index="0"> <properties> <property name="webdriver.chrome.driver" value="C:/webdriver/chromedriver.exe" /> </properties> </name> <!-- 火狐浏览器 对应的selenium3.x版本 的配置文件 --> <name value="org.openqa.selenium.firefox.FirefoxDriver" seleniumVersion="3.x" index="1"> <properties> <property name="SystemProperty.BROWSER_BINARY" value="C:\Program Files\Mozilla Firefox\firefox.exe" /> <property name="webdriver.gecko.driver" value="C:/webdriver/geckodriver.exe" /> </properties> </name> <!-- 火狐浏览器 对应的selenium2.x版本 的配置文件 --> <name value="org.openqa.selenium.firefox.FirefoxDriver" seleniumVersion="2.x" index="2"> <properties> <property name="SystemProperty.BROWSER_BINARY" value="C:\Program Files\Mozilla Firefox\firefox.exe" /> </properties> </name> <!-- IE浏览器配置文件 --> <name value="org.openqa.selenium.ie.InternetExplorerDriver" index="3"> <properties> <property name="webdriver.ie.driver" value="C:/webdriver/IEDriverServer.exe" /> </properties> <capabilities> <capability name="InternetExplorerDriver.IGNORE_ZOOM_SETTING" /> <capability name="InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS" /> </capabilities> </name> </driver>
GetDriverUtil.java
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class GetDriverUtil { private static Class clazz; private static Object obj ; public static void main(String[] args) throws InterruptedException, FileNotFoundException { WebDriver driver = getDriver(); driver.get("http://www.baidu.com"); Thread.sleep(10000); driver.quit(); } public static WebDriver getDriver() throws FileNotFoundException { Document document = null; Element driverNameElement= null; String driverName =null; SAXReader reader = new SAXReader(); try { File f = new File("./driverProperties.xml"); InputStream in = new FileInputStream(f); document = reader.read(in); //document = reader.read(GetDriverUtil.class.getResourceAsStream("./driverProperties.xml")); } catch (DocumentException e) { e.printStackTrace(); } /** * 下面是通过解析XML,获取到驱动的类全名 */ Element rootElement = document.getRootElement(); //获取到根节点 int index = Integer.parseInt(rootElement.attributeValue("driverIndex"));//获取到根节点上的driverIndex并转成int类型 //获取到所有"name"子节点,遍历,找出与根节点中的driverIndex相同的,将其value属性值获取出来,作为类全名用于反射 List<Element> driverNameElements = rootElement.elements("name"); for (Element driverNameElement1 : driverNameElements) { int i = Integer.parseInt(driverNameElement1.attributeValue("index")); if (i == index) { driverName = driverNameElement1.attributeValue("value");//获取到name子节点的“value”属性值 driverNameElement = driverNameElement1;//将该节点赋值给driverElement,后续根据它来获得子节点 break; } } /** * 通过类全名,反射出驱动类来 */ try { clazz = Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } /** * 下面是解析XML中的系统参数以及能力参数 */ Element propertiesElement = driverNameElement.element("properties"); List<Element> propertyElements = propertiesElement.elements("property"); //设置系统参数 for (Element property : propertyElements) { System.setProperty(property.attributeValue("name"), property.attributeValue("value")); } //设置能力(ie的话,需要设置忽略域设置等级 以及忽略页面百分比的能力) Element capabilitiesElement = driverNameElement.element("capabilities"); if (capabilitiesElement != null) { //创建能力对象 DesiredCapabilities realCapabilities = new DesiredCapabilities(); //获得能力列表 List<Element> capabilitiesElements = capabilitiesElement.elements("capability"); for (Element capability : capabilitiesElements) { //遍历能力列表,并给能力赋值 realCapabilities.setCapability(capability.attributeValue("name"), true); } } /* * 通过反射,创建驱动对象 */ try { obj = clazz.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } WebDriver driver = (WebDriver) obj; return driver; } }
参考地址:www.cnblogs.com/clairejing/p/9459466.html
标签:logs selenium nbsp internet drive img lca selenium2 put
原文地址:https://www.cnblogs.com/pashanhu/p/9463870.html