Alb*_*rto 6 selenium selenium-rc selenium-webdriver
我需要发送一个带有一些修改过的标头的 Http 请求。在尝试Selenium.addCustomRequestHeader为 Selenium 2寻找与 Selenium RC 等效的方法几个小时后,我放弃并使用 JavaScript 来达到我的目的。我预计这会容易得多!
有人知道更好的方法吗?
这就是我所做的:
var test = {
"sendHttpHeaders": function(dst, header1Name, header1Val, header2Name, header2Val) {
var http = new XMLHttpRequest();
http.open("GET", dst, "false");
http.setRequestHeader(header1Name,header1Val);
http.setRequestHeader(header2Name,header2Val);
http.send(null);
}
}
Run Code Online (Sandbox Code Playgroud)
// ...
@Test
public void testFirstLogin() throws Exception {
WebDriver driver = new FirefoxDriver();
String url = System.getProperty(Constants.URL_PROPERTY_NAME);
driver.get(url);
// Using javascript to send http headers
String scriptResource = this.getClass().getPackage().getName()
.replace(".", "/") + "/javascript.js";
String script = getFromResource(scriptResource)
+ "test.sendHttpHeaders(\"" + url + "\", \"" + h1Name
+ "\", \"" + h1Val + "\", \"" + h2Name + "\", \"" + h2Val + "\");";
LOG.debug("script: " + script);
((JavascriptExecutor)driver).executeScript(loginScript);
// ...
}
// I don't like mixing js with my code. I've written this utility method to get
// the js from the classpath
/**
* @param src name of a resource that must be available from the classpath
* @return new string with the contents of the resource
* @throws IOException if resource not found
*/
public static String getFromResource(String src) throws IOException {
InputStream is = Thread.currentThread().getContextClassLoader().
getResourceAsStream(src);
if (null == is) {
throw new IOException("Resource " + src + " not found.");
}
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String line = null;
int nLines = 0;
while (null != (line = br.readLine())) {
pw.println(line);
nLines ++;
}
LOG.info("Resource " + src + " successfully copied into String (" + nLines + " lines copied).");
return sw.toString();
}
// ...
Run Code Online (Sandbox Code Playgroud)
注意:为了简化这篇文章,我编辑了我的原始代码。我希望我没有引入任何错误!
根据阿尔贝托的回答,如果您正在使用它,您可以将修改标头添加到 Firefox 配置文件中:
FirefoxDriver createFirefoxDriver() throws URISyntaxException, IOException {
FirefoxProfile profile = new FirefoxProfile();
URL url = this.getClass().getResource("/modify_headers-0.7.1.1-fx.xpi");
File modifyHeaders = modifyHeaders = new File(url.toURI());
profile.setEnableNativeEvents(false);
profile.addExtension(modifyHeaders);
profile.setPreference("modifyheaders.headers.count", 1);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", SOME_HEADER);
profile.setPreference("modifyheaders.headers.value0", "true");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
return new FirefoxDriver(capabilities);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17314 次 |
| 最近记录: |