即使已定义变量也无法识别变量
Unable to identify variable even though variable has been defined
我正在尝试使用 Eclipse 和 Cucumber 通过 selenium webdriver 在工作中实现自动化。 运行 我的功能文件
时出现以下错误
java.lang.Error: Unresolved compilation problem:
driver cannot be resolved
正如您在下面看到的,在我的 Tests_Steps.java class 中我已经正确地声明了变量 "driver"。我还将对象分配给 class(FirefoxDriver) 的实例。下面是我的代码。
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");
}
出于某种原因,我的驱动变量在第二步和第三步没有被识别。我看到红色的波浪线,当我将鼠标悬停在红线上时,它显示 "driver cannot be resolved" 第一步工作正常。
你们能帮我做些什么吗?
您已在 user_is_on_the_Home_Page() 方法中声明了您的变量,因此它的范围仅限于该方法,并在该方法完成时被销毁。
移动为class的实例变量并在构造函数中初始化它。
java.lang.Error: Unresolved compilation problem: driver cannot be resolved
实际上,您是在本地 user_is_on_the_Home_Page()
内声明 WebDriver
变量,因此这是有限的,并且仅适用于此方法。
您应该全局声明此变量,它可用于以下所有这些方法:-
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");
}
------------
------------
}
我正在尝试使用 Eclipse 和 Cucumber 通过 selenium webdriver 在工作中实现自动化。 运行 我的功能文件
时出现以下错误java.lang.Error: Unresolved compilation problem: driver cannot be resolved
正如您在下面看到的,在我的 Tests_Steps.java class 中我已经正确地声明了变量 "driver"。我还将对象分配给 class(FirefoxDriver) 的实例。下面是我的代码。
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");
}
出于某种原因,我的驱动变量在第二步和第三步没有被识别。我看到红色的波浪线,当我将鼠标悬停在红线上时,它显示 "driver cannot be resolved" 第一步工作正常。
你们能帮我做些什么吗?
您已在 user_is_on_the_Home_Page() 方法中声明了您的变量,因此它的范围仅限于该方法,并在该方法完成时被销毁。
移动为class的实例变量并在构造函数中初始化它。
java.lang.Error: Unresolved compilation problem: driver cannot be resolved
实际上,您是在本地 user_is_on_the_Home_Page()
内声明 WebDriver
变量,因此这是有限的,并且仅适用于此方法。
您应该全局声明此变量,它可用于以下所有这些方法:-
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");
}
------------
------------
}