首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >即使已定义了变量,也无法识别变量

即使已定义了变量,也无法识别变量
EN

Stack Overflow用户
提问于 2016-10-17 16:58:31
回答 2查看 144关注 0票数 0

我试图通过使用Eclipse和黄瓜的selenium webdriver来实现工作中的自动化。在运行我的特性文件时,我得到了以下错误

java.lang.Error:未解决的编译问题:驱动程序无法解决

如下所示,在我的Tests_Steps.java类中,我正确地声明了变量“驱动程序”。我还将对象分配给类的实例(FirefoxDriver)。下面是我的密码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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");
    }

出于某种原因,在第二步和第三步,我的司机变量没有被识别。我看到红线,当我把鼠标悬停在红线上时,它说“司机无法解决”,在第一步,它的工作很好。

你们能帮我做些什么吗。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-17 17:22:50

java.lang.Error:未解决的编译问题:驱动程序无法解决

实际上,您是在本地的WebDriver中声明user_is_on_the_Home_Page()变量,因此这是有限的,并且只能用于此方法。

您应该全局声明此变量,该变量可用于所有这些方法,如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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");
  }

  ------------
  ------------
}
票数 1
EN

Stack Overflow用户

发布于 2016-10-17 17:07:58

您已经在user_is_on_the_Home_Page()方法中声明了变量,因此它的作用域仅限于该方法,并且在方法完成时被销毁。

移动为类的实例变量,并在构造函数中初始化它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40097883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文