如何正确使用 addCustomRequestHeader

发布于 2024-10-07 06:49:23 字数 1961 浏览 3 评论 0原文

我正在尝试将标头添加到特定测试用例的 HTTP 请求中。这非常重要,因为我正在尝试测试要在手机中使用的应用程序。我设法找到了有关方法 addCustomRequestHeader(String arg0, String 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) 看到错误结果,正常

addCustomRequestHeader 方法的文档说:

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

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

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.

This is my Test Suite:

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.

Also, I'm getting this WARNING:

WARNING: getString(addCustomRequestHeader) saw a bad result OK

The documentation of addCustomRequestHeader method says:

This only works if the browser is configured to use the built in Selenium proxy

I guess that's the problem. Any idea of how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2024-10-14 06:49:23

我的出发点和你不同,但最终却得到了和你一样的结果; “如何向 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

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

}

Update
You 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
最笨的告白 2024-10-14 06:49:23

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

http://mogotest.com/blog/2010/06/23/how-to-perform-b​​asic-auth-in-selenium

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

You need to make sure that you're using Selenium as a proxy server. I've written up an article on how to use addCustomRequestHeader in order to support basic authentication. You should be able to extrapolate the relevant portions (note I used Ruby, but it maps to other languages just as well):

http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium

One thing you very much need to be aware of is that there is no way to remove the request header. It's always additive and it's added for every request. If you need to use different headers, you'll need to restart the Selenium server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文