Wiki

Clone wiki

WebAutomation / SpecFlowGenericSteps

WebAutomation.SpecFlowGenericSteps

Package provides a ready to use SpecFlow steps for all commonly used actions based on WebAutomation framework. With generic steps, you can:

Perform an action on WebComponent

  • Check / Uncheck
  • Click
  • Clear
  • Fill with text
  • Select an option in combobox
  • Hover

Validate the state of WebComponent

  • Present / Not Present (in DOM)
  • Displayed / Not Displayed
  • Checked / Unchecked
  • Enabled / Disabled

Validate the value of WebComponent

  • text
  • text length
  • selection (combobox)
  • CSS
  • Html attributes

Installation

To install WebAutomation.SpecFlowGenericSteps, run the following command in the Package Manager Console

PM> Install-Package WebAutomation.SpecFlowGenericSteps
https://www.nuget.org/packages/WebAutomation.SpecFlowGenericSteps/

Sample test

To create a sample test which uses WebAutomation.SpecFlowGenericSteps, create following files:

  • Youtube.xml (sources available here)

  • Youtube.tt (sources available here)

  • YoutubeTests.feature

    Scenario: Open a video
        # Custom step
        Given Web browser is opened
    
        # Generic steps
        And user navigates to 'http://www.youtube.com'
        When user fills 'YoutubePage-SearchInput' with 'Hans Zimmer greatest hits'
        And user clicks 'YoutubePage-OkButton'
        Then the 'YoutubeSearchResultPage-Link' will be displayed
            | LinkText                           |
            | The greatest hits from Hans Zimmer |
        When user clicks 'YoutubeSearchResultPage-Link'
            | LinkText                           |
            | The greatest hits from Hans Zimmer |
        Then the 'YoutubePlayerPage-Player' will be displayed
        Given user closes the web browser
    

  • CustomSteps.cs

    using OpenQA.Selenium.Firefox;
    using System;
    using TechTalk.SpecFlow;
    using WebAutomation.SpecflowGenericSteps;
    
    [Binding]
    public class CustomSteps : SpecFlowTestBase
    {
        public CustomSteps(ScenarioContext scenarioContext)
           : base(scenarioContext)
        {
        }
    
        [Given(@"Web browser is opened")]
        public void GivenWebBrowserIsOpened()
        {
            this.WebDriver = new FirefoxDriver();
            this.WebDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));
        }
    }
    

  • SpecFlowGenericSteps.cs - already provided by the framework

Tips & tricks

How to validate a table content

Let's assume that we have a simple table on a page, and we want to validate content of that table.

<table>
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1 C 1</td>
            <td>Row 1 C 2</td>
        </tr>
        <tr>
            <td>Row 2 C 1</td>
            <td>Row 2 C 2</td>
        </tr>

    </tbody>
</table>

Based on above example, all we have to do is to add a new entry in our XML file that will represent a single row. To do so, let's use a parameterized xpath

<container name="SampleWebSite">
  <component name="TableRow" pxpath="//table/tbody/tr[./td[1][text()='{0}']][./td[2][text()='{1}']]" />
</container>
  • Parameter {0} represents the text content of the first cell in a row.
  • Parameter {1} represents the text content of the second cell in a row.

Next you can use generic step (with table) to check whether an element is displayed

Scenario: : Use generic steps to check a table content
    # Some other steps [..]
    Then the 'SampleWebSite-TableRow' is displayed
        | Header 1  | Header 2  |
        | Row 1 C 1 | Row 1 C 2 |
        | Row 2 C 1 | Row 2 C 2 |

And that's all! You don't have to write any additional C# code :)

Please remember that each parameter corresponds to a column in the table. Number of rows however indicates how many times the given step will be executed (for different values).

Updated