Question

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;
        }
      }

}
Was it helpful?

Solution

You should declare your webdriver static, then use @BeforeClass to instantiate it and log-on to your app. You should do this in each test class, independently of the others. So, each test case has the logon as a default given. You can then have each test method (@Test) assume that you're logged on.

This probably won't really give you the separation you want though. If the server is refusing to log you on because someone has changed your password then all of your tests are going to fail.

This isn't your biggest problem though. Your biggest problem is that you're pouring concrete all over your UI, metaphorically at least. Your developers may choose, at some point, to change the submit buttons to anchors, to skin each one using jquery-ui's button, and then use javascript to invoke the form submit only if some basic validation has passed (this is very common). If this happens the behavior of your system will not have changed, and the UI will look pretty much the same, but your tests will fail. Probably many tests. For this reason, and many others, professional java developers rarely go within 10 feet of selenium. It violates most of the principles of testing. (note: there are so many java developers, that even rare use of a tool can mean that there are tens of thousands of people doing exactly that). Rather than use selenium, I'd take a look at BDD using jbehave and testing through your api rather than your GUI.

BTW: You shouldn't use import xxx.* -- it's allowed but considered bad practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top