0

我是 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.FindElementByAccessibilityId(element)我无法解决如何在步骤定义函数中从我的页面类中为适当的屏幕名称使用/调用 AccessibilityId 以及如何为所有其他屏幕动态使用相同的屏幕名称

提前致谢。

4

1 回答 1

0

我仍然不能 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);
    }
}
于 2020-04-14T12:09:20.350 回答