How to use nested Scenario Outline in Cucumber java - java

2suppose i have a Scenario Outline like
#Scenario1
Scenario Outline:Scenario one
Given fill up login fields "<email>" and "<password>"
And click the login button
Examples:
| email | password |
| someEmailAddress | SomePassword |
| someEmailAddress2| SomePassword2 |
and another Scenario like
#Scenario2
Scenario Outline:Scenario two
Given fill up fields "<value1>" and "<value2>"
Examples:
| value1 | value2 |
| value11 | value21 |
| value12 | value22 |
How could i run scenario like login with 'someEmailAddress' and fill up with all scenario2 value and then login with 'someEmailAddress2' and do the same.

Cucumber scenarios are tools we use to describe behaviour i.e. what is happening and why its important. They are not tools to program tests. The way to use Cucumber effectively is to keep your scenarios simple, and let code called by step definitions do your programming for you.
Step definitions and the methods they call are written in a programming language. This gives you all the power you need to deal with the details of how you interact with your system.
The art of writing Cucumber scenarios is for each one to talk about
The state we need setup so we can do something (Givens)
Our interaction (When)
What we expect to see after our interaction. (Then)
So for your scenario we have
Scenario: Login
Given I am registered
When I login
Then I should be logged in
When we make this scenario work our program has the behaviour that we can login. So then we can use that behaviour in other scenarios e.g.
Scenario: See my profile
Given I am logged in
When I view my profile
Then I should see my profile
Now to make this work we might need a bit more work because this scenario doesn't have a registered user yet. We can deal with this in a number of ways
1) Add another Given, perhaps in a background
Background:
Given I am registered
Scenario ...
Given I am logged in
2) We can register in the login step e.g.
Given "I am logged in" do
#i = register_user
login_as user: #i
end
Notice how in this step we are calling helper methods register_user and login_as to do the work for us.
This is the way to start using Cucumber. Notice how my scenarios have no mention of how we login, no email, no password, no filling in anything. To use Cucumber effectively you have to push these details down into the step definitions and the helper methods they call.
Summary
Keep you scenarios simple and use them to describe WHAT and explain WHY. Use the step definitions and helper methods to deal with HOW. There is no need to use Scenario Outlines when using Cucumber and you should never be nesting them.

There is no support for nested scenario outline in cucumber. but you can use following way to overcome it.
Scenario Outline:Scenario one and two
Given fill up login fields "<email>" and "<password>"
And click the login button
And fill up fields "<value1>" and "<value2>"
Examples:
| email | password | value1 | value2 |
| someEmailAddress | SomePassword | value11 | value21 |
| someEmailAddress | SomePassword | value12 | value22 |
| someEmailAddress2| SomePassword2 | value11 | value21 |
| someEmailAddress2| SomePassword2 | value12 | value22 |

Related

Second scenario doesn't run if first fails in selenium java

I have a selenium test case with multiple scenario's. I have a problem if the first one fails it doesn't run the second scenario properly. It does show the error for the first one failing. The one following it will just show all green check marks without starting up the UI tests. In the example below Bob has an error (throws an exception), but Sandra has never even started up the browser and it still shows like she has passed.
Abstract Scenario: I should be able to login
Given I log in with user with the login '<username>'
And I give the password '<password>'
Then I am logged in
Example:
| username | password |
| Bob | easyPassword|
| Sandra | hardPassword |
Does anyone know how to make the second scenario show the proper results instead of showing it like it passed? It is important because it's helpful to know one which exact scenario the test has failed.

navigate between features scenarios in cucumber

I have two gherkin file the
first do the login with multiple user
and the second create multiple patient
here is my gherkin files:
Scenario Outline: User Login
Given user is on login page
When user enters username and password from line <RowNumber> and clicks login
Then check if login with data from line <RowNumber> was successful
Examples:
| RowNumber |
| 0 |
| 1 |
| 2 |
Scenario Outline: Patient Creation
Given user is on the creation page of the application
When user enters patient information from row <RowNumber>
Then user checks that displayed errors are the same as row <RowNumber>
Examples:
| RowNumber |
| 0 |
| 1 |
| 2 |
what I want is that when login with the first user I want to go to other scenario patine creation and create the 3 line and do the same with the 2 - 3 user
Test cases from scenarios should cover the appliaction and make it "safe". You can do the programing and just create sepparate method, or call the existing one that is transfered from gerkin into your step definitions.
Like inside of method "user enters username and password from line and clicks login", you can call method from other step definition file like "user enters patient information from row " (not same text of method in step definition as in feature file).
But that should not be the case, remember you will count your scenarios for report, there is no magic trick you can do about it, can go with multiple assertions, but that is not recommended.
The real question is do you need that structure of scenarios and .feature files or you could write them in a better way. If can not, leave them as they are, you will not lose much on execution time.

How to ensure that there are no race conditions in a database update statement using spring

Imagine there are two entities Children and Gifts. Let's say there are 1000 children and 10 gifts. Children will always try to grab available gifts, and gifts will be tagged to children on a "first come, first serve" basis.
Table structure
children table
+----+------+
| id | name |
+----+------+
| 1 | Sam |
| 2 | John |
| 3 | Sara |
+----+------+
gift table
+----+---------+-------------+
| id | gift | children_id |
+----+---------+-------------+
| 1 | Toy Car | 2 |
| 2 | Doll | 3 |
+----+---------+-------------+
Here the children_id is the child who grabbed the gift first.
In my application, I want to update the children_id in such a way that only the first child who initiated the request will get it and rest get a GiftUnavailableException.
How will I ensure that, even if a 1000 requests come at a time to grab a specific gift, only the first one will get it. Or how will I ensure that there are no race conditions for this update.
Are there any spring specific feature that I can make use of or are there any other ways.
I'm using spring-boot as my backend.
I can't post a comment so here I go !
I assume you are using Spring Data JPA.
Then you should use #Transactional annotation : This mean that everytime you are requesting your database, you do a transaction :
Begin Transaction
Execute Transaction
Commit Transaction
Lot of usefull informations about this (Read it !!): Spring #Transactional - isolation, propagation
You will need to seed your Transactional Isolation to Serializable and maybe change the propagation method.
And if you are not using Spring data JPA.. Well There is a synchronized keywords but I think it's a bit awful to use it here.

Java Database Access Builder Patter

I have a database with a table called Car. The car table looks like this:
+----+------+--------------+-----------+----------+------+
| Id | Name | Desccription | Make | Model | Year |
+----+------+--------------+-----------+----------+------+
| 1 | A | something1 | Ford | Explorer | 2010 |
| 2 | B | something2 | Nissan | Ultima | 2005 |
| 3 | C | something3 | Chevrolet | Malibu | 2012 |
+----+------+--------------+-----------+----------+------+
Different pages on my website want to display different information. Some pages only want to display the name, others wants to display the make and model, etc.
I have an api that the web calls to retrieve all this information. The api uses JPA and QueryDSL to communicate with the database and fetch information. I want to only fetch the information that I want for that particular page. I'm thinking about implementing some sort of builder patter to my repo to allow for me to only retrieve what I want but I'm not quite sure how to go about it.
For example, my home page only wants to display the Name of the car. So it'll call the HomeController and the controller will call the HomeService which will call the repository layer something like this:
carRepository.getCarById(1).withName().build();
Some other page that wants to display the make and model would make a repo call like this:
carRepository.getCarById(1).withMake().withModel.build();
What is the best way to implement something like this in Java/Jpa?
If I understand the question correctly, you want queries for different projections of your entities to be built dynamically.
In that case, dynamic entity graphs are what you want (see e.g. here: https://www.thoughts-on-java.org/jpa-21-entity-graph-part-2-define/). You start with an empty entity graph, and each call to one of your with() method simply adds a field to the graph.
The base query remains unchanged, you just need to set the fetch graph hint (javax.persistence.fetchgraph) upon calling build() (note that the samples in the above link use load graphs instead of fetch graphs; the subtle difference between the two is described here: What is the diffenece between FETCH and LOAD for Entity graph of JPA?)

How to verify all elements on the page?

I want to verify if all needed elemants exist on the page.
I can list them in the Examples section for Scenario Outline. For example:
Scenario Outline: I am able to see all elements on My Page
When I am on my page
Then I should see the following <element> My Menu
Examples:
| element |
| MENU button |
| MY logo |
| MY_1 link |
| MY_2 link |
| Button_1 button |
| Button_2 button |
| Loggin button |
Each row runs a separate method to verify an element's presence on the page. The problem is - the page is reloaded.
How can the problem be solved in more appropriate way?
You don't need a scenario outline. You just need a step that verifies all the elements in the table.
Scenario: I am able to see all elements on My Page
When I am on my page
Then I should see the following elements in My Menu
| MENU button |
| MY logo |
| MY_1 link |
| MY_2 link |
| Button_1 button |
| Button_2 button |
| Loggin button |
Where you can use the table as a array of arrays:
Then(/^I should see the following elements in My Menu$/) do |table|
table.raw.each do |menu_item|
#my_page_object.menu(menu_item).should == true
end
end
When(/^I am on my page$/) do
#my_page_object = MyPageObject.new(browser)
end
First of all using a scenario outline will result in 1 scenario for each element you want to test. This has huge run-time costs, and is not the way to go.
Secondly putting all this information in the scenario is also very expensive and unproductive. Gherkin scenarios are supposed to talk at the business level, not the developer level, so I would rewrite this as
Scenario: I am able to see all elements on Foo page
When I am on foo page
Then I should see all the foo elements
and implement it with something like
Then "I should see all the foo elements" do
expect(should_see_all_foo_elements).to be true
end
and now you can make a helper module to make this work
module FooPageStepHelper
def should_see_all_foo_elements
find('h1', text: /foo/) &&
...
end
end
World FooPageStepHelper
Now when the foo page gets a new element, you only have to change one line in one file. Notice how the business need (that all the elemnts should appear on the page) doesn't change when you add or remove elements
(n.b. you could improve the helper function in a number of ways to get better info when something goes wrong, and even output listing the elements that are present)

Categories