Tests in WebDriver Selenium suite are used to setup further tests. Is there some way I can make my tests not so dependent on previous 'setup' tests?

Viewed 81

I am fairly new to software testing (3 -4 years) and I work as a sole tester in an organisation. Unfortunately I have not have a mentor to help me when building the automated test suite.

The main issue I want to overcome is tests failing because a previous test failed. This is mainly because the tests in the WebDriver Selenium suite are used to setup further tests. Is there some way I can make my tests not so dependent on previous 'setup' tests? Let me describe a typical scenario:

To create a transaction, the following things are prerequisites and must be set up in the system first. These are all done as individual tests on the site first:

  • Source Country
  • Source Currency
  • Destination Country
  • Destination Currency
  • Commission charge
  • Agent profile
  • Sender profile
  • Receiver profile

Only then can I run the tests to actually create a transaction.In some cases, there are more steps involved in setting up the system before a tests can be run. The problem arises when, for example, the Destination Currency test fails. This will cause all subsequent tests to fail.

What can I do to make the tests more independent so if a previous 'setup' test fails for any valid reason, it doesn't affect the remainder of the tests? I just want to follow best test practices and cannot find the answer to this question anywhere online.

Many thanks in advance.

1 Answers

If your tests can pass independently then it can be done using cucumber-guice injection to independently open browser for each scenario.

You may have to arrange your tests as scenarios like below

 Scenario: set source country
    Given source country is setup
 
  Scenario: set source currency
    Given source currency is setup

OR you can use example feature of cucumber. In this method you may to do extra parsing in step defs depending on how much different it is to set up each source

  Scenario Outline:  login registration 
      
      Given these <source> are set up with <details>
      
        Examples:
        |source|details|
        |"country" |"xyz"|
        |"currency" |"abc"|      
        ...
        ...

cucumber-guice will open one browser for each scenario in both above methods ( cucumber consider each example line a scenario)

Now the actual guice implementation. To use guice you will need cucumber-guice dependency

 <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-guice</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>4.2.3</version>
      <scope>test</scope>
    </dependency>

These are the classes that I use to independently open browser for each scenario. In a chromemanager class you do

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

public class ChromeManager extends DriverManager {

     protected ChromeDriver driver;
    
         @Override
         protected WebDriver createDriver() {

                    System.setProperty("webdriver.chrome.driver", "/Users/../chromedriver");
                    return driver = new ChromeDriver(ChromeDriverService.createDefaultService());
        }

        @Override
         public WebDriver getDriver() {
                 if (driver == null) {
                             this.createDriver();
                }
                return driver;
        }

}

Driverfactory class

import org.openqa.selenium.WebDriver;

public class DriverFactory {
        public WebDriver getManager() {

            return new ChromeManager().getDriver();
        }
}

Drivermanager class

import org.openqa.selenium.WebDriver;

     public abstract class DriverManager {

                     protected abstract WebDriver createDriver();
                     public abstract WebDriver getDriver();
     }

and then in a global class

import org.openqa.selenium.WebDriver;
import io.cucumber.guice.ScenarioScoped;  
 
 @ScenarioScoped
    
          public class Global {
          public WebDriver driver;
           public WebDriverWait wait;
        
          public Global() {
                driver = new DriverFactory().getManager();
                wait = new WebDriverWait(driver, 3000);
            //any class you instantiate here will be injected in below initialIT class for each scenario
          }
    
    }

and finally your test class

import com.google.inject.Inject;     
import com.test.support.Global;    


public class InitialIT  {

         public static  ChromeDriver driver ;
                     
        @Inject
        Global global;
            
        
      @Test
       @Given("source country is setup")
        public void setCountry() throws MalformedURLException {                 
                global.driver.get(yourSite);                   
       }

       @Test
       @Given("source currency is setup")
        public void setCurrency() throws MalformedURLException {                 
                global.driver.get(yourSite);                   
       }
         
         @After
        public void closeBrowser() {    
             global.driver.quit();
      }

}
Related