-1

I have a list of URLs in an Enum. I would like to pass the enum value as a parameter to my method and use it to browse to the URL using Selenium Web driver.

Error CS0029 Cannot implicitly convert type 'string' to 'automation.Enums.UrlEnums.Url' automation F:\Projects\C#\automation\Helpers\NavigateHelper.cs 22 Active

My code snippet is:

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;
    }
}

How do I use the enum to pass as a parameter and go to the URL? This list of Enums will grow and have more URLs soon.

My SpecFlow Scenario is:

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 |

Thanks,

OptimusAutomation
  • 141
  • 1
  • 6
  • 15
  • You are comparing a `Url` enum to a string in your switch. Compare the `Url` to a `Url` and your error will go away – maccettura Mar 27 '18 at 20:17
  • 2
    I do not understand what "Thriller" and "Comedy" have to do with your enum. – M Bakardzhiev Mar 27 '18 at 20:20
  • 1
    Your code also makes no sense. Your switch statement does nothing different between cases. Plus you are checking for "Thriller" and "Comedy" but neither of your example `Url` enums contain anything related to that. You need to edit your question and provide a [MCVE] because its unanswerable in this state – maccettura Mar 27 '18 at 20:21
  • I think the user lacks understanding of enums. If you want a switch on the enum, you'd want to actually use it. IE case company1 – Trey Mar 27 '18 at 20:37
  • From my specflow scenario the step Given I navigate to category, category will be passed as a parameter. If category is Thriller then go to the respective url else if category is Comedy then go to the comedy url – OptimusAutomation Mar 30 '18 at 21:39

2 Answers2

1

In this case there is no need to use switch case because, you perform the same operation for different values. If you use reflection for finding url values, you can execute operation (navigation) over that spesific urls.

I hope this helps your problem.

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
    }
}
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
0

A good solution would be something like this:

public class UrlEnums
{
    public enum Url
    {
        [Description("https://www.companysite1.com")] 
        company1,
        [Description("https://www.companysite2.com")] 
        company2
    }
}

public void NavigateTo(UrlEnums.Url url)
{

     Driver.Navigate().GoToUrl(GetEnumDescription(url));

}

    public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }

The GetEnumerationDescription i found here

That was what your looking for or?

  • In the if statement at the line return attributes[0].Description I am getting the error: Error CS1061 'DescriptionAttribute' does not contain a definition for 'Description' and no extension method 'Description' accepting a first argument of type 'DescriptionAttribute' could be found (are you missing a using directive or an assembly reference?) – OptimusAutomation Mar 31 '18 at 17:56
  • use: using System.ComponentModel; – DominikM Mar 31 '18 at 18:39