0
votes

I am trying to run a script in Eclipse using Firefox driver and selenium using Cucumber Gherkin format for BDD. I am getting a lot of exceptions when I run this on Junit this is my code as follows for the Java file.

annotation.java

package annotation; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 

public class annotation { 
   WebDriver driver = null; 
   @Given("^I am on Facebook login page$") 

   public void goToFacebook() { 
      driver = new FirefoxDriver(); 
      driver.navigate().to("https://www.facebook.com/"); 
   }

   @When("^I enter username as \"(.*)\"$") 
   public void enterUsername(String arg1) {   
      driver.findElement(By.id("email")).sendKeys(arg1); 
   }

   @When ("^I enter password as \"(.*)\"$") 
   public void enterPassword(String arg1) {
      driver.findElement(By.id("pass")).sendKeys(arg1);
      driver.findElement(By.id("u_0_v")).click(); 
   } 

   @Then("^Login should fail$") 
   public void checkFail() {  
      if(driver.getCurrentUrl().equalsIgnoreCase(
         "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
            System.out.println("Test1 Pass"); 
      } else { 
         System.out.println("Test1 Failed"); 
      } 
      driver.close(); 
   }

   @Then("^Relogin option should be available$") 
   public void checkRelogin() { 
      if(driver.getCurrentUrl().equalsIgnoreCase(
         "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
            System.out.println("Test2 Pass"); 
      } else { 
         System.out.println("Test2 Failed"); 
      } 
      driver.close(); 
   }
} 

These are the exceptions that I am receiving once I execute the test

Feature: annotation

#This is how background can be used to eliminate duplicate steps
Background: [90m# annotation\outline.feature:4[0m User navigates to Facebook Given I am on Facebook login page

#Scenario with AND Scenario: [90m# annotation\outline.feature:9[0m [90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\outline.feature:10) [0m [90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m

#This is how background can be used to eliminate duplicate steps
Background: [90m# annotation\outline.feature:4[0m User navigates to Facebook Given I am on Facebook login page

#Scenario with BUT Scenario: [90m# annotation\outline.feature:15[0m [90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\outline.feature:16) [0m [90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m [90mBut [0m[90mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m [1A [36mBut [0m[36mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m

java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\outline.feature:10)

java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\outline.feature:16)

This is my outline.feature file

Feature: annotation 
#This is how background can be used to eliminate duplicate steps 

Background: 
   User navigates to Facebook Given 
   I am on Facebook login page 

#Scenario with AND 
Scenario: 
   When I enter username as "TOM"
   And I enter password as "JERRY" 
   Then Login should fail 

#Scenario with BUT 
Scenario: 
   When I enter username as "TOM" 
   And I enter password as "JERRY" 
   Then Login should fail 
   But Relogin option should be available
2
Does replacing @Given("^I am on Facebook login page$") with @Before fix the problem?howlger
...or adding static before WebDriver driver = null;?howlger
Please show the outline.featureyong
Please share your feature fileAnkur Singh

2 Answers

1
votes

"Given" should be the first keyword on the line in the Background step, like so:

Background: User navigates to Facebook  
   Given I am on Facebook login page 
0
votes

I am able to run your scenarios in my system.Can you share the dependency/jars you are using. Also, driver.close() method in Then Statement (Login should fail ) will work fine for first scenario but in second scenario it would close the browser before the next Then (Relogin option should be available) statement.so you may want to remove that close method .