我使用 Selenium IDE 创建了一个包含 2 个测试用例的测试套件。我将该套件导出为 Java/JUnit4/WebDriver。
第一个测试用例允许用户登录网站,找到匹配后进行会员搜索,访问会员的个人资料
第二个测试案例:在会员资料中,点击“捐赠”链接添加一个承诺。
测试套件在 Selenium IDE 中运行良好,但是当我执行该套件时它在 Eclipse 中挂起。在 Eclipse 中的行为,第一个测试用例运行良好,第二个用例打开一个新的浏览器,系统需要登录(输入用户名和密码)。
我想知道我该怎么做,所以测试用例 2 继续进行而不要求用户登录。感谢您的帮助和建议。
这是我的测试套件代码分为 3 个部分(我删除了 uid 和 pd,因为该站点是一个内部站点)
-test suite runner file: searchDonorAddPledge
-test case1: searchDonorSuzy.class
-test case2: DonorAddPledge.class
失败跟踪消息:
org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素 - 页面可能在查找后已更改 命令持续时间或超时:30.12 秒
原因:org.openqa.selenium.remote.ErrorHandler$UnknownServerException:缓存中未找到元素 - 可能页面在查找后已更改构建信息:版本:'2.31.0',修订:'1bd294d',时间:'2013-02-27 20:53:56'
跑步者档案:
import org.junit.runners.Suite;
import org.junit.runner.RunWith;
@RunWith(Suite.class)
@Suite.SuiteClasses
(
{
SearchDonorSuzy.class,
DonorAddPledge.class
}
)
public class searchDonorAddPledge { }
测试用例 1 代码:
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 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
public void testSearchDonorSuzy() throws Exception {
// set overall speed of the test case
// ERROR: Caught exception [ERROR: Unsupported command [setSpeed | 4000 | ]]
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.findElement(By.cssSelector("input[type=\"image\"]")).click();
driver.findElement(By.linkText("Balagia, Suzy")).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;
}
}
}
测试用例2代码:
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 DonorAddPledge
{
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
public void testDonorAddPledge() throws Exception
{
driver.get(baseUrl + "/?nd=db_member&account_id=942&nowwritestate=i1110536420226242");
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();
}
@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;
}
}
}