0

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.

Javed Khan
  • 31
  • 10

1 Answers1

0

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);
    }
}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • It is just that I wanted to create a common step definition file for multiple features or scenarios. Your suggested solution helped in one way. Thanks – Javed Khan Apr 20 '20 at 08:12
  • @JavedKhan: Step definitions are global. They apply to all features. You cannot limit them to certain scenarios or feature files. – Greg Burghardt Apr 20 '20 at 11:06
  • yes. I tried a few more things and understood what you are telling. Thanks for all suggestions and guidance. – Javed Khan Apr 20 '20 at 14:16