问题描述
我正在尝试将标头添加到特定测试用例的 HTTP
请求中。这是非常重要的,因为我正在尝试测试一个用于移动电话的应用程序。我设法找到方法 addCustomRequestHeader(String arg0,String arg1)
。不幸的是,似乎我不知道如何正确使用它。
I'm trying to add headers into my HTTP
request for a particular test case. That's very important since I'm trying to test an application meant to be used in mobile phones. I manage to find out about the method addCustomRequestHeader(String arg0, String arg1)
. Unfortunately, it seems I don't know how to use it properly.
这是我的测试套件:
package testscripts;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TCNewspapers extends SeleneseTestCase {
//Local parameters
private static final String SELENIUM_SERVER_HOST = "localhost";
private static final int SELENIUM_SERVER_PORT = 4444;
private static final String NAVIGATOR = "*firefox";
private String URL = "http://www.marca.com/";
Selenium selenium;
@Before
public void setUp() throws Exception {
super.setUp();
selenium = new DefaultSelenium(SELENIUM_SERVER_HOST,
SELENIUM_SERVER_PORT,
NAVIGATOR,
URL);
setUp(URL, NAVIGATOR);
//selenium.start();
selenium.start("addCustomRequestHeader=true");
//selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true");
Thread.sleep(5000);
selenium.windowMaximize();
}
@Test
public void testOpenMarcaMobilePage() {
selenium.addCustomRequestHeader("user-agent", "Mozilla/5.0 (iPhone;");
selenium.open(URL);
selenium.waitForPageToLoad("300000");
verifyTrue(selenium.isTextPresent("Golf"));
}
@After
public void stopClient () throws Exception {
selenium.stop();
}
}
测试用例正在通过,但高尔夫字符串不应出现在页面的移动版本中。检查导航器时我看到我不在移动版本中。
The test case is passing, althoug the "Golf" string should not appear in the mobile version of the page. When checking the navigator I see that I'm not in the mobile version.
此外,我收到此警告:
addCustomRequestHeader 方法说:
我想这就是问题所在。我怎么能这样做呢?
I guess that's the problem. Any idea of how can I do this?
推荐答案
我开始的时间与你不同,但后来最终得到了和你一样的结果; 如何向selenium添加请求标头。我知道你发布原始问题已经有一段时间了,所以这适用于那些正在寻找Java答案的人。以下是我在 JAVA 中为您的selenium请求添加请求标头的一些代码:
I started out in a different place than you, but then ended up with the same result as you; "How do I add a request header to selenium". I know it's been a while since you posted your original question, so this is for anyone that is looking for the answer in Java. Here is some code I came up with to add request headers to your selenium request in JAVA:
public class MyTestCase extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost");
selenium.start("addCustomRequestHeaders=true");
selenium.open("/");
}
@Test
public void testMyTestCase() {
selenium.addCustomRequestHeader("HEADER_NAME", "HEADER_VALUE");
//header "HEADER_NAME", with "HEADER_VALUE" is now in your request
selenium.click("link=Hello World");
}
}
更新
您还必须使用'-addCustomRequestHeader'输入变量启动selenium服务器。例如:
UpdateYou will also have to start your selenium server with the ' -addCustomRequestHeader' input variable. For example:
%java_home%\bin\java -jar selenium-server-standalone-2.25.0.jar -addCustomRequestHeader
这篇关于如何正确使用addCustomRequestHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!