Specflow ignores When step in F# step assembly - f#

I am using VS2010, SpecFlow 1.9.0, NUnit 2.6.2 and ReSharper 7.1. I have a feature file taken from the example:
Feature: SpecFlowFeature1
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
#mytag
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press the add button
Then the result should be 120 on the screen
I have implemented the step definitions in a separate F# assembly:
[<TechTalk.SpecFlow.Binding>]
module StepDefinitions
open TechTalk.SpecFlow
open NUnit.Framework
let [<Given>] ``I have entered (.*) into the calculator`` (a: int) =
ScenarioContext.Current.Pending()
let [<When>] ``I press the add button`` =
ScenarioContext.Current.Pending()
let [<Then>] ``the result should be (.*) on the screen`` (r: int) =
ScenarioContext.Current.Pending()
I have told SpecFlow where to find them via the stepAssemblies tag in app.config
However, when I run the test, it finds the Given and Then steps, but not the When step. The error I get is:
No matching step definition found for one or more steps.
using System;
using TechTalk.SpecFlow;
namespace MyNamespace
{
[Binding]
public class StepDefinitions
{
[When(#"I press the add button")]
public void WhenIPressTheAddButton()
{
ScenarioContext.Current.Pending();
}
}
}
Given I have entered 50 into the calculator
-> pending: StepDefinitions.I have entered (.*) into the calculator(50)
And I have entered 70 into the calculator
-> skipped because of previous errors
When I press the add button
-> No matching step definition found for the step. Use the following code to create one:
[When(#"I press the add button")]
public void WhenIPressTheAddButton()
{
ScenarioContext.Current.Pending();
}
Then the result should be 120 on the screen
-> skipped because of previous errors
Have I gone wrong somewhere, or is there a bug with F# support?

The correct translation of the C# is actually
let [<When>] ``I press the add button``() =
ScenarioContext.Current.Pending()
Note the extra (). As this was missing in the original version, the function had a different signature which meant that it wasn't found.

Related

Facing issue using QAF common step og click and clear methods

Facing some weird issue when using QAF common steps. We just facing in “I click” and “I clear” methods so far.
Step to reproduce:
Step: And I click on "LOGIN_btn||rt.loc.e8R5BmyEUI"
#QAFTestStep(description = "I click on {0}")
public static void iClick(String locator) throws Exception {
click(locator);
}
Result: Fail
Issue: Unable to locate login button.
Here, Element locator is absolutely fine and if I change method name like “iClickOn” or “iClickABC” like below then its working fine as expected.
#QAFTestStep(description = "I click on {0}")
public static void iClickOn(String locator) throws Exception {
click(locator);
}
Result: Pass
Another approach is also working fine. If I removed “On” from step then its working fine.
Step: And I click "LOGIN_btn||rt.loc.e8R5BmyEUI"
#QAFTestStep(description = "I click {0}")
public static void iClick(String locator) throws Exception {
click(locator);
}
Result: Pass
Note: Same issue I'm facing in I clear locator as well.
Kindly check and confirm is there any restriction to use words in steps or what could be the problem here.
Check all entries of step.provider.pkg. Most probably the reason is you have another library specifying step with same name with same description and step loaded from it.
For example if you are using Perfecto-Quantum it has step with same name and description. The simplest way to find out is editor content-assist in IDE. You will see all available step definitions in content assist.

Specflow - How to pass on the data which has brackets to the step definition without any data loss

I have a specflow test which looks like this
When I create a team with <TeamName> <MemberName> <MemberMobileNumber> successfully
Examples:
|TeamName|MemberName|MemberMobileNumber|
|Apple(RD) |James(Smith)|(09)1234567|
If you observe, we have brackets in the test data details and when I read the data, the data is distorted. Is there a way to write the step definition in such a way that the data is not distorted.
The step definition looks like this
[When(#"I create a team with (.*) (.*) (.*) successfully")]
public void WhenICreateATeamWithTeam......{
}
Thanks a lot for your help on this.
Don't see any data loses in your example:
I also tried with Table and it works well.
I got data loses when I tried to make steps like this:
[When(#"I create a team with \(.*) \(.*) \(.*)")]
My version of SpecFlow is 2.4.0
For given specification in .feature file as you showed in question:
Scenario Outline: workWork
When I create a team with <TeamName> <MemberName> <MemberMobileNumber> successfully
Examples:
|TeamName |MemberName |MemberMobileNumber |
|Apple(RD) |James(Smith) |(09)1234567 |
And for step definition like:
[When(#"I create a team with (.*) (.*) (.*) successfully")]
public void WhenICreateATeamWithAppleRDJamesSmithSuccessfully(string p0, string p1, string p2)
{
}
Then data inside p0, p1 and p2 parameters in step is as expected: p0 = "Apple(RD)", p1 = "James(Smith)", p3 = "(09)1234567".
There is 2nd way to do this, using "" for textual data in .feature file, and then follow that with ""(.*)"" in regular expression (in method generated in step.cs file) for every input parameter.
Scenario Outline: workWork
When I create a team with <TeamName> <MemberName> <MemberMobileNumber> successfully
Examples:
|TeamName |MemberName |MemberMobileNumber |
|"Apple(RD)" |"James(Smith)" |"(09)1234567" |
[When(#"I create a team with ""(.*)"" ""(.*)"" ""(.*)"" successfully")]
public void WhenICreateATeamWithAppleRDJamesSmithSuccessfully(string p0, string p1, string p2)
{
}

No matching step binding found for this step exception

I've created a very simple feature file. When go to the definition by pressing F12. I get an error
No matching step binding found for this step.
Its being thrown for my When step.
Feature: ItemBag
Scenario Outline: Add item to bag until 200 is reached
Given I navigate to the homepage
And I search for trainers
When I add to the bag
Then trainers are added till 200 is reached
You need to create a Step file. It should be like this:
public class Steps
{
[Binding]
public void InavigateToTheHomepage()
{
this step implementation;
}
public void nextStep()
{
this step implementation;
}
}
This class should contain implementation for all of your steps. After that you shouldn't receive this kind of error.

Specflow calling steps within steps causes "No matching step definition" error

I am following the technique outlined here
using a step defined like
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
And("some action");
When("some result");
}
from a scenario like
Scenario: Some dependant scenario
Given some condition
And some base scenario has happened
When some other action
Then some other result
However the step
When some other condition
produces the following error
-> No matching step definition found for the step. Use the following code to create one:
[When(#"some other condition")]
public void Whensome other condition()
{
ScenarioContext.Current.Pending();
}
I can work around the problem by having the base scenario only use Given
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
Given"some action");
Given("some result");
}
however this is not what I should have to do.
Am I missing something?
Why cant the base scenario be called using an AND ?
In Specflow there are only 3 types of steps. Given, When and Then. When you use a step with And in your scenario description SpecFlow looks at the previous type of step and assumes that your And step is of the same type.
So when you write this
Scenario: Some dependant scenario
Given some base scenario has happened
And some other condition
When some other action
Then some other result
Specflow looks for step which have bindings:
Given("some base scenario has happened")
Given("some other condition")
When("some other action")
Then("some other result")
Notice there is no And binding?
So your solution is to to ensure that in your composite step you must avoid using And and just use the same binding (or one of them if they have multiple) that the original step had. Your final solution should look something like this:
[Given("some condition")]
public void SomeCondition()
{
...
}
[When("some action")]
public void SomeAction()
{
...
}
[Then("some result")]
public void SomeResult()
{
...
}
[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
Given("some condition");
When("some action");
Then("some result");
}
[Given("some other condition")]
public void SomeOtherCondition()
{
...
}
[When("some other action")]
public void SomeOtherAction()
{
...
}
[Then("some other result")]
public void SomeOtherResult()
{
...
}
You can't use And in the composite steps because no steps are actually bound with an And, there is no such binding - The only bindings are Given, When or Then. The And and But keywords are only used when generating the unit tests that are run, the steps using those keywords are still bound to a Given, When or Then step ultimately.
In a scenario definition the things are processed in order and you can easily tell what an And step actually is based on the step it appears after, so when specflow generates the step bindings it knows what step type to use (either a Given, When or Then). When you are calling a a step from within another step you are explicitly calling one of those step bindings and you have to call it with the binding that it is bound with. So if it was bound with a Given binding like this:
[Given("some other condition")]
public void SomeOtherCondition()
{
...
}
then you have to call it like this from the code:
Given("Some other condition");
but you could refer to it like this in a scenario:
Given some condition
And some other condition
because specflow knows when it generates the unit test that the And some other condition is actually calling a Given bound step
This question has been previously answered correctly above.
I just came across the same error "No matching step definition found for one or more steps".
The reason that I had this issue, was that I had forgotten to put the attribute [Binding, Scope(Feature = "My Feature")] just above my steps c# code class which links methods to feature file, which is needed to match "Feature: My Feature" at the top of my feature file.
I just taught that I would document it here to help someone else that was seeing the same error but for the different reason that I outlined.
Possible solutions
Use Given instead of And
Scenario: Some dependant scenario
Given some base scenario has happened
Given some other condition
When some other action
Then some other result
or
Tag the step with more than one binding, e.g.
[Given(#"some other condition")]
[When(#"some other condition")]
public void Whensome other condition()
{
but this won't always make semantic sense, so use this only when it really does make sense.
try verifying if your sentence has empty spaces. i.e: Given some description(empty space)
so, in the method will appear like: [Given("some description ")]

AssertionError instead of failure

i have developed a test package using SWTbot and ant to build it , when i run it, it finds that there is a failure however in the test report it shows as an error instead of failure:
my code is :
public static void Check_TargetPack(final SWTWorkbenchBot bot,String configuration,
String targetpack) {
boolean exist=false;
String[] h=bot.comboBoxWithLabel("TargetPack").items();
int i=0;
for (i=0;i<h.length;i++){
if (h[i]==targetpack)exist=true;
assertTrue("target pack"+targetpack+" doesn't exist in targetpack list",exist);
};
bot.sleep(2000);
bot.button("Close").click();
}
and the result is
I can see one problem in your code.
You are matching Strings with "==" operator.
You should use following instead
h[i].equals(targetpack)

Resources