【发布时间】:2014-03-17 13:34:24
【问题描述】:
我已经在堆栈上筛选了一段时间的答案,并且遇到了一些似乎可以完成或至少接近的工作示例。
- Use same web driver throughout selenium suite
- Before and After Suite execution hook in jUnit 4.x
- JUnit Test Suite for Selenium 2
我想要做的就是将 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