0
\$\begingroup\$

I'm trying to find out if it is possible to simplify my framework a little bit more. The current solution works, but it does cause some code duplication for me and some other issues that might come down the road.

public class Browsers {

  private String Browser;
  private String Windows;
  private String Ver;

  protected WebDriver driver;

  @BeforeTest
  public void BeforeMethod() {

    DriverFactory driverfactory = new DriverFactory();
    driver = driverfactory.createdriver(Browser);
  }

  @AfterTest
  public void AfterMethod() {}

  public Browsers(String Browser, String Windows, String Ver) {
    this.Browser = Browser;
    this.Windows = Windows;
    this.Ver = Ver;

  }


  @DataProvider
  public static Object[][] dataProviderMethod() {
    return new Object[][] {{"FF", "WIndows", "10"}, {"Chrome", "Windows", "10"}};
  }

}


public class DriverFactory {

  private WebDriver driver;

  public WebDriver createdriver(String Browser) {
    if (Browser.contains("FF")) {

      driver = new FirefoxDriver();
    }

    else {
      driver = new ChromeDriver();
    }
    return driver;
  }


}


public class LoginTests extends Browsers {


  @Factory(dataProvider = "dataProviderMethod")
  public LoginTests(String Browser, String Windows, String Ver) {
    super(Browser, Windows, Ver);
  }


  @Test
  public void LoginValid() {

  }

  @Test
  public void LoginInvalid() {

  }


}

Problem

Currently, I'm able to run tests when I create a factory on each of the test classes.

I have to write a constructor to all the test classes that I create. Which causes me some code duplication. When I have 100 tests then it will be quite a lot. Is it possible to avoid it.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
  • First of all, calling your super class "Browsers" is wrong, because it doesn't have anything to do with browsers. It is probably a TestBase class, if your Test extends it.
  • You don't have to have duplicates and you don't have to have a constructor in your base class. Tests doesn't care about what browser it is executed on, so you should handle this information in TestBase class (currently Browsers). Personally I handle it in configuration/properties file. In that case I can switch easily and modify values from continuous integration server (i.e TeamCity).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.