我在不同的机器上使用 Selenium 来自动测试 MVC Web 应用程序。
我的问题是我无法获取每台机器的基本网址。
我可以使用以下代码获取当前网址:
IWebDriver driver = new FirefoxDriver();
string currentUrl = driver.Url;
但当我需要导航到不同的页面时,这并没有帮助。
理想情况下,我可以使用以下内容导航到不同的页面:
driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
driver.Navigate().GoToUrl(baseUrl+ "/Home");
我使用的一个可能的解决方法是:
string baseUrl = currentUrl.Remove(22); //remove everything from the current url but the base url
driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
我有更好的方法可以做到这一点吗?
解决这个问题的最佳方法是创建一个Uri
URL 的实例。
这是因为Uri
class http://msdn.microsoft.com/en-us/library/System.Uri.aspx.NET 中已经有代码可以完全为您执行此操作,因此您应该使用它。我会选择类似的东西(未经测试的代码):
string url = driver.Url; // get the current URL (full)
Uri currentUri = new Uri(url); // create a Uri instance of it
string baseUrl = currentUri.Authority; // just get the "base" bit of the URL
driver.Navigate().GoToUrl(baseUrl + "/Feedback");
本质上,你是在追求权限属性 http://msdn.microsoft.com/en-us/library/system.uri.authority.aspx内Uri
class.
请注意,有一个属性可以做类似的事情,称为Host http://msdn.microsoft.com/en-us/library/system.uri.host.aspx但这不包括端口号,而您的站点则包括端口号。不过,这是需要牢记的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)