Java 即使已定义变量,也无法识别变量
我正试图通过SeleniumWebDriver使用Eclipse和cucumber实现工作自动化。运行功能文件时出现以下错误 java.lang.Error:未解决的编译问题: 无法解析驱动程序 正如您在下面看到的,在我的Tests_Steps.java类中,我正确地声明了变量“driver”。我还将该对象分配给一个类(FirefoxDriver)的实例。下面是我的代码Java 即使已定义变量,也无法识别变量,java,eclipse,selenium,selenium-webdriver,cucumber,Java,Eclipse,Selenium,Selenium Webdriver,Cucumber,我正试图通过SeleniumWebDriver使用Eclipse和cucumber实现工作自动化。运行功能文件时出现以下错误 java.lang.Error:未解决的编译问题: 无法解析驱动程序 正如您在下面看到的,在我的Tests_Steps.java类中,我正确地声明了变量“driver”。我还将该对象分配给一个类(FirefoxDriver)的实例。下面是我的代码 package stepDefinition; import java.util.concurrent.TimeUnit;
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");
}
由于某些原因,在第二步和第三步中没有识别到我的驱动程序变量。我看到红色的曲线,当我将鼠标悬停在红线上时,第一步显示“驱动程序无法解决”,工作正常
你们能帮我做些什么吗。你们已经在user\u中声明了变量在\u Home\u Page()方法中,因此它的作用域仅限于该方法,并在该方法完成时被销毁
移动为类的实例变量,并在构造函数中初始化它 您已经在用户_中声明了您的变量是在_Home_Page()方法上的,因此它的作用域仅限于该方法,并且在该方法完成时被销毁 移动为类的实例变量,并在构造函数中初始化它 错误:未解决的编译问题:无法解决驱动程序 实际上,您正在本地声明
WebDriver
中的user\u在\u主页()上的变量,因此这是有限的,并且仅适用于此方法
您应该全局声明此变量,该变量可用于所有这些方法,如下所示:-
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");
}
------------
------------
}
错误:未解决的编译问题:无法解决驱动程序
实际上,您正在本地声明WebDriver
中的user\u在\u主页()上的变量,因此这是有限的,并且仅适用于此方法
您应该全局声明此变量,该变量可用于所有这些方法,如下所示:-
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");
}
------------
------------
}
这是因为方法中定义的变量只存在于该方法中。非常感谢!成功了!!这是因为方法中定义的变量只存在于该方法中。非常感谢!成功了!!