TechTalk.SpecFlow.BindingException:为步骤

问题描述

我有2个功能使用相同的“给定”步骤并传递用户名Given I navigate to the HomePage with the <user>

当我尝试运行测试时,我得到以下信息:

TechTalk.SpecFlow.BindingException: 'Ambiguous step deFinitions found for step 'Given I navigate to the SourceForge HomePage with the user1

功能1:

Feature: UploadProjectFileFeature
    In order to to be to add project documentation
    I want to be able to upload project file(s) to the correct project repository
@mytag
Scenario: Upload Project File(S) to the correct project repository
    Given I navigate to the HomePage with the <user>
    And I Log into the application with <username> and <password>
    When I navigate to the <project> page and I upload File(s)
    Then the file should upload successfully

    Examples:
        | user  | username | password     | project         |
        | user1 | username | Passtest.123 | TestAutomationP |

功能2:

Feature: UserLogin
    In order to access my account
    As a user of the website
   I want to log into the website

@mytag1
Scenario: Successful Login with Correct user
    Given I navigate to the HomePage with the <user>
    And I Navigate to the LoginPage
    When I Log into the application with <username> and <password>
    And I Navigate to the Profile Page
    Then <user> and <username> should be displayed correctly


    Examples:
    | user  | username| password     |
    | user1| username| Passtest.123 |

我试图创建一个基类,然后使用标签,虚拟和覆盖作为替代方法,但是我仍然遇到相同的错误

BaseClass:

  public  class FeatureBaseClass
    {
        
        [Given(@"I navigate to the HomePage with (.*)")]
        public virtual void GivenINavigatetoTheSourceForgeHomePageWith(string user)
        {
   
            string User = user;
        }

测试:

    [Binding]
  [Scope(Feature = "UserLogin")]
    public class UserLogin : FeatureBaseClass
    {
        
       
        [Given(@"I navigate to the HomePage with (.*)")]
    [Scope(Feature = "UserLogin",Scenario = "Successful Login with Correct user")]
    public override void GivenINavigatetoTheSourceForgeHomePageWith(string user)
        {
            Console.WriteLine("Launched browser with " + user);
 
        }
    [Binding]
   [Scope(Feature = "UploadProjectFileFeature")]
    public class UploadProjectFile : FeatureBaseClass
    {
      
        [Given(@"I navigate to the HomePage with (.*)")]
        [Scope(Feature = "UploadProjectFileFeature",Scenario = "Upload Project File(S) to the correct project repository")]
        public  override void GivenINavigatetoTheSourceForgeHomePageWith(string user)
        {
            Console.WriteLine("Launched browser with " + user);
            
 
        }

有人可以看到我要去哪里了吗?还是有人可以提出更好的解决方案?

解决方法

“歧义步骤定义”意味着您具有2个或多个具有相同定义的步骤,因此specflow不知道您要运行哪个步骤。每个步骤都必须是唯一的。在您的情况下,您有2个相同的Given语句,如下所示:

 [Given(@"I navigate to the HomePage with (.*)")]

在两个不同的类中。删除其中之一,它应该可以运行。