Reading datatable value of one feature from another feature in specflow - background

i have a feature called as create experiment in feature1, i am passing the data that is needed to create experiment as Background section of feature file 1.
i want to access the data i am passing from feature file1 into feature file 2 as precondition. before executing featurefile2 i want to make sure the experiment is present with the data given in feature file1.
i am using a common class to collect all the data used in feature file 1 instead of storing them in stepdefination .
following the example given in this link to achieve this. https://docs.specflow.org/projects/specflow/en/latest/Bindings/Context-Injection.html
The problem here is, when i run feature file2 alone it does not get the data, since to data is filled during execution of feature file 1.
How to read the data of featurefile1 without executing featurefile1.
how to solve this .

Depending on the state of another test is generally not the idea on how to do BDD tests, or any unit/integration tests in general, since they are supposed to be self-contained. Understandably there won't be any support in SpecFlow on how to do this.
The easiest solution I can think of is to either copy the step definitions to your second feature file, which then calls the same code again. You don't have to copy the test implementation, because all steps are global in SpecFlow by default.
Another solution would be to add a background to the second feature file where you simply call the methods of the first feature file.

Related

How to link Feature File to multiple step defintion files in Python BDD

I am developing a framework for automation using pytest-bdd based framework. Based on functionality I have multiple feature files and multiple step defintion files. Some scenarios take steps from other step definition files.
For example I have a Login Module , User Details Module. Now for validation of a step in User Module I do have to start with steps from the Login Module.
However in python bdd, I could see a one to one mapping of feature and step definition file.
Please let me know if this a limitation of pytest bdd framework .
Yes as far as i have worked with pytest bdd, you can only map one step definition to a single feature file, but there are work arounds to these.
1.Use conftest to keep all your common steps that you want to call across multiple feature files.
2.Use methods to be called into other step definitions by importing those methods into other step definitions.
I have a similar experience and i realized that if i don't use 1:1 mapping of feature to step definition file then it results in step_def not found errors
e.g.pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found:
So, I stick with the safe approach of 1:1 mapping
Would like to hear more thoughts feedback on this

Cucumber Gherkin: Is there a way to have your gherkin features written and managed in excel sheets instead of .feature files in IntelliJ or eclipse?

Cucumber Gherkin: Is there a way to have your gherkin scenarios written and managed in excel sheets instead of .feature files in IntelliJ or Eclipse like in SpecFlow+Excel(screenshot given as link below)? I am using Cucumber-JVM with selenium for my automation framework.
Excel based Scenarios
PS: Will there be any pros or cons to using excel sheets as your feature files?
No, Gherkin is the language understood by Cucumber.
If you want to introduce Excel in the equation, you probably want to use some other tool. Or implement your own functionality that reads Excel and does something interesting based on the content.
You could write a compiler that takes .csv files and translates them into feature files by doing a write.
Here's a quick thing I whipped up in JS that does just that.
First it removes blank lines, then searches through for keywords with a missing colon (it might happen somewhere down the road) and adds those in, checks for an examples table, and as per your photo, this was easy to do, as it was just checking whether the start character was a comma (after having removed the blank lines, and keywords always being in the first section). Finally, removing the rest of the commas.
The only difference, I believe, is that mine included the Feature: and Scenario:/Scenario Outline line that is needed to create a valid scenario in cucumber.
So yes. It is possible. You'll just have to compile the csv's into feature files first.
function constructFeature(csvData) {
let data = csvData,
blankLineRegex = /^,+$/gm,
keywordsWithCommasRegex = /(Feature|Scenario|Scenario Outline|Ability|Business Need|Examples|Background),/gm,
examplesTableRegex = /^,.*$/gm;
data = data
.toString()
.replace(blankLineRegex, "");
data.match(keywordsWithCommasRegex).forEach((match) => {
data = data.replace(match, match.replace(',', ":,"))
});
data.match(examplesTableRegex).forEach((match) => {
data = data.replace(match, match.replace(/,/g, "|").replace(/$/, "|"))
});
data = data.replace(/,/g, " ");
data = data.replace(/\ +/g, " ");
console.log(data);
}
let featureToBuild = `Feature,I should be able to eat apples,,
,,,
Scenario Outline,I eat my apples,,
Given,I have ,<start>,apples
When,I eat,<eat>,apples
Then,I should have,<end>,apples
,,,
Examples,,,
,start,eat,end
,4,2,2
,3,2,1
,4,3,1`
constructFeature(featureToBuild);
Just take that output and shove it into an aptly named feature file.
And here's the original excel document:
The downside of using Excel is that you'll be using Excel.
The upside of using feature files as part of your project are:
* they are under version control (assuming you use git or similar)
* you IDE with Cucumber plugin can help you:
- For instance, IntelliJ (which has a free Community edition) will highlight any steps that have not been implemented yet, or suggest steps that have been implemented
- You can also generate step definitions from your feature file
* when running tests you will see if and where your feature file violates Gherkin syntax
TL;DR: Having the feature file with your code base will make it easier to write and implement scenarios!
Also, like #Thomas Sundberg said, Cucumber cannot read Excel.
Cucumber can only process the feature files, so you will have to copy your scenarios from Excel to feature files at some point.
So what you are asking is not possible with Cucumber.
If you want to read test cases from Excel you'll have to build your own data driven tests. You could for instance use Apache POI.
There's a solution out there which does scenarios and data only in Excel. However, this I have tried in conjunction with Cucumber and not with any test application. You could try and see if you want to use this:
Acceptance Tests Excel Addin (for Gherkin)
However, not that Excel as a workbook or multiple workbooks can be source controlled, but it is hard to maintain revisions.
But then Excel provides you flexibility with managing data. You must also think not duplicating your test information and data in multiple places. So check this out, may be helpful.

Testing with Cucumber and Selenium:How can i break the sequence of feature file and run in random sequence

I have tried many option but any of them not work as i need. I have created some feature file as i required time by time. so i have a disorder feature file.
I want to run the feature file as i wanted. like.
fil1.feature
fil2.feature
fil3.feature
fil4.feature
so i want to run in this sequence : file3.feature->fil4.feature->fil1.feature.
I have tried #tag, #feature in junit test runner option but its maintain the sequence its run only 3,4 but can't run 1.
So can you tell me how to run feature file randomly???
Cucumber picks up the feature files in alphabetic order from the folder given in the features parameter of the CucumberOptions. So one options would be to rename your feature files alphabetically in the order you want.
After the feature files in the initial folder are read, then the sub folders in the location are picked up alphabetically and the feature files inside them are read. So you can place the file you want to be used later into a sub-folder.
Saying all this, it is not a very good idea to have any dependency between tests which requires a sequence to be maintained.
I got the same stop. Have you solved this problem?
I did some test, use --tags #XXX to controll the sequence is useless, it's just action on different scenarios in a feature file.
so far, I have to do it like this cucumber features/c.feature features/a.feature features/b.feature I think this is a little bit better than rename feature files.
But you may say that: "If there is a lot of feature files......" In my project(ROR), I assigned the statement to ./config/cucumber.yml
In cucumber.yml I define a profile, test_dev: features/c.feature features/a.feature features/b.feature after that, I just to use cucumber -p test_dev and the feature files execute in order.
If you have a butter way, please share with us.

Share backgrounds between Cucumber files?

I have some Cucumber scenarios, for which I created the following files:
create_extended_search.feature
activate_extended_search.feature
edit_extended_search.feature
delete_extended_search.feature
Within these files, I have several scenarios.
Three of the files use the same background, and it would be nice to be able to place it into one file (e.g. support/backgrounds.rb) and then reference it from the feature files.
Is this possible somehow? Thanks.
I believe you would have to create a step that is made up of the steps in your current background. Then call that step in the background for each feature.
There's no notion of 'include'ing feature files in Cucumber. As Justin points out, you can create a single step representing what you want as a background, and call that where appropriate. An alternative is to use a Before hook to perform certain tasks in advance of scenarios that you mark with a specific tag.
Personally, I'd treat this problem as something of a red flag, and start asking if my feature files were split up in the best way possible. Frequently if I find myself bemoaning the inability to include other feature files, or conversely, wishing I could exclude certain scenarios from running my background, it's a very strong sign that my feature files are too finely sliced up, or I'm trying to cram unrelated functionality together and need to split it up further.

CDash Custom Dynamic Analysis

I'm trying to integrate custom dynamic analysis tools to CDash. Such as KWStyle, CppCheck and Visual Leak Detector.
I'v figured out that I need to generate a DynamicAnalysis.xml file and submit it to CDash, from CTest scripts.
I think I know how to run the external tool as a part of the ctest script.
Either by using these variables to change how ctest_memcheck() works
CTEST_MEMORYCHECK_COMMAND
CTEST_MEMORYCHECK_SUPPRESSIONS_FILE
CTEST_MEMORYCHECK_COMMAND_OPTIONS
or by running the tool from the execute_process() command.
But I'm a bit uncertain which one to use.
The main problem I think I have is, how can I extract errors from the output of the custom tool and include that information into the DynamicAnalysis.xml to submit?
The extreme solution i see is that i'd need to make a program that generates a valid DynamicAnalysis.xml file.
But the problem is that I don't know the syntax of the DefectList element in the XML file. I have found no answer from google and even the XML Schema for that file is unhelpful.
EDIT:
Looking at this:
http://www.cdash.org/CDash/viewDynamicAnalysis.php?buildid=987149
What draws my attention are the labels, especially the empty ones. I don't see how these would come from the DynamicAnalysis.xml file. Maybe it tracks any labels that have ever appearred? Can i create my own custom labels somehow?
Does CDash create the labels automatically, depending on the tool type? Does this block custom defect types?
I'm just guessing here, so the question is; can i create custom labels for my custom tool, just by generating a DynamicAnalysis.xml - file.
It occurred to me that the amount of different errors from CppCheck (static code analysis) is huge, compared to valgrind for instance. I'm not that certain that I should use the dynamic analysis. Maybe a custom build type (Continuous / Experimental / Nightly) thing would work better. Like this:
http://www.cdash.org/CDash/buildSummary.php?buildid=930174
I have no idea how to do this, i guess it requires meddling around with CDash code?
Which one would work better?
If you are using valgrind, you can simply set CTEST_MEMORYCHECK_COMMAND to the full path to valgrind, and ctest will generate the DynamicAnalysis.xml file for you from the valgrind output when you call ctest_memcheck.
The best way to understand the possible values that can appear in the DynamicAnalysis.xml file is to analyze the source code of CTest.
The file CMake/Source/CTest/cmCTestMemCheckHandler.cxx has the list of defect types in a variable named "cmCTestMemCheckResultLongStrings". Search through that file for references to that variable to see what the possible values are and how they are used to generate "<Defect/>" xml elements.
EDIT (for additional information):
You can also easily see what XML elements CDash is expecting by inspecting its source code. Specifically, the file "CDash/xml_handlers/dynamic_analysis_handler.php".
From what I'v learned so far, is that for a tool that runs on the tests made in the cmake script, the Dynamic Analysis is the thing.
For tools that run on the entire program, a custom Build.xml is the thing you need.
I found out that i can commit those files from the ctest_submit command by using the FILES parameter.
I also found out that you can add custom "build names" to the side of Continuous, Nightly, and others.
And that you can set the builds from certain machines to be automatically transferred under these.
The custom labels under DynamicAnalysis did come from somewhere in CDash, i can't remember where anymore.