【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值得方式还未研究,待补充;

猜你喜欢

转载自blog.csdn.net/df0128/article/details/82812829