What is the "Background" keyword in Cucumber feature file


What is the "Background" keyword in Cucumber feature file


When we are writing Feature file in cucumber,  we write multiple Scenarios. All scenarios start with a particular point. Suppose  I am writing the feature file called home_page_facebook and the number of scenarios is there to check the home page functionality. Now if you think about any scenario then you need to login first on the facebook page to reach to the home page. So it is better to write all common or repeated step in one place rather than in all scenarios. To achieve this situation we need to add "Background" keyword in the feature file. Let's understand with an example.  

Feature file : home_page_facebook.feature

Feature: In order to test the home page of the application as a registered user  I want to specify the features of the home page. 

Scenario :  Home page default contents.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.
And user validate default contents of home page

Scenario : Top banner settings option.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.
When user click on the setting other 
Then user get the logout option


There is 2 scenario where you can see login steps are common to both the scenarios.  So we can eliminate the common section and put it into a commonplace.  

So I am rewriting the feature file with the Background keyword. 


Feature file : home_page_facebook.feature

Feature: In order to test the home page of the application as a registered user  I want to specify the features of the home page. 

Background: Flow till home page 
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.

Scenario :  Home page default contents.
Then user validate default contents of home page

Scenario : Top banner settings option.
When user click on the setting other 
Then user get the logout option

Now you can see login steps are in the commonplace.
There we use Background keyword.  All steps mentioned in the Background keyword will be executed before every scenario or scenario outline. 

I have provided all my glue code and cucumber feature which I have written in eclipse.

cucumber Background keyword

Step definition 

 package com.Cucumber.steps;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import cucumber.api.java.After;  
 import cucumber.api.java.en.Given;  
 import cucumber.api.java.en.Then;  
 import cucumber.api.java.en.When;  
 public class home_page_facebook {  
      WebDriver driver;  
      @Given("^user on application landing page$")  
      public void user_on_application_landing_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");  
           driver = new ChromeDriver();  
           driver.get("http://www.facebook.com/");  
           driver.manage().window().maximize();  
      }  
      @When("^user enter password$")  
      public void user_enter_password() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("pass")).sendKeys("giveourfacebookpassword");  
      }  
      @When("^user enter username$")  
      public void user_enter_username() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("email")).sendKeys("yourusername");  
      }  
      @When("^user click on login button$")  
      public void user_click_on_login_button() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("loginbutton")).click();  
      }  
      @Then("^user navigates to application home page\\.$")  
      public void user_navigates_to_application_home_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        Thread.sleep(3000);  
      }  
      @Then("^user validate default contents of home page$")  
      public void user_validate_default_contents_of_home_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        if(driver.findElement(By.name("q")).isDisplayed())  
        {  
             System.out.println("home page search box diplayed");  
        }else{  
             System.out.println("home page search box not diplayed");  
        }  
      }  
      @When("^user click on the setting other$")  
      public void user_click_on_the_setting_other() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("pageLoginAnchor")).click();  
           Thread.sleep(3000);  
      }  
      @Then("^user get the logout option$")  
      public void user_get_the_logout_option() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        if(driver.findElement(By.xpath("//*[text()='Log out']")).isDisplayed()){  
             System.out.println("logout diplayed");  
        }else{  
             System.out.println("logout not diplayed");  
        }  
      }  
      @After  
       public void tearDown(){  
           driver.quit();  
      }  
 }  


Feature file


 Feature: In order to test the home page of the application as a registered user  
  I want to specify the features of the home page.  
  Background: Flow till home page  
   Given user on application landing page  
   When user enter password  
   And user enter username  
   And user click on login button  
   Then user navigates to application home page.  
  Scenario: Home page default contents  
   Then user validate default contents of home page  
  Scenario: Top banner settings option  
   When user click on the setting other  
   Then user get the logout option  

In my previous cucumber tutorial, I have talked about how to setup cucumber in eclipse and how to run cucumber project. Here is the link

https://www.automation99.com/2017/06/how-to-install-cucumber.html


How to setup Jenkins slave machine




How to setup Jenkins slave machine step by step guide

Jenkins is an open-source automation server, it can be used as automating all kind of task such as building project, testing, deploying project. In software development, DevOps is a very important process nowadays and Jenkins is an essential tool to follow the processes. It is really imported to know how to install Jenkins and set up the Jenkins slave.


Before downloading the Jenkins you need to download and install Java JDK.


Step 1:

Download Jenkins: Jenkins can be downloaded from below link
Jenkins Download

It will download a "jenkins.war" file.

Step 2:

Copy the .war file in a folder. I am copying "jenkins.war" file in my D:/java/ folder.

Step 3:

Open up a terminal in the download directory and run below command

java -jar jenkins.war --httpPort=8080

Here "--httpPort=8080" is optional field. By default, Jenkins runs on 8080 port, but if you want to change the port number then you have to mention the port number as mentioned above.

Step 4:

Once you run the command, Jenkins server starts working and configuration setup starts.

Please find the configuration procedure to setup Jenkins master

Create jenkins master


Step 5:  

Next, we need to setup Jenkins node from Jenkins master. Before creating Jenkins node we need to enable TCP port for JNLP agents. We can enable TCP port for JNLP agents from below steps

1. Click on Manage Jenkins
2. Click on Configure Global Security.
3. Click on the Random radio button beside  "TCP port for JNLP agents". 


Add a new node from Manage Jenkins -> Manage Nodes 

jenkins setup

We need to provide some information about the node. Once we click on the Manage Node link node page will open and we will see "New Node" link.  


Steps for adding a new node. 

1. Click on the New Node
2. Provide the node name 
3.Click on the radio button Permanent agents
4. Click Ok



Now we need to provide below details for newly created node  


Name: < Name of the Node e.g. slave 1>

Description: < Description of the project (This is an optional field)>

# of executors:
The maximum number of concurrent builds that Jenkins may perform on this agent.
The default value is 1.

Remote root directory: This is a mandatory field.


An agent needs to have a directory dedicated to Jenkins. Specify the path to this directory on the agent.
It is best to use an absolute path, such as /var/jenkins or c:\jenkins. This should be a path local to the
agent machine. There is no need for this path to be visible from the master.
e.g. c:\jenkins

Labels:
Labels (or tags) are used to group multiple agents into one logical group.
For example, if you have multiple Windows agents and you have a job that must run on Windows, then
you could configure all your Windows agents to have the label windows, and then tie that job to this
label. 

This would ensure that your job runs on one of your Windows agents, but not on any agents without
this label.

Launch method
Controls how Jenkins starts this agent.


In my tutorial, I am using Launch agent Java Web Start.

I have created one slave with below configuration  


Once we have created slave node we can see newly created slave node displayed under Build Executor Status section on Jenkins home page. But we can see that node as offline mode like below 


How to start Jenkins slave machine 

1.  Open the Jenkins server from slave machine 
Suppose Jenkins server running on 192.168.0.103 and Jenkins port is 8080 then you need to open Jenkins from another machine in the same network with 192.168.0.103.8080. 



2. Provide user name and password. 
3. Click on the Jenkins slave machine (In my case slave 1)
4. Click on the slave.jar hyper link, you will see slave.jar will be downloaded in slave machine.  You just need to copy that jar file into you slave Jenkins directory. I have created Jenkins directory in my d:/jenkins folder.  

5. Open command prompt or cmd and navigate to the folder where you paste or download the slave.jar 


6. Copy the command mentioned in the node 

java -jar slave.jar -jnlpUrl http://localhost:8080/computer/slave1/slave-agent.jnlp -secret e15c60d05ca37d01d593abd6297d6e3f1bb3b9d91c3c97d9155262acea8c61d3 -workDir "d:\jenkins"

You just need to change the ip address. You need to provide master machine ip address where the jenkins server is running. In my case, my server running on 192.168.0.103 and post is 8080 , so command should be
 java -jar <a href="http://192.168.0.103:8080/jnlpJars/slave.jar" style="box-sizing: border-box; color: #5c3566; word-wrap: break-word;">slave.jar</a> -jnlpUrl http://192.168.0.103:8080/computer/slave1/slave-agent.jnlp -secret e15c60d05ca37d01d593abd6297d6e3f1bb3b9d91c3c97d9155262acea8c61d3 -workDir "d:\jenkins"  

7. Paste the command in command prompt. and hit enter 
  

8 . Afer successfully executed the command Jenkins slave will be online.



your ad