Reputation: 531
Short version:
I am looking to make sure that a URL (partial match) is requested (client-side).
Long Version:
I am looking to automate part of my testing. Currently I use Fiddler2 to manually verify.
Here's the scenario:
I would like to verify, in C#, that step 2 happened by doing a partial match (contains {string} for example).
Question:
How should I go about this? I have started looking into HttpWebRequest
class and FiddlerCore
, but my love using the simplest code possible (so other team members to update if needed) lead me to ask what the users of StackOverflow would recommend.
Upvotes: 5
Views: 14723
Reputation: 531
Follow Up: I ended up using Telerik's proxy to send HTTP Requests and parse the responces via C#. Here's the article that was used as a springboard:
https://docs.telerik.com/teststudio/advanced-topics/coded-samples/general/using-the-http-proxy
Upvotes: 0
Reputation: 25087
Edit: Didn't see #2 as an intermediate URL when I read the question, it looked like it was the (only) redirect action. Depending on your browser of choice, and the type of redirect performed, you can use Selenium to read the page referrer and get the redirect.
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
// Call any javascript
var referrer = js.executeScript("document.referrer");
I would recommend Selenium Webdriver for all your web site/app testing needs in C#. It integrates very nicely with NUnit, MSTest and other test frameworks - it's very easy to use.
With Selenium Webdriver, you will start an automated browser instance (Firefox, Chrome, Internet Explorer, PhantomJS and others) from your C# testing code. You will then control the browser with simple commands, like "go to url" or "enter text in input box" or "click button". See more in the API.
It doesn't require much from other developers either - they just run the test suite, and assuming they have the browser installed, it will work. I've used it successfully with hundreds of tests across a team of developers who each had different browser preferences (even for the testing, which we each tweaked) and on the team build server.
For this test, I would go to the url in step 1, then wait for a second, and read the url in step 3.
Here is some sample code, adapated from Introducing the Selenium-WebDriver API by Example. Since I don't know the URL nor {string}
("cheese" in this example) you are looking for, the sample hasn't changed much.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class RedirectThenReadUrl
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Print the original URL
System.Console.WriteLine("Page url is: " + driver.Url);
// @kirbycope: In your case, the redirect happens here - you just have
// to wait for the new page to load before reading the new values
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Url.ToLower().Contains("cheese"); });
// Print the redirected URL
System.Console.WriteLine("Page url is: " + driver.Url);
//Close the browser
driver.Quit();
}
}
Upvotes: 1
Reputation: 46728
Sounds like you want to sniff HTTP traffic. You could use a packet capture driver like winpcap, import that DLL and test, or use SharpPcap that @SimpleCoder mentioned.
The path of minimum effort would be write a FiddlerScript Addon, to check the request and redirect if necessary.
Upvotes: 0
Reputation: 29658
Take a look at SharpPcap. It's based on pcap (WinPcap on Windows), which is the packet capture library that is used by the popular Wireshark.
There is a really great tutorial on CodeProject with lots of example code to get you started: http://www.codeproject.com/Articles/12458/SharpPcap-A-Packet-Capture-Framework-for-NET
Once you have a hold of the packets (SharpPcap does capture, not parsing), you can use Packet.Net to parse the packets into something usable (HTTP communications, in your case).
Upvotes: 7