模拟浏览器自动化测试工具Selenium之六设置代理篇
2017-02-22 17:36
495 查看
在使用Selenium自动化测试时,如果需要设置代理访问网络时,可以参考如下代码:
package com.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class EBayCom {
public static void main(String[] args) {
System.getProperties().setProperty("webdriver.chrome.driver","D:\\dev\\workspace\\ocweb\\libs\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
//设置代理
String proxyIpAndPort= "ip:post";
DesiredCapabilities cap = new DesiredCapabilities();
Proxy proxy=new Proxy();
proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
System.setProperty("http.nonProxyHosts", "localhost");
cap.setCapability(CapabilityType.PROXY, proxy);
//访问网址
try {
webDriver.get("IP");//访问网址
//标题
WebElement eleItemTitle = webDriver.findElement(By.id("itemTitle"));
String txtItemTitle=eleItemTitle.getText();
System.out.println(txtItemTitle);
}catch (Exception e) { System.err.println( "Exception: " + e );}
webDriver.close();
webDriver.quit();
}
}
package com.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class EBayCom {
public static void main(String[] args) {
System.getProperties().setProperty("webdriver.chrome.driver","D:\\dev\\workspace\\ocweb\\libs\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
//设置代理
String proxyIpAndPort= "ip:post";
DesiredCapabilities cap = new DesiredCapabilities();
Proxy proxy=new Proxy();
proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
System.setProperty("http.nonProxyHosts", "localhost");
cap.setCapability(CapabilityType.PROXY, proxy);
//访问网址
try {
webDriver.get("IP");//访问网址
//标题
WebElement eleItemTitle = webDriver.findElement(By.id("itemTitle"));
String txtItemTitle=eleItemTitle.getText();
System.out.println(txtItemTitle);
}catch (Exception e) { System.err.println( "Exception: " + e );}
webDriver.close();
webDriver.quit();
}
}
相关文章推荐
- 模拟浏览器自动化测试工具Selenium之一eclipse集成开发部署篇
- 模拟浏览器自动化测试工具Selenium之七采集网页信息写入excel
- Selenium2学习-024-WebUI自动化实战实例-022-网站不同分辨率下页面样式展示兼容性问题解决方案 -- 设置浏览器显示区域大小(无人值守,节约测试成本的福音,BOSS 最爱)
- 自动化测试工具selenium常见的元素定位方法
- WIN7- selenium-python 自动化测试工具配置环境
- 自动化测试工具 Selenium WebDriver 入门教程
- 需求:在openstack中安装极小的Linux,可以安装Firefox浏览器,接着安装selenium完成自动化网页测试
- Web自动化测试工具Selenium 1.0正式发布:Chrome支持+用户指南
- Selenium自动化测试工具学习总结(基础篇)
- Selenium2学习-021-WebUI自动化实战实例-019-设置浏览器窗口位置大小
- 一步一步使用自动化测试工具selenium
- 使用Selenium通过浏览器对网站进行自动化测试
- Web自动化测试工具Selenium 1.0正式发布:Chrome支持+用户指南
- selenium webdriver + Junit 自动化测试:打开不同浏览器网页
- 两个WEB自动化测试工具比较 WatiN & Selenium RC
- Web自动化测试工具Selenium 1.0正式发布:Chrome支持+用户指南
- Web自动化测试工具Selenium 1.0正式发布:Chrome支持+用户指南
- 自动化测试工具selenium 之Selenium-IDE
- 自动化测试工具selenium初探
- Python-Selenium2做Web自动化测试(2)-自动化测试常用工具