I am using Selenium and cucumber to perform behaviour driven tests.
I have the following feature:
Scenario: User changes name
When User changes name
Then User can see "Name has been updated"
And User can see the updated name
however, on this website, sometimes there is an error with the server so the name might not update, in which case it will say 'name has not been updated' and the updated name won't be shown. This is an acceptable response, but how would I test this scenario using cucumber, covering both situations?
Like something like an if then else statement but in cucumber.
You should setup the site so it gives a consistent result, using a Given.
Given site is accepting name changes
When User changes name
Then User can see "Name has been updated"
And User can see the updated name
Given site is not accepting name changes
When User changes name
Then User can see "Name has not been updated"
If you can't get consistent results from the website I would play around with your step definition.
For example would do something like:
Scenario: User changes name
When User changes name
Then User can see the updated name
And in your step definition I would go for something like:
pseudocode:
Boolean pass = false;
String elementText = driver.findElement(...).getText();
if(elementText.equals("actual name") || elementText.equals("Name has not been updated") {
pass = true;
}
assertTrue(pass);
Or something like that. But still I would rethink this test scenario as maybe there is a better approach to test this feature.
Related
I am required to create a Java Script but unable to figure on how to proceed as I don't have that much of coding idea in OIM, can someone assist(below I have mentioned the scenario)
Scenario:
In OIM User Attribute Page, there is a User Field: 'Job Code' now we have experienced that there are some issues we are facing.
From the trusted source we are getting the correct data but as soon as it reaches OIM for few users we are getting random incorrect value. Value should be numbers (123456) which is present in the Database too and valid but for few we are receiving values like E?401#q something like this.
We are required to place a check to find users who are having these invalid Job Code entry.
Once detected, we need to trigger a email to the concerned team based on the User Type (Employee or Contractor) for employee it should trigger an email to a respective team and for Contractor we have to trigger to a different team to take action.
So, I believe we have to place two conditions here, can someone assist.
If you believe it only happens during trusted source reconciliation, then you can create Post-Process Handler on User Create/Modify operation to check the value which was posted into the DB as a result of recon event.
From this handler you can do all the things you need to do: mailing, fixing, etc.
For notification purposes I'd recommend to use built-in NotificationService, though it might be bit daunting, if you have little experience in OIM development. As an alternative you can do java mailing.
If you are not sure about the moment, when this "spoiling" happens, you can create a scheduled task, to be executed periodically, which will check JobCode values, to report invalid ones.
I' m working with dialogflow to have a chatbot for renting different buildings(in my case called FMRs). I have a "New FMR" intent as shown in the following picture:
As you see this FMR has different rent prices according to its facilities.I have a graphql database to store FMRs and their parameters. I used webhook and wrote a piece of java script code to get the list of FMRs. Whenever user says "Set up a new FMR called TEST with rent two bedroom at $1500" my code checks whether the FMR name(TEST) exists in the database or not.If it exists we should ask the user if she/he wants to edit the parameters of existed FMR, and if it doesn't exist, the new FMR will be created in database. My problem is with the first part that FMR exists in database.As I only can check its existence in my own code, where can I ask the user to edit the existed FMR or no and get his/her response to continue the work? How can I have a sub-intent to connect it to my code and have the above scenario correctly? Or how can I trigger a sub-intent in my code to be executed after user says "Yes" or "no" to my question about "editing existed FMR"? Sorry for long description and thanks in advance for your help
I'm still confused about how this conversation flow should look, but it sounds like it would be something like this:
User Says: "Create a new FMR named name with..."
If "name" does not exist, save the data, reply with "The FMR named name has been created" and close the conversation.
If "name" exists, reply: "The name FMR exists. Do you want to edit it?"
User says: "no"
Reply: "What should it be named?"
User says the new name and processing repeats as above.
User says: "yes"
Save the data, reply with "The FMR named name has been updated", and close the conversation.
In your fulfillment code, if you need to ask the yes/no question, you'd also reply with a Context, setting the parameters with enough information so you can remember what the user has already said about this FMR the next time your fulfillment gets called.
You can then create followup Intents for the "Yes" and "No" responses. Give each one a different Action setting, so you can differentiate them in your fulfillment. You'll also need another Intent that has this Context as the input context and has sample phrases that match how the user will specify the name (ie - with a parameter named "fmr-name" of type #sys.any) - this can have the same Action setting as your original Intent.
When your fulfillment is called again, you know if they've responded yes or no (by checking the Action). If they've said "yes", you have the previous information they already said and can save and exit.
If they've said "no", you'll prompt for the new name and set the Context again with the information you need to remember when you get a reply. When they do reply, you'll check the parameters for the new name and use the values from the context.
I am trying to convert Selenium test to Gherkin. Is there way to implement if statements in Gherkin?
Example : assume the code is written in the below format. I am just writing description as below. Please understand the part after double slash is the actual Selenium code:
// launch the application
// login to application
// navigate to page
String str;
if(str== "XYZ")
{
// verify title
}
//verify text field 1
//verify test field 2
//verify select box
For this I am trying to write code in Gherkin as follows
Given user launches the application
When user login with valid credentials
and navigate to required page
When String str is "XYZ"
Then verify title
And verify text field 1
And verify test field 2
And verify select box
but this code is incorrect because if the str is not equal to "XYZ" we want that title should not be verified but other verification like text field1,2 and select box should be verified.
You don't implement if in Gherkin.
Gherkin is about communication and those you want to communicate with, non coders, don't know what an if statement is. They don't care either.
The solution? Two scenarios to cover both cases.
Ideally, this level of detail would not be in your Gherkin scenario. The best approach is describe business use cases, not low level details. This is what Gherkin is designed for: communicating with non-technical stakeholders so that you can work out if you are building the right thing in the first place. Here is what I would write:
Given the user is logged in
And the user is on the required page
When they enter data that requires the optional fields to be validated
And they enter invalid data in the optional fields
Then the form shows an error on the optional fields
The low level details don't matter (that the string is specifically "XYZ" or that it is the title field is not important), so these should be hidden in the step definition and/or unit tests.
In order to continue to check the other fields, you can just add another step after this:
When they enter invalid data in all of the other fields
Then each other field has an error message attached to it.
Again, there is no need to specify the actual fields, or separate them into their own steps. The idea is to express the high level business value of the scenario, i.e. that the form is validated when it should be.
The advantage to keeping things high level is that when the form changes (as it eventually probably will), then this scenario can remain untouched. Which is correct as the business case is the same: it should validate when it's supposed to. All the changes will be in the step definitions. This means that there is no reason to have another discussion with your stakeholders about whether your scenarios are still testing the right thing.
You can write the scenario, somewhat like this:
Given the user launches the application
When user login with valid credentials
And navigates to required page
Then he should see the page datails
Inside the Then step you manage all the logic.
Then(/^he should see the page details$/) do
if condition
...
else
...
end
end
Gherkin is not a programming language to use if or else conditions. It is a part of BDD framework, that is implemented, to make the stakeholders and other non technical resources understand what the test process is about. Hence, it is always recommended, you keep the gherkin as simple and as generic as possible.
Strictly speaking you should create an alternative statement along the lines of:
Given user launches the application
When user login with valid credentials
and navigate to required page
When String str is NOT "XYZ"
Is there anyway where we can use if/else concept in feature file? For example:
Scenario: User should be able to check login page
Given I am on login page
When I click on SignIn button
Then I should be in home page
If yes
Then I will create a new profile
Else
Then I will logout from page
Not that I am aware of. Gherkin (and cucumber) are best used when they specify discreet business cases though, and should be repeatable, else they get hard to follow and test. It looks like you have two stories here at least:
Scenario: A new user should be asked to sign in
Given I am a new user
And I navigate to the login page
When I click on SignIn button
I should not be able to get to the home page
Scenario: An existing user should be able to log in
Given I am an existing user
And I navigate to the login page
And I submit valid credentials
When I click on SignIn button
I should be taken to the home page
No you can't and you shouldn't. Feature files are for business behaviour, not programming.
From your scenario I think you are trying to deal with different behaviour, depending on whether you are registered or not. To do this you would write two scenarios
Given I am registered
When I
Then I should ....
Given I am a new user
When I ...
Then I should be asked to register
Notice how these scenarios don't describe 'how' anything is done. Anything like `I click on foo' in feature is a smell and should be avoided.
You would like to twist your scenario as below
Scenario: User should be able to check login page
Given I am on login page
When I click on SignIn button
Then I should be in home page
And check if home page exist and store result as existence
If existence is true
Then I will create a new profile
And Else
Then I will logout from page
In the step definition for And check if home page exist and store result as existence is Check home page exist or not by checking the driver.findElement returns null or not and based on it store it as true or false in thread context instance with key as existence
In the step definition for If existence is true retrievie the thread contenxt instance data for key existence as asserts it if its true or false and store it with key conditionresult in thread context instance.
In the step definition for Then I will create a new profile before creating the profile retrieve thread context instance data for key conditionresult and check if value is true then create the new profile and set again thread context instance for conditionresult as null with in the condition loop.
In the step definition for And else retrieve the thread context instance for conditionresult and check if its false and if yes set again thread context instance for conditionresult as true
In the step definition for Then I will logout from page retrieve the thread context instance for conditionresult and check if its true then perform logout and set again thread context instance for conditionresult as null with in the condition loop.
What about if we are using Gherkin in a smoke test type situation and we need to ensure something exists in the database using only the UI?
Scenario: I need to create one (and only one) Box before I run the rest of my smoke tests
Given I login as Admin
When I am on the Box list Page
Then the test passes if the Box named "QA SmokeTest" exists
When I click the Add New Box Button
And enter the details for a New Box
And press Save New Box
Then the test passes if the Box named "QA SmokeTest" exists
The re-use of the same Then step twice is essentially an if-else that will make sure my Box exists so that I can run my other tests in the smoke test suite that require a Box.
But that is dependent on being able to stop the Scenario execution in the test runner or doing something extraneous like:
ScenarioContext.Current["TestPassed"] = true;
and then in each of the steps
if(ScenarioContext.Current.Get<bool>("TestPassed")) return;
You can use parameter in feature file and implement the If else in the Code based on the parameter passed.
Is it possible to reuse a feature as the "Given" for another feature?
Or am I trying to do something I shouldn't be trying to do
basically my features look like:
Scenario: Creating a basic account with valid details (happy path)
Given I am on the "signup" page
And I enter all the right details #this is shortened of about 20 steps for your reading ease
When I press the button labelled "Sign up"
Then I should see the text "Thanks for signing up"
And I should have an email from "confirmation#mysite.com" titled "Confirm your account Michael"
And the database should contain a record for the user marked as unconfirmed
Scenario: Confirming account via email
Given I have created a basic account
When I open the confirmation email and visit the url to confirm the account
Then I should be logged in
And the database should contain a record for the user makred as confirmed
I clear my DB after every feature as they should all be able to be run individually...
Am I going about this the wrong way?
Thanks
The Problem
What you're actually attempting is to reuse a scenario. This is no longer supported in Cucumber.
Aside from other problems with this approach, your tests will be slower and interdependent, since you will be:
driving account creation through a browser, and
making all your tests dependent on the account-creation test passing.
Don't do that.
The Cucumber Way
Generally, you should write your tests to work independently, although you can certainly reuse step definitions. So, in the general case, you might want to add shared steps like:
Given that user account "Test User" does not exist
Given that user account "Test User" exists
which can then be included in your scenarios as needed. The nice thing about this approach is that the steps can create or delete users programmatically.
Alternatively, if most of your tests will be on existing accounts, set up the default data set with the right user fixtures already in place. For the limited subset where you will be testing account creation, just add a scenario background that drives user deletion.
In case you are using Javascript, I've created a package named reuse-cucumber-scenarios for calling a scenario by doing:
Given the scenario "#scenario_tag"
.
Given the scenario "#scenario_tag" with parameters
"""
{
"1": ["step1_param1", "step1_param2"],
"2": ["step2_param1", "step2_param2", "step2_param3"],
"3": ["step3_param1", "step3_param2", "step3_param3"],
}
"""
or creating gherkin variables...
Given the variable "$variable_name" is equal to
"""
#JSON object
"""
or creating scenario functions and calling them by doing...
Given the scenario "#$scenario_function_tag" where variable "$variable_name" is "value_to_replace"
and more...
Currently, you can use the following approach:
Background:
Given Some common setup
And Some more setup
Scenario: one
When setup1
Then something1
Scenario: two
When setup2
Then something2