how-to use addCustomRequestHeader properly
Asked Answered
C

2

4

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?

Cavallaro answered 14/12, 2010 at 17:56 Comment(0)
C
4

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
Coldblooded answered 14/3, 2012 at 20:15 Comment(1)
This has been tested on Selenium RC 2.17aColdblooded
E
1

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.

Elecampane answered 21/12, 2010 at 14:19 Comment(3)
Thanks for your answer nivdrum. What I did was: Set my Firefox to connect to internet via Selenium Server (localhost:4444) and then, since in my office I connect to the internet via proxy, I started my Selenium Server with the options -Dhttp.proxyHost=myProxy -Dhttp.proxyPort=8080.Cavallaro
Although it is working I'm still getting this WARNING: WARNING: getString(addCustomRequestHeader) saw a bad result OKCavallaro
I'm not familiar with the warning message. But, what version of Selenium are you running? I fixed some of the request header stuff in 2.0a5 or 2.0a6.Elecampane

© 2022 - 2024 — McMap. All rights reserved.