webdriver - JUnit4 test case won't continue -


i created test suite 2 test cases using selenium ide. exported suite java/junit4/webdriver.

the first test case allow user log-in site, member search after match found, access member's profile

the second test case: once in member profile, click on link 'donation' add pledge.

the test suite runs fine in selenium ide, hang in eclipse when executed suite. behavior in eclipse, first test case runs fine, 2nd case opens new browser, system required log-in (enter username , password).

i know shall do, test cases 2 continues without asking user log-in. appreciate , advises.

here test suite code break down 3 sections (i removed uid , pd, due site internal site)

-test suite runner file: searchdonoraddpledge    -test case1: searchdonorsuzy.class     -test case2: donoraddpledge.class 

failure trace message:

  1. org.openqa.selenium.staleelementreferenceexception: element not found in cache - perhaps page has changed since looked command duration or timeout: 30.12 seconds

  2. caused by: org.openqa.selenium.remote.errorhandler$unknownserverexception: element not found in cache - perhaps page has changed since looked 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 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();         }                   {           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();         }                   {           acceptnextalert = true;         }       }  } 

both testcases instantiate new driver in @before method; starts new browser instance fresh session, loses logged-in session details previous testcase:

driver = new firefoxdriver(); 

i'd recommend looking 2 alternative strategies:

  1. instantiate driver once per class, , put testcases same class rely on using same session.
  2. restructure testcases independent of each other.

1. keeping same session across multiple testcases

you can make code execution more efficient instantiating driver once per class, instead of separately every testcase. should think of efficiency saving, however; don't recommend using way of linking testcases testcase 2 relies on testcase 1 having run successfully, example.

2. making testcases independent

it's tempting construct testcases in sequence, testcase 1 (which includes logging in, example) , leads naturally on testcase 2 (which further actions). i'd warn lead problems. makes whole test suite more fragile - if there's problem in testcase 1, testcases afterwards fail. makes testing less flexible - can't run testcase 3 retest in isolation, , can't pick , choose individual testcases out of suite.

i'd recommend second strategy; example if testcase 1 testing login routine, have test login routine , log out again. , if testcase 2 testing functionality can reached after login - well, make log in can functionality it's interested in. fact testcase 1 log in not relevant testcase 2, , it's not problem either - in fact, can see opportunity start re-factoring testcases extract repeated code separate method call.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -