2016-10-18 6 views
0

Eclipseとキュウリを使ってセレンのwebdriverを使って作業を自動化しようとしています。私の機能ファイルを実行中に下記見ることができるように私は私のTests_Steps.javaクラスで私が正しく変数「ドライバ」を宣言した、変数が定義されていても変数を特定できません

java.lang.Error: Unresolved compilation problem: driver cannot be resolved

を以下のエラーを取得しています。また、オブジェクトをクラスのインスタンス(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"); 
    } 

何らかの理由で、私のドライバ変数が2番目と3番目のステップで認識されません。私は赤い波状の線を見て、私は赤い線の上にマウスを置いたとき、それは "ドライバは解決することはできません"と言う最初のステップでは正常に動作します。

あなたは何をする必要があるか私を助けてもらえますか?

+2

方法で定義された変数は唯一、そのメソッド内に存在するためであろうと。 – immibis

+0

ありがとう!出来た!! –

答えて

1

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"); 
    } 

    ------------ 
    ------------ 
} 
+1

ありがとう!出来た!! –

1

あなたの変数はuser_is_on_the_Home_Page()メソッドの中に宣言しています。したがって、スコープはそのメソッドに限定され、メソッドが完了すると破棄されます。

クラスのインスタンス変数に移動し、コンストラクタで初期化します。

+0

ありがとう!出来た!! –

関連する問題