-1

セクション @After のメソッドがテスト後にブラウザーを閉じない理由を説明してもらえますか?

package TestCases;

import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class ScriptCase {

    private WebDriver driver;

    @Before
    public void startWeb() {
        WebDriver driver = new InternetExplorerDriver();

        driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
    }

    @After
    public void ShutdownWeb() {
        driver.close();
    }

    @Test
    public void startWebDriver(){

        Assert.assertTrue("Title is different from expected",
                driver.getTitle().startsWith("PixStack Photo Editor Free"));

    }
}

コードを @After からセクション @Test (最後まで) に直接移動すると、プロジェクトはブラウザーを正常に閉じます。プロジェクトは適切にコンパイルされています。

4

3 に答える 3

1

サンプル コードには、2 つの異なるdriver変数があります。1 つはstartWebメソッドに対してローカルで、ブラウザーのインスタンスを作成するために使用されます。もう 1 つの変数はクラス レベルにあり、インスタンス化されません。これは、ShutdownWebメソッドで使用しようとしているインスタンスです。driverこれを解決するには、セットアップ メソッドでローカル変数を再宣言しないでください。ウィット:

public class ScriptCase {

  private WebDriver driver;

  @Before
  public void startWeb() {
    // This is the line of code that has changed. By removing
    // the type "WebDriver", the statement changes from declaring
    // a new local-scope variable to use of the already declared
    // class scope variable of the same name.
    driver = new InternetExplorerDriver();
    driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
  }

  @After
  public void shutdownWeb() {
    driver.quit();
  }

  @Test
  public void startWebDriver(){

    Assert.assertTrue("Title is different from expected", driver.getTitle().startsWith("PixStack Photo Editor Free"));

  }
}

quitさらに、代わりにメソッドを使用するというアドバイスcloseは適切であり、その変更を上記のコードに含めました。

于 2013-07-30T23:42:05.133 に答える
0
public class ScriptCase {

    private WebDriver driver;

   @BeforeClass 
    public void startWeb() {
       driver = new InternetExplorerDriver(); 
       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
    }

    @AfterClass 
    public void ShutdownWeb() {
        driver.close();
        driver.quit();
    }

    @Test
    public void startWebDriver(){

        Assert.assertTrue("Title is different from expected",
                driver.getTitle().startsWith("PixStack Photo Editor Free"));

    }
}

これを試してみてください.....うまくいきます

于 2013-07-31T04:46:55.727 に答える
0

@ を使用するよりも、試す前に...

      @BeforeClass 
    baseUrl = "http://localhost:8080/";
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get(baseUrl);

and @AfterClass
  driver.quit();

だから、これを使ってみてください。

于 2013-07-30T17:50:57.913 に答える