Reputation: 11
I'm trying to do automation at work through selenium webdriver using Eclipse and cucumber. I'm getting the below error while running my feature files
java.lang.Error: Unresolved compilation problem: driver cannot be resolved
As you can see below, in my Tests_Steps.java class I have declared the variable "driver" correctly. I have also assigned the object to the instance of a class(FirefoxDriver). Below is my code.
package stepDefinition;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Tests_Steps {
@Given("^User is on the Home Page$")
public void user_is_on_the_Home_Page() throws Throwable {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.get("http://www.gmail.com/login");
}
@When("^User Clicks on the Login$")
public void user_Clicks_on_the_Login() throws Throwable {
driver.findElement(By.xpath(".//*[@id='login']")).click();
}
@When("^User enters UserName and Password$")
public void user_enters_UserName_and_Password() throws Throwable {
driver.findElement(By.id("login")).sendKeys("ab24146_111");
driver.findElement(By.id("psw")).sendKeys("Password1");
driver.findElement(By.id("loginButton")).click();
}
@Then("^Message displayed LogIn Successfully$")
public void message_displayed_LogIn_Successfully() throws Throwable {
System.out.println("Login Successfully");
}
For some reason my driver variable is not being recognized on the second and the third step. I see red squiggly lines and when I hover my mouse on the red line it says "driver cannot be resolved" In the first step its working fine.
Can you guys please help me in what needs to be done.
Upvotes: 0
Views: 204
Reputation: 23825
java.lang.Error: Unresolved compilation problem: driver cannot be resolved
Actually you're declaring WebDriver
variable inside user_is_on_the_Home_Page()
locally, so this is limited and and would be available only for this method.
You should declare this variable globally which would be available for all of those methods as below :-
public class Tests_Steps {
WebDriver driver = null;
@Given("^User is on the Home Page$")
public void user_is_on_the_Home_Page() throws Throwable {
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.get("http://www.gmail.com/login");
}
------------
------------
}
Upvotes: 1
Reputation: 160
You have declared your variable inside user_is_on_the_Home_Page() method, thus it's scope is limited to that method and is destroyed when the method is complete.
Move to be an instance variable of the class and initialize it in a constructor.
Upvotes: 1