Closed vickyRathee closed 6 years ago
That'd be a question for @Teun
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?"
Thanks, it works.
@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);
}
Yes, it seems you're right: the status code is not set correctly on WebExceptions. It is really odd that noone noticed before.
It wasn't coded in Browser.cs. I'm submitting a fix for that now.
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 ?