How to use data table in background in cucumber - selenium

I want to execute common steps for few test cases.below are the common steps.But in background,since i cannot use Scenario Outline,what is the alternate?
Background:To test employee id search
Scenario Outline:
Given I am on login Screen
When I enter credentials "<User_ID>" and "<Password>"
When I clicks on search button
And search by "<Employee_id>"
Examples:
|User_ID|Password |Employee_id|
|Admin |Password | Q58ewQ |

A scenario outline is a way to run several scenarios whilst writing just a single scenario. Background is a set of steps to run before each scenario. You can't put an outline in a background, it makes no sense.

A Background is used for steps that will be run before each Scenario (or Example) in the feature file.
Each Scenario Outline will run as a separate Scenario / Example.
You cannot use Scenario Outline inside a Background, as that would make no sense.
Please check the links to the documentation for more information.

This is example is very imperative, BDD need to be declarative. Best Pratices BDD
Why do you create one method that do login.
Login(username, password){
set username = username;
set password = password;
clickButton;
}
And create one step.
Given the user logged on the system
login(username, password)
This is step do you use with background.

You can use Data Table in Cucumber Background :
Background:
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| testuser_1 | Test#153 |

You can use a table in Cucumber Background:
Background:
Given I open Google's search page
When I use the following keywords to search in the Google's search page:
| CucumberBDD |
| CucumberHooks |

Related

Cucumber: loop over only part of the Scenario

is there any possibility to loop part of the scenario instead of all steps.
Below I have prepared an example, I want to login and click a list of links, but I want to execute the login-part only once. After logging the clicking part should be looped.
Scenario Outline
Given I am on Homepage
When I enter Email and Password and press send
And I am logged in
*And I click the link <link>
Examples:
| link1 |
| link2 |
| link3 |
| link4 |*
You should split your scenario in that case. Make the login as a scenario, do not close your driver in between scenarios. Now make the I click the link as a scenario outline in the same feature file.

testng-opening multiple browsers for scenario examples

I have this feature file
Scenario Outline:
Given User is on my website
When User is Admin or Mastering Editor
3.And User is on /page/ page
4.And User click /header/ link
5.Then User should be taken to /target page/
Examples: Of page, header and target
| page | header | target page |
| Homepage | Master | Master page |
| Homepage | Translation | Translation page |
For "Given User is on my website"
I wrote the login details, and this is opening new browser for every example in scenario.
I want single browser to open and do all examples.
can anyone help please
Follow these steps:
Create one driver class with initialization, closing and some common driver related methods
Create your step definition file with extends this driver class.
Create before class method to initialize the driver
every step will be using the same opened browser
so every feature file one browser will be initialized.
public class PersonSteps extends DriverHelper

Can we use scenario outline for background steps

I am using POM with cucumber. I have 3 scenarios to execute where half of the steps are common for all the scenarios. So I am using background keyword for common steps. But I need to pass data through Example keyword to the steps defined in Background as well. How do I do it?
Below is my background
Given User is on login Screen
When user enters "<User_ID>" and "<Password>"
Then verify successfull login
When user clicks on search button
And search by "<reference_number>"
Examples:
| User_ID | Password |reference number |
| Abc | XYZ |ABC123 |
You are not able to pass example data from a scenario outline to the background. You will need to include those parameterized steps in the scenario outline rather than the background. If one step depends on another, they are probably stuck repeating steps in each scenario.

How to write scenario to verlify a list of values displayed?

How to write a gherkin scenario in which the user clicks on a menu button and he should see a list of sub-menus displayed.I tried:
Scenario Outline:Display of the login page
Given User is an admin
When user clicks on the menu
Then he should see the <list> displayed
Examples:
| list |
| loginform, UsernameField, PasswordField |
Your description of the scenario and the scenario itself contradict each other. You write about sub-menus, the scenario lists fields. As the very first step, discuss the scenarios with your colleagues. When you have a clear plan, you can take on the syntax.
Syntax: why would you use a Scenario Outline when you only have a single example?

What is better test design: Navigate directly to URL or navigate normally by clicking (as a real user would) in selenium tests

Let us say my test wants to see if a user can see an image on XYZ page. And let's say in normal usage, the user can only go to XYZ page by clicking a link on ABC page (might be the home page).
Now assuming the URL to XYZ page is not static, but maybe depends on the image, and can be generated in the code simply, I have two ways of writing the test:
Generate the URL in test and directly navigate to XYZ, and then check if the image is present.
Go to ABC like a normal user would, click on the link which takes you to XYZ and then check for image
For option 1, I feel like I get more test isolation. If the link on ABC page is not generated correctly or is broken for some other reason, this particular test should not fail, right? That should be the responsibility of some other test?
But for option 2, that is how a real user would do it. He would almost never try to guess the pattern of the URL and then navigate to it directly. And I cannot have a huge test that goes to every link and sees if it is not broken, that would be way too complicated. So this much sacrifice in test isolation is needed.
How should I decide between the two options? Is there a right way? Hopefully the question is not too subjective for stackoverflow.
It depends on what you're wanting to test. If you want to test the buttons or links themselves (ie: you're testing the whole user workflow), click on them just like the user would.
If, on the other hand, clicking these links is just a means to an end and that the real target of the test case is deeper into the app, I think it's perfectly fine to skip directly to the part of the app you're actually testing.
As an End User, best approach will be the Option 2 which also widen the coverage of your test hence you can check:
1. Whether links/ buttons are clickable and not throwing exception.
2. Click on above is navigating to correct page.
Option 1, can be used for test scenario where user doesn't bother about how to reach to the page rather focused only on the opened page contents.
Choosing any of these option is depending on your approach of test coverage and its scope.