Binding variable in cucumber - cucumber

I am doing BDD test on an app with cucumber, and I want to have clear instruction as it is recommanded in cucumber doc. The thing is that we have to do reusable step definitions so the maintenance cost is acceptable.
Example of scenario we have
Given I am on project page
When I click on 'buttonAddProject' //not easily readable
And I click on 'switchProjectPrivate'
And I click on 'buttonDeleteProject'
etc..
I don't want to have a function for each step like that: I change projet visibily or I delete project,
because this is basically just a click on a button, and we are going to have hundred of function like this. I also can't change the param in key to something more suitable, because every button key should be unique to avoid ambiguity.
So is there a way to do this with cucumber ?:
Given I am on project page
When I click on 'Add' //easily readable
And I click on 'Private'
And I click on 'Delete'
Bindings: //this keyword doesn't exist
'Add' : 'buttonAddProject'
'Private': 'switchProjectPrivate'
'Delete':'buttonDeleteProject'
I have tried that:
Scenario Outline:
Given I am on project page
When I click on <Add> //easily readable
And I click on <Private>
And I click on <Delete>
Examples:
|Add |Private |Delete |
|'buttonAddProject'|switchProjectPrivate'|'buttonDeleteProject'|
it works... but I need to do this for every scenario in the file, and if I really want to use scenario outline to iterate several times, I would have to copy paste this for every line, not really what I want.
How to organize this tests to make them more readable without making things to complex ?

First of all Cucumber scenarios that show HOW each thing is done are not maintainable or particularly useful.
What are cucumber scenario should describe and document is WHAT you are doing. To do this you need to determine WHY you are clicking on these buttons and what is achieved by these actions.
Now I have no idea from your scenarios about WHAT you are adding, WHY it is private or WHY you are then deleting it. But I can speculate from your post. The scenarios you should be writing should be something like.
Scenario: Delete a project
Given there is an existing project
And I am viewing the project
When I delete the project
Then ...
Scenario: Create a project
When I create a project
Then a project should be created
When you write your scenarios in this manner you push the details of how you interact with your UI down into your step definitions. So you might have something like
When 'I create a project' do
visit project_page
click "Create Project"
end
or better just
When 'I create a project' do
# must be on project page
click "Create Project"
When you work this way step definition re-use becomes less relevant and valuable. Each step does more and does something more specific.
You can continue this pattern of pushing the HOW down by having step definitions make calls to helper methods. This is particularly useful when dealing with Given's which get alot of re-use. Lets explore this with Given there is an existing project
Given 'there is an existing project' do
#project = create_project
end
Here we are pushing how we create an existing project down into the helper method create_project. The crude way to this would be to go through your UI visiting the project page and adding a new project. However this is really slow. You can optimise this process by bypassing your UI.
The most important point, whatever you decide to do, is that you are taking HOW you do something out of Cucumber and into some underlying code so now Cucumber is only interested in WHAT you are doing and WHY its important.
Making this change is probably the single most important thing you can do when Cuking. If you keep the HOW in your cucumber scenarios and step definitions you will end with a large number of brittle step definitions and very large scenarios that break all the time because everything is coupled together. You will get lots of bugs where making a change to get one step definition working causes lots of other scenarios to break. You will get lots of bugs where small changes to how you do a particular thing cause lots of unrelated scenarios to break.
Finally you are not doing BDD if you are writing the test after the code has been written. You can only do BDD if you write your scenarios collaboratively before the code is written.

Each step must be tied to a step definition. If you like to reuse an existing step def, you can just pass the command as argument (" Add", "Private","Delete"). You will have to use both the scenario name and the corresponding command to perform the required action.It will be something like this,
Scenario: scenario1_deleteproject
Given I am on project page
When I click on 'Add'
And I click on 'Private'
And I click on 'Delete'
Scenario: scenario2_createproject
Given I am on project page
When I click on 'Add'
And I click on 'Private'
And I click on 'Delete'
The step definition:
#When("When I click on {string}")
public void I_Click_On_Something(String command)
{
Switch(Command)
{
case Add:
//perform steps here
case delete:
//perform steps here
default:
}
If you want to differentiate the commands between the scenario, you will have to use scenario name ( need a class with definitions of scenario & command). You can grab the scenario name #Before hook.

Related

How to add an option to Cucumber report to remove scenarios that have a certain tag

I want to have an option on the cucumber report to mute/hide scenarios with a given tag from the results and numbers.
We have a bamboo build that runs our karate repository of features and scenarios. At the end it produces nice cucumber html reports. On the "overview-features.html" I would like to have an option added to the top right, which includes "Features", "Tags", "Steps" and "Failures", that says "Excluded Fails" or something like that. That when clicked provides the same exact information that the overview-features.html does, except that any scenario that's tagged with a special tag, for example #bug=abc-12345, is removed from the report and excluded from the numbers.
Why I need this. We have some existing scenarios that fail. They fail due to defects in our own software, that might not get fixed for 6 months to a year. We've tagged them with a specified tag, "#bug=abc-12345". I want them muted/excluded from the cucumber report that's produced at the end of the bamboo build for karate so I can quickly look at the number of passed features/scenarios and see if it's 100% or not. If it is, great that build is good. If not, I need to look into it further as we appear to have some regression. Without these scenarios that are expected to fail, and continue to fail until they're resolved, it is very tedious and time consuming to go through all the individual feature file reports and look at the failing scenarios and then look into why. I don't want them removed completely as when they start to pass I need to know so I can go back and remove the tag from the scenario.
Any ideas on how to accomplish this?
Karate 1.0 has overhauled the reporting system with the following key changes.
after the Runner completes you can massage the results and even re-try some tests
you can inject a custom HTML report renderer
This will require you to get into the details (some of this is not documented yet) and write some Java code. If that is not an option, you have to consider that what you are asking for is not supported by Karate.
If you are willing to go down that path, here are the links you need to get started.
a) Example of how to "post process" result-data before rendering a report: RetryTest.java and also see https://stackoverflow.com/a/67971681/143475
b) The code responsible for "pluggable" reports, where you can implement a new SuiteReports in theory. And in the Runner, there is a suiteReports() method you can call to provide your implementation.
Also note that there is an experimental "doc" keyword, by which you can inject custom HTML into a test-report: https://twitter.com/getkarate/status/1338892932691070976
Also see: https://twitter.com/KarateDSL/status/1427638609578967047

How to add pre-scenario for Scenario Outline in Jira Xray?

I want to add pre-steps to Scenario Outline as Scenario, but I don't want to add pre-condition. How can I do that in Jira Xray?
Some illustrative example:
Scenario:
Given: Open website
Then: Check URL
Scenario Outline:
When I click this <button>
Then Something happens with <this> element
I want to have some "background" for Scenario Outline because this is something that repeats in several tests but it turns out that for every step in S.Outline Background is repeating so I want to create a normal Scenario as "artificial" background. How to do that in Xray? Can I add something like pre-step? Pre-condition always creates "Background" in Gherkin.
I don't want to have several Scenario Outline scenarios with a lot of repeated steps.
This is more a Gherkin related question than Xray specific. There are no dependencies between Scenarios in Gherkin.
The way to have some initial Gherkin statements that apply to all to several Scenarios, is by having a Background in the corresponding .feature file.
In Xray this means that you need to have a Precondition, of Cucumber type, with those Gherkin statements. Then you need to associate it all Tests (Scenario/Scenario Outline types) that you want. Whenever exporting those tests, a .feature file will be created with a Background corresponding to the previous Precondition.
The rules for generating .feature files based on Test and Preconditions issues in Xray is detailed here.
Note: in Cucumber libraries there also hooks, that contain code that can be executed before/after tests. However, this is used for something that isn't described as behaviour and more for easing implementation, and doing things like test setup/cleanup.

Can we call one scenario inside another scenario in cucumber?

I started working with behavior driven tool cucumber. Its a fun tool to use. While i was working on a problem. I came across that most of time, I am not reusing my code.
That's why I want to call a scenario from another scenario. I have searched but found nothing helpful. Can I do that ?
Another same question posted here on github
This may be what you're looking for: https://github.com/cucumber/cucumber/wiki/Calling-Steps-from-Step-Definitions
So there are a couple of things you can do. If you have a step you want to reuse like the following:
Given /^I log in as (.*)$/ do |name|
# ...
end
You can call it within another step like so:
Given /^(.*) is logged in$/ do |name|
step "I log in as #{name}"
end
You can also do the following within a step definition:
steps %Q{
Given I log in as #{name}
}
I came to the same question - and found this post. Maybe it is on purpose that you cannot call Scenarios from other Scenarios. The framework is based on that you think about creating practical Stepdefinitions, so they can be used many times. The basis is to think before creating steps...
I created own Steps vor Login-Method, Pagetransitions to search-page or new File etc..
So in many Scenarios i reuse these Steps - and then add new ones (that can be reused, too).
Now you can think about how big one step should be. You can size it as one action in the testobject or use it as a routine to come to a certain startpoint of your Test over multiple actions. E.G. Given Go to Startpage of creating a security request
Java Code:
#Given ("^Go to Startpage of creating a security request$")
public void GoToStartpageOfCreatingASecurityRequest(){
//logic to get to the demanded point in testobject...
}
So as any other framework cucumber has its limits but they are intended and you have ways to work around it. ;)
Do not forget to use assertions in your test. Wether you use JUNIT or TestNG (I use TestNG). ;)

Saving to multiple lists from 1 sharepoint 2007 list form

I have a request form I'm working on, wherein different departemnts need to be able to update it. To minimize overlap and lost changes I'd like to be able to submit data from the new form to different lists, but I cannot find a way to do this.
Does anyone have any experience trying to do anything similar?
If you're familiar with JQuery andSPServices I could envisage a way to do this.
In the EditForm.aspx, add the JQuery and SPServices libraries. using the $.(document).Ready function, I'd do a quick item update with the SPServices and just copy a column with the same data, so in effect no change looks to have taken place. I'd add in the edit comments something like "Pseduo checkout to [name], [date_time]".
Then allow the user to edit the form as normal but in the code you've added, you trap the PreSave Action and check that the person trying to do the save is the same as the last modified - if it is, save as normal, otherwise, return false on the PreSave and it will be denied. When you actually allow the save, set the edit comments to something sensible.
To complete this, check before doing the pseudo checkout, that the last comments don't contain the psuedo checkout phrase so that you can prevent anyone opening/editing the form whilst somebody else is in the middle of an edit.
This gives a cheap and relatievly easy to implement Check-In/Check-Out for a list. Not perfect of course but should work well in most scenarios (not in datasheet though, so you might need to prevent that type of edit).
If you have two lists would you not then have the problem of potentially two requests for the same thing?
Does none of the version control options for the list solve the problem of potentially multiple concurrent editors?
While SPService is certainly a solution, but you will have to build a UI of ur own.
Try writing a event receiver, which can copy over item to another list as soon as it is created.
It will be nice if you can tell why you really want to have a copy of item in another list
i.e. Auditing purpose etc. , you can get a perfect solution for this in Forum

One of X number of colums must contain data

HI all,
I am creating a timesheep app and I have five colums that can contain hours worked. When the user enters a new form how do I check to see if at least one of the columns contains data. I must admit I am not a developer just a Sharepoint/Sharepoint designer hack so be nice. Thanks
Glenn Thibeault
The only bullet-proof way would be to create a SharePoint event receiver using C# (lots of examples on the web).
I'm not really sure how you could accomplish this with SPD.
If you don't want to write any C# code, that really only leaves JavaScript. It will still take development work (this is a programming site after all). You could probably take advantage of SPUtility.js (full disclosure, this is a library I maintain).
The basic steps would be:
Edit your NewForm.aspx and add a Content Editor web part
Inside the Content Editor web part, write your JavaScript:
Attach a new onClick handler to the NewForm.aspx's "OK" buttons
Use SPUtility's GetValue method to get the value of your 5 fields, validate one has a value, and display a message if invalid

Resources