创建测试套件,测试用例彼此独立?

时间:2013-06-08 22:47:27

标签: junit4 test-suite

我在论坛上发布了一个问题,询问如何使测试套件(带有2个测试套件)始终如一地运行而不会中断。  link to previous post

有用的回复表明

  • 每个类实例化一次驱动程序并将测试用例放入其中 依赖于使用相同会话的类。

  • 用户还建议让测试用例彼此独立。

    我有2个测试用例(为了保持相同的登录会话,我将2个测试用例合并为一个类)

  • case1:验证会话登录到站点,然后搜索 会员并访问会员资料

  • case2:在会员资料中,访问捐赠者资料页面,然后添加 承诺,然后通过访问特定的搜索质量金额 活动页面。

    我的问题是:如何使测试用例彼此独立,例如当登录会话失败时,套件仍然可以执行testcase2。我的想法是我需要在每个测试类中创建单独的驱动程序实例(代表每个测试用例),所以当case1失败时,case2可以继续运行。请告诉我正确的方法来完成这项工作。

    这是我的测试套件代码

    执行测试类的驱动程序

import org.junit.runner.RunWith; import 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;
        }
      }

}

1 个答案:

答案 0 :(得分:0)

您应该声明您的webdriver是静态的,然后使用@BeforeClass来实例化它并登录到您的应用程序。您应该在每个测试类中独立完成此操作。因此,每个测试用例都将登录作为默认值。然后,您可以让每个测试方法(@Test)假定您已登录。

这可能真的不会给你你想要的分离。如果服务器拒绝让您登录,因为有人更改了您的密码,那么您的所有测试都将失败。

这不是你最大的问题。你最大的问题是你至少在隐喻方面在整个用户界面上浇注混凝土。在某些时候,您的开发人员可以选择将提交按钮更改为锚点,使用jquery-ui的按钮为每个按钮设置外观,然后仅在某些基本验证通过时使用javascript调用表单提交(这是很普通的)。如果发生这种情况,系统的行为将不会发生变化,并且UI看起来几乎相同,但您的测试将失败。可能很多测试。出于这个原因,以及许多其他人,专业的Java开发人员很少在距离selenium 10英尺范围内。它违反了大多数测试原则。 (注意:有这么多的Java开发人员,即使罕见使用工具也意味着有成千上万的人正在做这件事)。我不是使用selenium,而是使用jbehave查看BDD并通过api而不是GUI进行测试。

顺便说一句:你不应该使用import xxx。* - 它被允许但被认为是不好的做法。

相关问题