I posted a question in the forum to ask how to make a test suite (with 2 testcases) run consistently without interruption. link to previous post
A helpful reply suggested that
Instantiate the driver once per class and put testcases into the same class which rely on using the same session.
the user also suggested to make the test cases independent from each other.
I have 2 test cases (in order to maintain the same login session I combined 2 test cases into one class)
case1: Authentication session log into the site, then search the member and access the member profile
case2: in the member profile, access the donor profile page, then add a pledge, then search the pledge amount by access the specific campaign page.
My question is: How to make the test cases independent of each other, for example when the log-in session failed, the suite can still execute the testcase2. My thought is I need to create separate driver instance in each test class (represent each test case), so When case1 fail, case2 can continue to run. Please advise me the proper way to make this work.
Here is my code for the test suite
driver to execute the test class
import org.junit.runner.RunWith; import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses
(
{
SearchDonorSuzy.class
}
)
public class searchDonorAddPledge
{
}
test cases code include Authentication, search member, access donor profile, add pledge and search the pledge amount.
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;
}
}
}