SimpleBrowserDotNet / SimpleBrowser.WebDriver

A webdriver for SimpleBrowser
Apache License 2.0
43 stars 16 forks source link

HTTP Headers, Proxy and HTTP status code #33

Closed vickyRathee closed 6 years ago

vickyRathee commented 8 years ago

How can i set user-agent header, proxy using SimpleBrowser.WebDriver? And also, how to get HTTP status code of request. e.g 200, 404 etc ?

axefrog commented 8 years ago

That'd be a question for @Teun

Teun commented 8 years ago

Right. It is.

Normally, you'd create an instance of the WebDriver doing

IWebDriver browser = new SimpleBrowserDriver();

This will instantiate a SimpleBrowser instance for you, but you cannot access this browser instance anymore. The IWebDriver interface doesn't allow you to do things beyond "user interactions". But. You can pass in your own instance of SimpleBrowser. The constructor expects a browser implementing IBrowser (this is for testability), so you have to wrap it. A simple wrapper is available. So what you do is this:

// create SimpleBrowser
var browser = new new Browser();
// attach event handler to look into the exact requests later
browser.RequestLogged += new Action<Browser, HttpRequestLog>(HandleRequestLogged);

// wrap and create driver
IBrowser wrapped = new BrowserWrapper(browser);
IWebDriver dr = new SimpleBrowserDriver(wrapped);

So this reduces your question to "how do I set proxy settings, extra headers on SimpleBrowser?"

vickyRathee commented 8 years ago

Thanks, it works.

vickyRathee commented 8 years ago

@Teun Sorry, would need to reopen this.

The HTTP ResponseCode for errored request is always 0

        var browser = new Browser();
        browser.RequestLogged += new Action<Browser, HttpRequestLog>(HandleRequestLogged);
        IBrowser wrapped = new BrowserWrapper(browser);
        IWebDriver driver = new SimpleBrowserDriver(wrapped);

        string[] urlList = new string[3];
        urlList[0] = "http://www.pricetree.com/faq.aspx";
        urlList[1] = "http://www.pricetree.com/bad-page-url.aspx"; // Should return 404 not found
        urlList[2] = "http://www.pricetree.com/contact.aspx";

        foreach (string url in urlList)
        {
            driver.Navigate().GoToUrl(url);
        }

    public static void HandleRequestLogged(Browser req, HttpRequestLog log)
    {
        Console.WriteLine(log.Url);
        Console.WriteLine(log.ResponseCode);
    }

http-bad-error

Teun commented 8 years ago

Yes, it seems you're right: the status code is not set correctly on WebExceptions. It is really odd that noone noticed before.

kevingy commented 8 years ago

It wasn't coded in Browser.cs. I'm submitting a fix for that now.