Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确使用addCustomRequestHeader_Java_Selenium_Selenium Rc - Fatal编程技术网

Java 如何正确使用addCustomRequestHeader

Java 如何正确使用addCustomRequestHeader,java,selenium,selenium-rc,Java,Selenium,Selenium Rc,我正在尝试为特定测试用例向我的HTTP请求添加头。这一点非常重要,因为我正在尝试测试一个用于移动电话的应用程序。我设法找到了方法addCustomRequestHeader(字符串arg0,字符串arg1)。不幸的是,我似乎不知道如何正确使用它 这是我的测试套件: package testscripts; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.SeleneseTest

我正在尝试为特定测试用例向我的
HTTP
请求添加头。这一点非常重要,因为我正在尝试测试一个用于移动电话的应用程序。我设法找到了方法
addCustomRequestHeader(字符串arg0,字符串arg1)
。不幸的是,我似乎不知道如何正确使用它

这是我的测试套件:

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();
    }

}
测试用例通过了,尽管“Golf”字符串不应该出现在页面的移动版本中。当检查导航器时,我发现我不在移动版本中

此外,我收到了以下警告:

警告:getString(addCustomRequestHeader)看到错误的结果OK

addCustomRequestHeader
方法的文档说明:

这仅在浏览器配置为使用内置Selenium代理时有效


我想这就是问题所在。知道我该怎么做吗?

您需要确保使用Selenium作为代理服务器。我写了一篇关于如何使用addCustomRequestHeader来支持基本身份验证的文章。您应该能够推断相关部分(注意,我使用了Ruby,但它也映射到其他语言):


您非常需要注意的一件事是,无法删除请求头。它始终是可添加的,并且为每个请求添加。如果需要使用不同的头文件,则需要重新启动Selenium服务器。

我开始的位置与您不同,但最终的结果与您相同;“如何向selenium添加请求头”。我知道你已经有一段时间没有发布你的原始问题了,所以这是为那些正在寻找Java答案的人准备的。下面是我在JAVA中为selenium请求添加请求头的一些代码:

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服务器。例如:

%java_home%\bin\java -jar selenium-server-standalone-2.25.0.jar -addCustomRequestHeader

谢谢你的回答。我所做的是:将我的Firefox设置为通过Selenium服务器(localhost:4444)连接到internet,然后,因为在我的办公室我通过代理连接到internet,所以我使用选项-Dhttp.proxyHost=myProxy-Dhttp.proxyPort=8080启动了我的Selenium服务器。尽管它正在工作,但我仍然收到以下警告:警告:getString(addCustomRequestHeader)看到了坏结果,我不熟悉警告信息。但是,您运行的是什么版本的Selenium?我在2.0a5或2.0a6中修复了一些请求头内容。这已经在Selenium RC 2.17a上进行了测试