1. 程式人生 > >【selenium3+JAVA】介面自動化測試教程(一)——瀏覽器的啟動之IE瀏覽器的啟動

【selenium3+JAVA】介面自動化測試教程(一)——瀏覽器的啟動之IE瀏覽器的啟動

前言

同chrome、firefox,ie瀏覽器的啟動一樣需要driver,但是IEDriver不像chromeDriver一樣有多個版本,不同版本對應不同的chrome版本,它只有32位版本和64位版本兩種; 下載地址為:https://www.seleniumhq.org/download/

啟動IE瀏覽器

1、設定驅動

設定驅動程式碼如下,第二個引數即為驅動的路徑,可以任意設定:

System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");

2、簡單啟動IE瀏覽器

簡單啟動程式碼如下,不用帶任何設定;

System.
setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe"); InternetExplorerDriver ie = new InternetExplorerDriver(); ie.get("https://www.baidu.com/"); System.out.println(ie.getTitle()); ie.quit();

3、定製啟動之帶Options

使用此種方法使用程式碼:

public static void main( String[] args )
    {
    	System.setProperty
("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe"); InternetExplorerOptions options = new InternetExplorerOptions(); options.usePerProcessProxy(); InternetExplorerDriver ie = new InternetExplorerDriver(options); ie.get("https://www.baidu.com/"); System.out.println(ie.
getTitle()); ie.quit(); }

(1)、新增代理

新增代理方法和chrome還有firefox相同,如下:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
    	DesiredCapabilities source = DesiredCapabilities.internetExplorer();
    	source.setJavascriptEnabled(true);
    	InternetExplorerOptions options = new InternetExplorerOptions(source);
    	options.usePerProcessProxy();
    	Proxy proxy = new Proxy();
    	proxy.setAutodetect(true);//是否自動檢測代理設定;
    	proxy.setFtpProxy(proxyIpAndPort);//ftp代理地址
    	proxy.setHttpProxy(proxyIpAndPort);//http代理地址
    	proxy.setNoProxy(proxyIpAndPort);//無需代理的網址列表,用;分隔
    	proxy.setProxyAutoconfigUrl(proxyAutoconfigUrl);//代理自動設定地址
    	proxy.setProxyType(ProxyType.MANUAL);//手工代理,必填
    	proxy.setSocksPassword(password);//密碼
    	proxy.setSocksUsername(username);//使用者名稱
    	proxy.setSocksVersion(5);//新增此項會自動設定proxyType為ProxyType.MANUAL,此項設定內容為socks的版本,為4或者5,最新為5
    	proxy.setSslProxy(proxyIpAndPort);
    	options.setProxy(proxy);
    	InternetExplorerDriver ie = new InternetExplorerDriver(options);
    	ie.get("https://www.baidu.com/");
    	System.out.println(ie.getTitle());
    	ie.quit();
    }

(2)帶Capability

IE和Chrome還有firefox不同,可以使用的options選項不多,且還可以直接附帶Capablitity,如下所示:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
    	DesiredCapabilities source = DesiredCapabilities.internetExplorer();
    	source.setJavascriptEnabled(true);
    	InternetExplorerOptions options = new InternetExplorerOptions(source);
    	options.usePerProcessProxy();
    	InternetExplorerDriver ie = new InternetExplorerDriver(options);
    	ie.get("https://www.baidu.com/");
    	System.out.println(ie.getTitle());
    	ie.quit();
    }

其他待補充

4、定製啟動之帶Capabilities

此方式結果和用Options直接新增Capablities功能相同,使用程式碼如下:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
    	DesiredCapabilities source = DesiredCapabilities.internetExplorer();
    	source.setJavascriptEnabled(true);
    	InternetExplorerDriver ie = new InternetExplorerDriver(source);
    	ie.get("https://www.baidu.com/");
    	System.out.println(ie.getTitle());
    	ie.quit();
    }

新增Capability的方法,即對應key值和value值得方式還未研究,待補充;