1

I'm very new to using C#/Specflow to write tests. I have a test, that ends like this:

Then the Details page should display the rating zip code
    And the Details page should display the ID
    And the Details page should display the Eligible Employee count

And the code behind looks like this:

    [Then(@"the Details page should display the rating zip code")]
    public void ThenTheDetailsPageShouldDisplayTheRatingZipCode()

    {          
        string actualvalue = Driver.Instance.FindElement(By.CssSelector("div.GroupInfoRow:nth-child(3) > label:nth-child(2)")).Text;
        Assert.IsTrue(actualvalue.Equals(actualvalue));          
    }

    [Then(@"the Details page should display the ID")]
    public void ThenTheDetailsPageShouldDisplayTheID()
    {
        string actualvalue = Driver.Instance.FindElement(By.CssSelector("div.GroupInfoRow:nth-child(1) > label:nth-child(2)")).Text;
        Assert.IsTrue(actualvalue.Equals(actualvalue));
    }

    [Then(@"the Details page should display the Eligible Employee count")]
    public void ThenTheDetailsPageShouldDisplayTheEligibleEmployeeCount()
    {
        string actualvalue = Driver.Instance.FindElement(By.CssSelector("div.GroupInfoRow:nth-child(4) > label:nth-child(2)")).Text;
        Assert.IsTrue(actualvalue.Equals(actualvalue));
    }

I would rather it be displayed in a table format, but I'm not sure how to do it.

2
  • What do you mean with table format? Commented Apr 18, 2019 at 7:20
  • @NielsvanReijmersdal, I mean the following when I say table, a Specflow table (and my apologies this is not formatted properly when I save it): <p> Then the Details page should display the following: | field | value | | ZipCode | <zipcode> | | ID | <ID> | | EligibleEmpCount | <EligEmpCount> |</p>
    – Jo-Anne
    Commented Apr 18, 2019 at 16:53

2 Answers 2

2

Your specflow file should look like this:

Then the Details page should display the rating zip code
    And the Details page should display
    | Parameter       | Value |
    |  ID             | 5     |
    |  Employee Count | 125   |

And your code should look like this:

[Then(@"the Details page should display")]
public void DetailsPageShouldDisplay(Table table)
{
   List<TestParameter> detailsParameter = table.CreateSet<TestParameter>().ToList();
   // then iterate through detailsParameter by Name and do proper assertions
}

And your TestParameter class should look like this:

public class TestParameter
{
    [TableAliases("Parameter", "Field")]
    public string Name { get; set; } = "";

    public string Value { get; set; } = "";
}
0

Instead of using Assert, you can create your own Assert class (a singleton), where the method IsXXX call Assert.IsXXX, capture any possible Exception, save the message, and continue. In a @AfterScenario, you can call Assert.IsTrue(false, singletonObject.message) or do whatever you want with the Exceptions if you have any Exception capture there.

I am not a C# programmer, but the idea would be something like:

public void IsTrue(Boolean value, String message) { 
    try{
      Assert.IsTrue(value, message);
    } catch (AssertionError e) {
      this.HasFailed = true;
      this.ExceptionList.add(e); // or this.ExceptionList.add(e.message) or this.ExceptionList.add(message); 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.