我在枚举中有一个网址列表。我想将枚举值作为参数传递给我的方法,并使用它来浏览使用Selenium Web驱动程序的URL。
错误CS0029无法隐式转换类型' string' to' automation.Enums.UrlEnums.Url'自动化F:\ Projects \ C#\ automation \ Helpers \ NavigateHelper.cs 22活动
我的代码段是:
public class UrlEnums
{
public enum Url
{
[Description("https://www.companysite1.com")]
company1,
[Description("https://www.companysite2.com")]
company2
}
}
public void NavigateTo(UrlEnums.Url url)
{
switch (url)
{
case "Thriller":
Driver.Navigate().GoToUrl(url);
//Driver.Navigate().GoToUrl("https://www.companysite1.com");
break;
case "Comedy":
Driver.Navigate().GoToUrl(url);
break;
}
}
如何使用枚举作为参数传递并转到URL? 此枚举列表将会增长并且很快会有更多网址。
我的SpecFlow场景是:
Feature: WelcomePage
In order to view the film categories
As a user I should be able to click on a category
I should see the welcome page for the selected category
@smoke
Scenario Outline: NavigateToFilmCategory
Given I navigate to <Category> page
Then I should see the welcome page <WelcomePage>
Examples: TestData
| Category | WelcomePage |
| Thriller | Thriller Films |
| Comedy | Comedy Films |
| Action | Action Films |
| Adventure | Adventure Films |
谢谢,
答案 0 :(得分:1)
在这种情况下,不需要使用switch case,因为您对不同的值执行相同的操作。如果您使用反射来查找网址值,则可以对该特定网址执行操作(导航)。
我希望这有助于解决您的问题。
public enum UrlEnum
{
[Description("https://www.companysite1.com")]
company1,
[Description("https://www.companysite2.com")]
company2
}
public void NavigateTo(UrlEnum url)
{
MemberInfo info = typeof(UrlEnum).GetMember(url.ToString()).First();
DescriptionAttribute description = info.GetCustomAttribute<DescriptionAttribute>();
if (description != null)
{
//do something like
Driver.Navigate().GoToUrl(description.Description);
}
else
{
//do something
}
}
答案 1 :(得分:0)