Публикувах въпрос във форума, за да попитам как да накарам тестов пакет (с 2 тестови случая) да работи последователно без прекъсване. връзка към предишна публикация
Полезен отговор предполага това
Създайте екземпляр на драйвера веднъж на клас и поставете тестови случаи в същия клас, който разчита на използването на една и съща сесия.
потребителят също предложи тестовите случаи да бъдат независими един от друг.
Имам 2 тестови случая (за да поддържам същата сесия за влизане, комбинирах 2 тестови случая в един клас)
case1: Сесия за удостоверяване влезте в сайта, след това потърсете члена и влезте в профила на члена
case2: в профила на члена отворете страницата с профила на дарителя, след това добавете вноска, след което потърсете сумата на вноската чрез достъп до страницата на конкретната кампания.
Моят въпрос е: Как да направя тестовите случаи независими един от друг, например, когато сесията за влизане е неуспешна, пакетът все още може да изпълни testcase2. Мисълта ми е, че трябва да създам отделен екземпляр на драйвер във всеки тестов клас (представляващ всеки тестов случай), така че когато 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;
}
}
}