2

Test Steps

public class TestSmoke {
        WebDriver driver;
        @Given("^open firefox and start application$")jjj
        public void open_firefox_and_start_application() throws Throwable {
            driver=new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
            driver.get("https://example.com");
            
        }
    **Scenario 1**
        @When("^I click on Login$")
        public void I_click_on_Login() throws Throwable {
            driver.findElement(By.xpath("//a[contains(.,'Login')]")).click();
            
        }
    
        @When("^enter valid \"([^\"]*)\" and valid \"([^\"]*)\"$")
        public void enter_valid_and_valid(String uname, String pass) throws Throwable {
            driver.findElement(By.id("Username")).sendKeys(uname);
            driver.findElement(By.id("Password")).sendKeys(pass);
            
        }
    
        @Then("^Click on login and User should be able to login successfully$")
        public void Click_on_login_and_User_should_be_able_to_login_successfully() throws Throwable {
            driver.findElement(By.id("loginUser")).click();
            
        }

jkjbjkkjhjknjkbjkhjkbjbjbbjbnmbbnmb

        **Scenario 2:**
        @Given("^Click on shop for carts$")
        public void Click_on_shop_for_carts() throws Throwable {
            hhjbhbhgjbkjbhlhihjbhbb
            driver.findElement(By.xpath("//span[text()='Shop for Parts']")).click();
            
        }
    
        @Given("^select plates$")
        public void select_plates() throws Throwable {
            driver.findElement(By.xpath("//a[contains(.,'Plates ')]")).click();
            
        }
    
        
        @When("^I click on drsired product$")
        public void I_click_on_drsired_product() throws Throwable {
            driver.findElement(By.xpath("//a[@data-itemnumber='PLT01096096046']")).click();
        }
    
        @When("^click on item increment$")
        public void click_on_item_increment() throws Throwable {
            WebElement ele=driver.findElement(By.xpath("//i[contains(@class,'fa fa-caret-up')]"));
            for(int i=0;i<=3;i++)
            {
                ele.click();
            }
            
        }
    
        @When("^Click on buy now$")
        public void Click_on_buy_now() throws Throwable {
            driver.findElement(By.xpath("//button[contains(.,'Buy Now')]")).click();
            
        }
    
        @Then("^Product should be added to the cart successfully$")
        public void Product_should_be_added_to_the_cart_successfully() throws Throwable {
            
            
        }

Feature File Feature: Test test Smoke scenario

 Scenario: Test login with valid credentials
    Given open firefox and start application
    When I click on Login
    And enter valid "s@yopmail.com" and valid "passw0rd"
    Then Click on login and User should be able to login successfully
    
    
    Scenario: Test shop for cart
    Given Click on shop for carts
    And select plates
    When I click on drsired product 
    And click on item increment 
    And Click on buy now
    Then Product should be added to the cart successfully
  

Test runner

@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"})
public class TestRunnr { 

While i am running this cucumber script its throwing an NullPointer Exception :

java.lang.NullPointerException
    at steps.testmoke.Click_on_shop_for_carts(testSmoke.java:47)
    at ?.Given Click on shop for carts(MyApplication.feature:11)

First scenario is executing successfully but second scenario is not executing.I am login in a ecommerce website and try to click on shop for parts .

Rakesh kumar
  • 157
  • 2
  • 3
  • 9

1 Answers1

4

Each scenario creates a fresh instance of all the step definitions. In your case you instantiate the driver in the Given step public void open_firefox_and_start_application() so the first scenario is successful. Now for the second scenario a new instance of your class has a webdriver which is null and you are not calling the earlier step to instantiate it.

You can use a static webdriver, but you will run into issues with parallel tests. If you are planning for parallel tests look up ThreadLocal to make sure your webdriver is attached to the specific thread.

Another way could be to move the login tests to a separate file. For the other scenarios move the login steps into the Background cucumber tag. This way the webdriver will be instantiated for each scenario. But you will need to decide if you want to keep logged in across scenarios, delete cookies or a new browser for each scenario.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • i got your suggestion but i am completely new to cucumber so i dont have idea how to implement these all......can you share some piece of code that help me out – Rakesh kumar Apr 01 '17 at 03:44
  • 2
    Just add the keyword static in front of the webdriver declaration. – Grasshopper Apr 01 '17 at 13:45