Home page

Friday, July 19, 2019

Maven with cucumber

Practice URL: https://www.techlistic.com/p/selenium-practice-form.html

Editor: Eclipse


Follow below mentioned steps:


  1. Select Maven project to create new project
  2. Click Next on “Select project name and location” popup
  3. Click Next on “Select an Archetype” popup.
  4. Enter “AboutMe_techlistic_cucumber” in Group Id & Artifact Id and click Finish button.
  5. Right click project > Maven > Update Project: Check “Force Update of Snapshots/Release” checkbox and click OK button. Follow this step to avoid any conflict related to maven.
  6. Delete packages from src/main/java and src/test/java
  7. Create “Features” folder under project.
  8. Create “drivers” folder under project.
  9. Create 3 packages under src/test/java: a) pageObjects b) utilities c) stepDefinitions
  10. Right click “Fetures” folder > create new file. Give filename AboutMe.feature (Popup will open “Better editor support for '*.feature' files is available on the Marketplace.”)
  11. Install Cucumber Eclipse Plugin.
  12. Write Feature and scenario in AboutMe.feature file as follow
Feature: About Me

Scenario: Fill About Me form successfully
Given User Launch Chrome browser
When User opens URL "https://www.techlistic.com/p/selenium-practiceform.html"
And User enters Firstname as "sABC" and Lastname as "sXYZ" and Gender as "Male" and Experience as "7" and Date as "06-05-1983" and Profession as "Automation Tester" and AutomationTools as "Selenium Webdriver" and Continents as "Asia" and SeleniumCommand as "Wait Commands"
And Click on Button
And close browser



13. Create AboutMePage.java file under pageObjects package and copy paste following code:
package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class AboutMePage {
               
                public WebDriver ldriver;
               
                public AboutMePage(WebDriver rdriver){
                                ldriver = rdriver;
                                PageFactory.initElements(rdriver, this);
                }
               
               
               
                By txtFirstName = By.name("firstname");
                By txtLastName = By.name("lastname");
                By genderMale = By.id("sex-0");
                By genderFemale = By.id("sex-1");
                By experience7 = By.id("exp-6");
                By date = By.id("datepicker");
                By prefession = By.id("profession-1");
                By tool = By.id("tool-2");
                By continent = By.id("continents");
                By submit = By.id("submit");
               
                //Action methods
                public void setFirstName(String FirstName){
                                ldriver.findElement(txtFirstName).sendKeys(FirstName);
                }
               
                public void setLastName(String LastName){
                                ldriver.findElement(txtLastName).sendKeys(LastName);
                }

                public void setGender(String gender_value){
                                if(gender_value.equals("Male"))
                                                ldriver.findElement(genderMale).click();
                                else if(gender_value.equals("Female"))
                                                ldriver.findElement(genderFemale).click();
                }
               
                public void setExperience(String experience){
                                if(experience.equals("7"))
                                                ldriver.findElement(experience7).click();
                }
               
                public void setDate(String date_value){
                                ldriver.findElement(date).sendKeys(date_value);
                }
               
                public void setProfession(String profession_valule){
                                if(profession_valule.equals("Automation Tester"))
                                                ldriver.findElement(prefession).click();
                }
               
                public void setTool(String tool_value){
                                if(tool_value.equals("Selenium Webdriver"))
                                                ldriver.findElement(tool).click();
                }
               
                public void setContinet(String continet){
                                Select drpContinent = new Select(ldriver.findElement(continent));
                                drpContinent.selectByVisibleText(continet);
                }

                public void clickSubmit(){
                                ldriver.findElement(submit).click();
                }
}

14. Create steps file under “stepDefinitions” package with name “steps_AboutMe.java”.
15. Provide following dependencies in POM.xml
<dependencies>
              <dependency>
                     <groupId>junit</groupId>
                     <artifactId>junit</artifactId>
                     <version>4.12</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-core</artifactId>
                     <version>4.6.0</version>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-html</artifactId>
                     <version>0.2.7</version>
              </dependency>

              <dependency>
                     <groupId>net.sourceforge.cobertura</groupId>
                     <artifactId>cobertura</artifactId>
                     <version>2.1.1</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-java</artifactId>
                     <version>4.6.0</version>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-junit</artifactId>
                     <version>4.6.0</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-jvm-deps</artifactId>
                     <version>1.0.6</version>
                     <scope>provided</scope>
              </dependency>

              <dependency>
                     <groupId>net.masterthought</groupId>
                     <artifactId>cucumber-reporting</artifactId>
                     <version>4.8.0</version>
              </dependency>

              <dependency>
                     <groupId>org.hamcrest</groupId>
                     <artifactId>hamcrest-core</artifactId>
                     <version>2.1</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>org.seleniumhq.selenium</groupId>
                     <artifactId>selenium-java</artifactId>
                     <version>3.141.59</version>
              </dependency>

              <dependency>
                     <groupId>com.sun</groupId>
                     <artifactId>tools</artifactId>
                     <version>1.8</version>
                     <scope>system</scope>
                     <systemPath>C:\Program Files\Java\jdk1.8.0_221\lib\tools.jar</systemPath>
              </dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
</plugin>
</plugins>

</build>



16. Open “AboutMe.feature” file and run program by right clicking in the file > Run As > Cucumber Feature
17. Console will print all missing test steps in the form of methods. Copy all methods and paste inside steps_AboutMe.java file
18. Step 16 & 17 is for your understanding how to get ready made methods.
19. Remove content of the steps_AboutMe.java and copy paste following code:
package stepDefinitions;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import pageObjects.AboutMePage;

public class steps_AboutMe {
  
   public WebDriver driver;
   public AboutMePage AM;
  
   @Given("User Launch Chrome browser")
   public void user_Launch_Chrome_browser() {
          System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers/chromedriver.exe");
          driver = new ChromeDriver();
          AM = new AboutMePage(driver);
   }

   @When("User opens URL {string}")
   public void user_opens_URL(String url) {
       driver.get(url);
   }

   @When("User enters Firstname as {string} and Lastname as {string} and Gender as {string} and Experience as {string} and Date as {string} and Profession as {string} and AutomationTools as {string} and Continents as {string} and SeleniumCommand as {string}")
   public void user_enters_Firstname_as_and_Lastname_as_and_Gender_as_and_Experience_as_and_Date_as_and_Profession_as_and_AutomationTools_as_and_Continents_as_and_SeleniumCommand_as(String FirstName, String LastName, String gender_value, String experience, String date_value, String profession_valule, String tool_value, String continet, String string9) {
      AM.setFirstName(FirstName);
      AM.setLastName(LastName);
      AM.setGender(gender_value);
      AM.setExperience(experience);
      AM.setDate(date_value);
      AM.setProfession(profession_valule);
      AM.setTool(tool_value);
      AM.setContinet(continet);
   }

   @When("Click on Button")
   public void click_on_Button() {
          AM.clickSubmit();
   }

   @When("close browser")
   public void close_browser() {
      
   }

}

20. Create testRunner package under src/test/java
21. Create TestRun.java file under testRunner package and copy paste following code inside
package testRunner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions
   (
   features=".//Features/AboutMe.feature",
   glue="stepDefinitions",
   dryRun=false,
   monochrome=true, //remove unnecessary characters
   plugin={"pretty", "html:test-output"}
                
                
   )
public class TestRun {

}

22. Right click inside TestRun.java file > Run As > JUnit Test.




No comments:

Post a Comment