【发布时间】:2013-06-04 23:22:13
【问题描述】:
我在论坛上发布了一个问题,询问如何使测试套件(包含 2 个测试用例)持续运行而不会中断。 link to previous post
一个有用的回复建议
每个类实例化一次驱动程序并将测试用例放入同一个类中 依赖于使用相同会话的类。
-
用户还建议让测试用例相互独立。
我有 2 个测试用例(为了保持相同的登录会话,我将 2 个测试用例合并为一个类)
case1:身份验证会话登录到站点,然后搜索 会员并访问会员资料
-
case2:在会员资料中,访问捐助者资料页面,然后添加 质押,然后通过访问特定的搜索质押金额 活动页面。
我的问题是:如何使测试用例相互独立,例如当登录会话失败时,套件仍然可以执行测试用例2。我的想法是我需要在每个测试类中创建单独的驱动程序实例(代表每个测试用例),这样当case1失败时,case2可以继续运行。请告诉我正确的方法来完成这项工作。
这是我的测试套件代码
执行测试类的驱动程序
导入 org.junit.runner.RunWith; 导入 org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses
(
{
SearchDonorSuzy.class
}
)
public class searchDonorAddPledge
{
}
测试用例代码包括认证、搜索会员、访问捐助者资料、添加质押和搜索质押金额。
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 SearchDonorSuzy
{
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 = "https://jlaustin.tcheetah.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
/*
*test case 1: login + search member
*/
@Test
public void testSearchDonorSuzy() throws Exception {
driver.get(baseUrl + "/?html=openid");
driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
driver.findElement(By.id("edit-name")).clear();
driver.findElement(By.id("edit-name")).sendKeys("username");
driver.findElement(By.id("edit-pass")).clear();
driver.findElement(By.id("edit-pass")).sendKeys("password");
driver.findElement(By.id("edit-submit")).click();
driver.findElement(By.id("cmp_admin")).click();
driver.findElement(By.id("quicksearch_anchor")).click();
driver.findElement(By.cssSelector("img[alt=\"Member\"]")).click();
driver.findElement(By.id("search_name")).clear();
driver.findElement(By.id("search_name")).sendKeys("suzy");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("input.btn")).click();
driver.findElement(By.linkText("Balagia, Suzy")).click();
/*
* test case 2: access donor's profile and add a pledge
*/
driver.findElement(By.xpath("(//a[contains(text(),'Donor')])[2]")).click();
driver.findElement(By.linkText("Campaign Manager")).click();
new Select(driver.findElement(By.id("campaign_id"))).selectByVisibleText("A Christmas Affair 2012");
driver.findElement(By.xpath("//a[contains(text(),'Add\n pledge')]")).click();
driver.findElement(By.id("pledge_amount")).clear();
driver.findElement(By.id("pledge_amount")).sendKeys("100.00");
driver.findElement(By.id("pledge_notes")).clear();
driver.findElement(By.id("pledge_notes")).sendKeys("test pledge");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();
/*
* search donation amount in donation campaign page
*/
driver.findElement(By.linkText("Donor")).click();
driver.findElement(By.linkText("A Christmas Affair 2013")).click();
new Select(driver.findElement(By.name("campaign_id"))).selectByVisibleText("A Christmas Affair 2012");
driver.findElement(By.linkText("Donors")).click();
driver.findElement(By.id("search_name")).clear();
driver.findElement(By.id("search_name")).sendKeys("suzy");
driver.findElement(By.cssSelector("input[type=\"image\"]")).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;
}
}
}
【问题讨论】:
-
你的问题是什么?
-
@Engineer Dollery。很抱歉造成混淆,我在原始帖子中以粗体添加了我的问题。请给我一些建议。谢谢!
标签: junit4 test-suite