How do I do if statements in Cucumber scenarios? - ruby-on-rails

My app has permissions, and certain tests need to not be run when a particular permission is on, and some tests need to be run when that same permission is on.
Is there a way to do this? or do I need to use a different framework?

The standard way of excluding 'tests' in Cucumber is to add a tag to them to identify them, then when invoking Cucumber you specify which tags to include/exclude. In your example, you could tag a specific scenario:
#needs_permission
Scenario: View users billing information
Or tag the whole feature:
#needs_permission
Feature: Administrative area
Scenario: View users billing information
Or tag certain examples in a scenario outline:
Scenario Outline: Visit a page
Given I visit "<page>"
Examples: Don't need permission
| page |
| index |
| sitemap |
#needs_permission
Examples: Do need permission
| page |
| admin |
Now, when you run Cucumber, you can exclude those tags if necessary:
When the permission is on and you want to run all tests:
cucumber .
When the permission is off and you want to exclude the tests that need it:
cucumber . -t ~#needs_permission
An alternative which I have used with mixed results, if you really don't know ahead of time, is to mark a step as pending if it doesn't apply given the current scenario, e.g.
Given /^I visit some page which needs permission$/ do
pending "Permissions aren't enabled - skipping" unless permissions_enabled?
end
This will mark the step as 'pending' which really means 'not fully implemented', which isn't ideal, especially if you have many such steps - it can be difficult to ensure that other unimplemented steps don't accidentally creep in and get hidden by all the ones you've deliberately marked as such.

We get a similar instance of this a lot, we have 2 solutions,
either tags as suggested, or
create different scenarios for different permission types:
Given i log in as a moderator
and there is a new post
when i view the post
Then i should see a delete button
Given i log in as a normal user
and there is a new post
when i view the post
Then i should not see a delete button

May be possible using tags. Difficult to say without more detail.

Related

What kind of hook can I use if I want to change my custom price on Woocommerce bulk edit page?

I created custom price for WC_Product, put them into WC Bulk Edit Page and want to save changed values to disk.
I have seen class WC_Admin_Post_Types (woocommerce/includes/admin/class-wc-admin-post-types.php) and found there some kind of hooks.
I tried to hook `woocommerce_product_bulk_edit_save', but this trigger does not work (or I did something wrong there).
I did find examples in WWW, but then I shall create and use wp_ajax_{something} and 20-30 lines JS code.
May be exists another way for this - my custom price needs all the same as native WC price - where can I see how they are handled and save?
I was able to connect my custom function to hook `woocommerce_product_bulk_edit_save': this hook run if any of native Bulk Edit Fields will be filled, but I filled my custom field only. All should be fine. Thanks if you read this).

Gherkin not recognizing examples for scenario outline

I'm new to writing features in gherkin, and am getting an error on my very first scenario outline.
#language:en
Feature: Create an account
In order to become a member
As a new user
I can create an account
Scenario Outline: View the account creation page
Given I am at the "<page>" page
And I have selected "<lang>" language
When I see the Sign Up link in my language
And I click the "<size>" Sign Up link in my chosen language
Then I see the User Account page in my chosen language
And the Create New Account tab in my chosen language
Examples:
|page |lang |size |
|landing |English |large |
|landing |English |small |
|FAQ |English |large |
|Forums |English |small |
|landing |Francais |large |
|landing |Espanol |small |
When I run
cucumber --dry-run
I get the following error:
Missing Examples section for Scenario Outline at [filename].feature:8
(Cucumber::Core::Gherkin::ParseError)
Originally I had the variables in angle brackets without quotes, and added quotes following the suggestion here but alas, it didn't help with my situation.
Any ideas what my mistake is?
Thanks!

Xtext - Temporarily tolerate unresolvable references and make the reference name user readable

Let's say I have a language that models a part of stackoverflow. Users are held in one resource, questions in another.
Users:
user fred : fred#foobar.com
user notfred : notfred#foobar.com
Questions:
question 123 by fred message "smart question"
question 124 by notfred message "not so smart question"
Now, the user "fred" wants to remove his account, but this wouldn't work because after loading both resources into my ResourceSet I would have a non-empty Resource#getErrors().
I can work around this by filtering XtextLinkingDiagnostic from the errors, but still other users reading the "smart question" cannot tell anymore that it was asked by someone called "fred". The info is still there, I can access it for example when I set a LinkingDiagnosticMessageProvider with LazyLinkingResource#setDiagnosticMessageProvider(...); however, the best thing I can now do is, show other users validation errors that "fred" was deleted, but they wouldn't know "fred" wrote the message they are just reading. Knowing this would help them a lot because everybody knows that "fred" writes great questions, right?
Long story short, I have an application into which users can load a declaration and a definition file. In very few cases something goes wrong and both files don't match perfectly, which means the definition has entries that are not declared in the declaration. However, I know that ~95% of the entries will still match!
Users cannot fix this quickly, but it is likely that they are happy just editing the 95% definitions, but they still need to be able to read the names of the 5% declarations without editing them!
I am not currently using any UI-parts of Xtext to edit the definitions, but rather a custom UI in form of a table. The current state with the missing declarations is that everything except a value column will be empty. The reference ID would be in another column, and knowing this ID would help the user a lot! Is there a clean way to achieve this?
Have a look at the 'Node Model' e.g. org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature(EObject, EStructuralFeature) allows you to access the text that is written in the file

Add a pop up box for each user/IP to see just 1 time

I don't know exactly what to search for when looking for info on this so here we go...
I've recently made a change to my MVC website (all the code parts are VB, not like most examples which tend to be in C#).
Once a user logs in, is there a way to show that user a pop up box, or a div, which simply explains the changes made (the text in the box is not important).
I only want the user to see this ONCE!
Example:
As soon as the user goes to the URL, they get a pop up box saying "You now have to add your name when creating a new post"
But of course, the user doesn't want to see this EVERY time they log on to the page.
Any pointers would be lovely,
Cheers.
You would need to in some way track that users have seen the message. If there's any likelihood that there will be future such messages, maybe any value in tracking when users saw the message, etc. then this can all be put into a fairly simple database structure.
Maybe a lookup table of messages:
ID | Message
-------------
1 | "This is the text of the message, or maybe the whole HTML, or some other data, it's up to you."
2 | "Other message, etc."
And a table to link that to users:
UserID | MessageID | DisplayedOn
--------------------------------
123 | 1 | 2014-01-30 08:56:21
You can even make the messages more "interactive" and have an "Accept" or "I Understand" button, requiring that the user accept the message or see it again until they do. For something like that you can change DisplayedOn to FirstDisplayedOn (maybe include a LastDisplayedOn?) and also an AcceptedOn to track that interaction. This could be very useful for updates to terms of service, for example.
Then when a user logs in, when you fetch the overall user context/object from the database you would include any messages they need to see, haven't seen yet, haven't accepted yet, etc. Your UI can then display whatever it needs to display based on that.
As you want to show popup only after user has logged you need to store information about who has seen this popup. I am guessing that you already have database and data access layer all set up. So, you need to add a new table with following columns:
Id | UserId | PopupId | ShownOn
Before displaying popup you need to query this table to see if user has already seen it. If not, just show the popup and insert a new record in data table.
As we don't know how you access your data it is impossible to give more precise solution.

Equivalent of Background to run after scenarios

I'm new to SpecFlow, and am setting up a number of test features/scenarios that are for authenticated users of different role types. I'm doing this via browser automation with Coypu.
So I have a Background step in the feature to set up the log in of a user in that role type.
Background:
Given I am logged in as a ...some role I'm interested in...
After each scenario in the feature, I want to log the user out again (otherwise the log in step in the Background won't work for the next scenario -- I'm keeping the same Coypu browser instance open in between tests).
I found the [AfterScenario] annotation that I could use, but as this is scoped across all scenarios in all features (as far as I understand...) it would affect scenarios say for unauthenticated users.
I could scope the [AfterScenario] with [Scope(Feature="Some Feature")] I believe, but I'm anticipating having to log in/out before and after a whole number of features that I'm testing, and I'm not keen on specifying all of these with a bunch of magic strings.
So I'm wondering if there's something I can put in the Feature file, kind of the equivalent of Background but to run after each scenario in that feature. (Or alternatively, maybe the way I'm logging in/out for each scenario is not the best way to go about things?)
There is no "Postground" feature in specflow however you can achieve something similar with tags filtering.
In most of our projects we are using tags to mark scenarios which have specific setup/teardown logic.
Then we are using the BeforeScenario/AfterScenario hooks to execute the logic:
[BeforeScenario("authentication")]
public void BeforeAuthenticationScenario()
{
//...
}
[AfterScenario("authentication")]
public void AfterAuthenticationScenario()
{
//...
}
And you can tag individual scenarios or whole features:
#authentication
Feature: Some feature requires authentication
#authentication
Scenario: Some scenario requires authentication
So in your code you will only have one magic string "authentication" and in your features you can apply the custom logic declaratively with the tag.

Resources