0
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class Test1 {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    System.setProperty("webdriver.ie.driver",  "D:/Development/ProgrammingSoftware/Testing/IEDriverServer.exe");

    WebDriver driver = new InternetExplorerDriver();
    baseUrl = "http://seleniumhq.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void test1() throws Exception {
    driver.get(baseUrl + "/download/");
    driver.findElement(By.linkText("Latest Releases")).click();
    driver.findElement(By.linkText("All variants of the Selenium Server: stand-alone, jar with dependencies and sources.")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}

同じセッションで IE を使用したいのですが、このコードは常に IE の新しいインスタンスを開きます。どうすればこの仕事を手に入れることができますか?

4

2 に答える 2

2

ドライバーを既存のセッションにアタッチすることはできないと思います。

テスト メソッドの実行が完了し、別のクラスまたはパッケージに存在する別のテスト メソッドを実行する場合は、現在のドライバーをメソッドに渡してメソッドを呼び出し、そこでドライバーの現在のインスタンスを使用できるようにします。 .

于 2013-01-25T11:56:39.663 に答える