【问题标题】:How to set up Selenium 2.x Test Suites in the same Webdriver in Eclipse (JUnit4)如何在 Eclipse (JUnit4) 的同一个 Webdriver 中设置 Selenium 2.x 测试套件
【发布时间】:2014-03-17 13:34:24
【问题描述】:

我已经在堆栈上筛选了一段时间的答案,并且遇到了一些似乎可以完成或至少接近的工作示例。

我想要做的就是将 selenium 测试用例导出为 JUnit4 文件,将它们带入 eclipse 并根据需要进行修改,然后能够在测试套件中添加或删除它。当测试套件运行时,它应该打开一个 webdriver 窗口并运行每个测试用例,就像它在基本 firefox 窗口上的 selenium IDE 中运行一样。唯一的问题似乎是 webdriver 类。我尝试在测试用例类之间引用它,这似乎可以让窗口保持打开而不会引发错误,但是第二个测试用例永远不会运行,就像它卡在一个循环中,或者停在@Before

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Suite.class)
@SuiteClasses({TestCase1.class,TestCase2.class})
public class RunTestSuite {

}

^^^ 测试套件示例^^^

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.support.ui.Select;

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

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.google.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testOpen() throws Exception {
    driver.get(baseUrl + "");
  }

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

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

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

^^^ 测试用例一^^^

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.support.ui.Select;

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

  @Before
  public void setUp() throws Exception {
    if (TestCase1.driver != null) {
      driver = TestCase1.driver;
  } else {
      driver = new FirefoxDriver();
  }
    baseUrl = "http://www.google.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testOpen() throws Exception {
    driver.get(baseUrl + "");
  }

@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 boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

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

^^^ 测试用例 2 ^^^

感谢您提供的任何帮助!

【问题讨论】:

    标签: eclipse selenium junit4 test-suite


    【解决方案1】:

    您必须将驱动程序实例的访问修饰符从私有更改为静态公共,以便第二个类可以访问它。 还要确保两个类在同一个包中。

    TestCase1 类应该是这样的

    public static WebDriver driver;
    

    我试过了,它很适合我。

    【讨论】:

      猜你喜欢
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多