Getting Null pointer exception



  • I am new to Cucumber and I am trying a basic cucumber test.

    I am getting the below error while running the test.

    java.lang.NullPointerException at stepDefinitions.Test_steps.the_user_enters_the_username(Test_steps.java:39) at ✽.the user enters the "abcd@gmail.com" username(src/test/java/resources/features/Test.feature:16)

    I have added 2 Scenario Outlines in a single Feature file.

    Please find below Feature file.

    Feature: creating a sample project 
    
    Scenario Outline: Login to google and search different values 
    
        Given user open browser and launches "<Search>" 
        When the login page is displayed 
        Examples: Search 
            |       Search      | 
            |       https://www.facebook.com/   |       
    Scenario Outline: search user 
    
        When the user enters the "<username>" username 
        And the user enters the "<password>" password 
        When the user clicks on login 
        Then search should login successfully. 
        Then closes the browser 
        Examples: username and password 
            |username       | password  |
            |abcd@gmail.com | Testing   |
    

    Please find below my Step Definition:

    Code:

    @Given("^user open browser and launches \"([^\"]*)\"$")
    
    public void user_open_browser_and_launches(String url) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            driver = new ChromeDriver();
    
            driver.get(url);
            driver.manage().window().maximize();
    
        }
    
        @When("^the login page is displayed$")
        public void the_login_page_is_displayed() {
    
                driver.getTitle();
            System.out.println(driver.getTitle());
    
        }
    
        @When("^the user enters the \"([^\"]*)\" username$")
        public void the_user_enters_the_username(String username) {
    
            System.out.println(username);
    
            driver.findElement(By.xpath("//input[@id='email']")).sendKeys(username);
    
        }
    
        @And("^the user enters the \"([^\"]*)\" password$")
        public void the_user_enters_the_password(String password) {
    
            driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
    
        }
    
        @When("^the user clicks on login$")
        public void the_user_clicks_on_login() {
    
            driver.findElement(By.xpath("//input[@type='submit']")).click();
    
        }
    
        @Then("^search should login successfully\\.$")
        public void search_should_login_successfully() {
    
            System.out.println("Page is loaded successfully");
    
        }
    
        @Then("^closes the browser$")
        public void closes_the_browser() throws InterruptedException {
            // Write code here that turns the phrase above into concrete actions
            Thread.sleep(3000);
            driver.quit();
    
    
        }
    

    Could you please let me know where I am going wrong.

    Thanks in Advance.



  • The way I execute my Automation code i find some flows in your way: 1. If you have a website to automate then there is no need to use 2 Scenario Outline's. 2. Single scenario outline should have all scenarios required to automate.

    I have divided your automation task in 3 parts 1. Feature File 2. Step Definition 3. Logical Implementation

    Feature File:

    Scenario Outline: Login to google and search different values

    Given user open browser and launches "<Search>"
    When the login page is displayed
    When the user enters the "<username>" username
    And the user enters the "<password>" password
    When the user clicks on login
    Then search should login successfully.
    Then closes the browser
    
    Examples:
      | Search                    | username       | password |
      | https://www.facebook.com/ | abcd@gmail.com | Testing  |
    

    StepDefinition:

    package com.stepdefinition;

    import com.ibepages.StackExchangePage;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class StackExchange {
    
    
        StackExchangePage stackExchangePage = new StackExchangePage();
    
    
        @Given("^user open browser and launches \"([^\"]*)\"$")
        public void user_open_browser_and_launches(String url) throws Throwable {
            stackExchangePage.openURL(url);
        }
    
        @When("^the login page is displayed$")
        public void the_login_page_is_displayed() throws Throwable {
    
        }
    
        @When("^the user enters the \"([^\"]*)\" username$")
        public void the_user_enters_the_username(String username) throws Throwable {
            stackExchangePage.enterUserName(username);
        }
    
        @When("^the user enters the \"([^\"]*)\" password$")
        public void the_user_enters_the_password(String password) throws Throwable {
            stackExchangePage.enterPassword(password);
        }
    
        @When("^the user clicks on login$")
        public void the_user_clicks_on_login() throws Throwable {
    
        }
    
        @Then("^search should login successfully\\.$")
        public void search_should_login_successfully() throws Throwable {
    
        }
    
        @Then("^closes the browser$")
        public void closes_the_browser() throws Throwable {
    
        }
    
    }    
    

    Logical Implementation:

    package com.ibepages;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class StackExchangePage {
    
        WebDriver localDriver;
    
        public StackExchangePage(){
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/chromedriver");
            localDriver = new ChromeDriver();
        }
    
        public void openURL(String url) throws InterruptedException {
            Thread.sleep(2000);
            System.out.print("the URL is --> " + url);
            localDriver.navigate().to(url);
            Thread.sleep(3000);
        }
    
        public void enterUserName (String username) {
            localDriver.findElement(By.xpath("//input[@id='email']")).clear();
            localDriver.findElement(By.xpath("//input[@id='email']")).sendKeys(username);
        }
    
        public void enterPassword(String password) {
            localDriver.findElement(By.xpath("//input[@id='pass']")).clear();
            localDriver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
        }
    
    
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2