Specflow:将名称(从功能文件传递字符串)替换为步骤定义文件中的 AccessibilityId

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

我是 Specflow 的新手。我的框架使用 C#。 特征文件:

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

在步骤定义文件中,函数如下:

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

需要的解决方案: 从功能文件中,我传递了一个带有屏幕名称的字符串作为变量,但在步骤定义文件中,我想使用 driver.FindElementByName(element) 而不是 driver.FindElementByName(element) =20=]driver.FindElementByAccessibilityId(元素)。 我无法获得有关如何从我的页面 class 在步骤定义函数中 use/call 相应屏幕名称的 AccessibilityId 以及如何为所有其他屏幕动态使用相同功能的解决方法

提前致谢。

我仍然不能 100% 确定您正在处理什么,但看起来屏幕和辅助功能 ID 的字典将是最简单的解决方案:

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