编辑:当我阅读问题时,没有看到 #2 作为中间 URL,它看起来像是(唯一的)重定向操作。根据您的browser of choice, and the type of redirect performed,您可以使用 Selenium 读取页面引用并获取重定向。
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
// Call any javascript
var referrer = js.executeScript("document.referrer");
我会推荐 Selenium Webdriver 来满足您在 C# 中的所有网站/应用程序测试需求。它与 NUnit、MSTest 和其他测试框架完美集成 - 非常易于使用。
使用 Selenium Webdriver,您将从您的 C# 测试代码启动一个自动浏览器实例(Firefox、Chrome、Internet Explorer、PhantomJS 等)。然后,您将使用简单的命令控制浏览器,例如“转到 url”或“在输入框中输入文本”或“单击按钮”。 See more in the API.
它也不需要其他开发人员太多 - 他们只运行测试套件,假设他们安装了浏览器,它就会工作。我已经成功地在一个开发人员团队中使用它进行了数百次测试,每个开发人员都有不同的浏览器偏好(即使是测试,我们每个人都对其进行了调整)和团队构建服务器。
对于这个测试,我会先go to the url in step 1,然后是wait for a second,然后在步骤 3 中读取 url。
这里是一些示例代码,改编自 Introducing the Selenium-WebDriver API by Example。由于我不知道您要查找的 URL 和 {string}(本例中为“奶酪”),因此示例没有太大变化。
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();
}
}