I created a test suite with 2 test cases using Selenium IDE. I exported the suite as Java/JUnit4/WebDriver.
the first test case allow user to log-in the site, do a member search after the match is found, access the member's profile
the second test case: once in the member profile, click on the link 'donation' to add a pledge.
The test suite runs fine in Selenium IDE, but it hang up in Eclipse when I executed the suite. behavior in Eclipse, the first test case runs fine, the 2nd case opens up a new browser, the system required log-in (enter username and password).
I would like to know what shall I do, so the test cases 2 continues without asking user to log-in. I appreciate your help and advises.
Here is my test suite code break down into 3 sections (I removed uid and pd, due to the site is a internal site)
-test suite runner file: searchDonorAddPledge
-test case1: searchDonorSuzy.class
-test case2: DonorAddPledge.class
failure trace message:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: 30.12 seconds
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element not found in the cache - perhaps the page has changed since it was looked up Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56'
runner file:
import org.junit.runners.Suite;
import org.junit.runner.RunWith;
@RunWith(Suite.class)
@Suite.SuiteClasses
(
{
SearchDonorSuzy.class,
DonorAddPledge.class
}
)
public class searchDonorAddPledge { }
testcase 1 code:
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;
}
}
}
testcase2 code:
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;
}
}
}