Search code examples
c#specflowwinappdriverfeature-file

Specflow: Replace Name(pass string from feature file) to AccessibilityId in step definition file


I am new to Specflow. My framework is with C#. Feature file:

Feature: test ABC app

Scenario: 00 Application is open 
    Given Application is open in "User" mode
    When the "Configuration" screen is open

Scenario: 02 Search for servers
    Given the "Add Server" screen is open
    And "Request Server" button is clicked
    When the "Request serer" screen is open

In the step definition file the function is like:

 [When(@"the ""(.*)"" screen is open")]
 [Given(@"the ""(.*)"" screen is open")]
 public void GivenScreenIsOpen(string element)
 {
   element_Interactions.ClickOnElement(element);
 }

Solution needed: From the feature file, I am passing a string with a screen name as a variable but in the step definition file, instead of using driver.FindElementByName(element) i want to use driver.FindElementByAccessibilityId(element). I am not able to get a workaround on how to use/call AccessibilityId for the appropriate screen name from my page class in the step definition function and how to dynamically use same for all other screens

thanks in advance.


Solution

  • I'm still not 100% sure what you are dealing with, but it seems like a dictionary of screens and accessibility Ids would be the simplest solution:

    [Binding]
    public class YourSteps
    {
        private static readonly Dictionary<string, string> accessibilityIds = new Dictionary<string, string>()
        {
            { "screen1", "accessibility-id-"},
            { "screen2", "accessibility-id-2"},
            { "screen3", "accessibility-id-3"}
        };
    
        [When(@"the ""(.*)"" screen is open")]
        [Given(@"the ""(.*)"" screen is open")]
        public void GivenScreenIsOpen(string element)
        {
            var accessibilityId = accessbilityIds[element];
    
            // Or however you click on an element by its accessibility Id
            element_Interactions.ClickOnElement(accessibilityId);
        }
    }