Selenium Webdriver: How do I run multiple tests, one after the other in the same window?

25,780

Solution 1

If your tests are dependent, you can put them in the same class with dependsOnMethod={method1} defined on method2 so that the order is gauranteed. If it is between different classes, you might consider extending your AddNormalEE class from the LoginScript class.

To run tests in the same browser, your driver instance needs to be shared between your classes or it has to be the same in all your @Tests. Either make it static or consider having a threadlocal webdriver variable in case you plan to run parallely some day. In the above case, you can also have a method getDriver() in your loginScript which returns the driver object to the AddNormalEE class if static needs to be avoided.

As a general practice, it is good to have independent tests. You can make use of parallel runs to overcome the time issue with independent tests. Making login as a method and not a test since we are not asserting any behavior as per your code above. If I am testing login, I would have separate tests that test login functionality only.

Solution 2

Each test in a test class should perform the test using a new browser session, This can be easily done by putting the browser invoke and the kill process in a base Test class and extends this class in each test class.

Share:
25,780
Jason Krept
Author by

Jason Krept

Updated on June 13, 2020

Comments

  • Jason Krept
    Jason Krept almost 4 years

    My goal is to have a series of tests run one after the other. I would like to have a "login" script log the user in and then the following scripts kick off continuing in the same window/driver. I'm using TestNG so my test suite is setup in the testng.xml file if that helps.

    public class LoginScript {
    String username, password, siteid;
    private WebDriver driver;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();
    static Logger log = Logger.getLogger(LoginScript.class);
    
    
    @BeforeSuite (alwaysRun=true)
    @Parameters({ "url","username","password","site" })
    
    public void setUp(String env, String user, String pwd, String ste) throws Exception {
    username=user;
    password=pwd;
    siteid=ste;
    
    driver = new FirefoxDriver();
    driver.get(env);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    
    @Test
    public void testLoginScript() throws Exception {
    //Maximize window
    driver.manage().window().maximize();
    
    //Login
    driver.findElement(By.id("TBSiteID")).clear();
    driver.findElement(By.id("TBSiteID")).sendKeys(siteid);
    driver.findElement(By.id("TBUserName")).clear();
    driver.findElement(By.id("TBUserName")).sendKeys(username);
    driver.findElement(By.name("TBPassword")).clear();
    driver.findElement(By.name("TBPassword")).sendKeys(password);
    driver.findElement(By.name("Login")).click();
    Thread.sleep(2000);
    log.info("Found requested site");
    
    }
    
     @AfterSuite
     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 boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
      }
     }
    
     private String closeAlertAndGetItsText() {
      try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
    }
    }
    

    Next script that I would like to run:

    public class AddNormalEE {
    String username, password, siteid;
    private WebDriver driver;
    private boolean acceptNextAlert = true;
     private StringBuffer verificationErrors = new StringBuffer();
    static Logger log = Logger.getLogger(AddNormalEE.class);
    
    
    @BeforeSuite (alwaysRun=true)
    @Parameters({ "url","username","password","site" })
    
    public void setUp(String env, String user, String pwd, String ste) throws Exception {
    username=user;
     password=pwd;
    siteid=ste;
    
        //driver = new FirefoxDriver();
      //driver.get(env);
      //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    
    
    @Test
    public void testAddNormalEE() throws Exception {
    //Maximize window
    //driver.manage().window().maximize();
    
    
    
    @AfterSuite
    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 boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
    }
    
    private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
     }
    }  
    
  • Jason Krept
    Jason Krept over 9 years
    I'm not sure if I should be using one java file with all my tests inside, each denoted by @Test and run them in a sequence or if I should have a bunch of java files, dedicating one per test. The problem with having a file for each test is testng has no way that I know of that can kick off the java files in order, rather than simultaneously.
  • djangofan
    djangofan over 9 years
    TestNG is able to force the order. If you have a testng.xml file that defines your tests, if you put a "preserve-order" flag on that test set, then all the test classes you define in that set will execute in order.
  • Jason Krept
    Jason Krept over 9 years
    That only runs the test methods in order. If I have two classes defined, it runs two instances of the browser. One for each class.
  • Stuart Dobson
    Stuart Dobson about 6 years
    Why? Please give a reason