0

I have a selenium framework with a BaseTest class. In the BaseTest class we instantiate a ChromeDriver instance which contains various arguments. These are part of the @BeforeMethod (using TestNG). My tests then extend the BaseTest class.

What I want to do for a specific test is to be able to add the --incognito argument to my ChromeDriver instance. How would I go about doing this?

BaseTest code:

public class BaseTest {
protected WebDriver driver;

@BeforeMethod
public void beforeMethod(Method method) {
    System.out.println("Running " + this.getClass().getName() + "#" + method.getName());
    d = new org.openqa.selenium.Dimension(1350, 660);
    if (browser.equals("firefox")) {
        System.out.println("Reached FF");
        instantiateFirefoxDriver();
    } else if (browser.equals("chrome")) {
        System.out.println("Reached Chrome");
        instantiateChromeDriver();
    } else if (browser.equals("safari")) {
        System.out.println("Reached Safari");
        instantiateSafariDriver();
    }
}

private void instantiateChromeDriver() {
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("disable-plugins");
    chromeOptions.addArguments("disable-extensions");
    chromeOptions.addArguments("--headless");
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--disable-dev-shm-usage");
    this.driver = new ChromeDriver(chromeOptions);
    driver.manage().window().setSize(d);
}

//Some more code......`

Example of a Test Class here:

public class FinanceScholarshipTest extends BaseTest {

private String financeUserUsername = TestUserConfig.STANDARD_USER_USERNAME;
private String financeUserPassword = TestUserConfig.STANDARD_USER_PASSWORD;
private String financeSecondUsername = TestUserConfig.CURRIC_USER_USERNAME;
private String financeSecondUserPassword = TestUserConfig.CURRIC_USER_PASSWORD;
private String studentNo = "1982076";

private CommonNavigation commonNavigation = new CommonNavigation();
private DataGenerationHelper randomData = new DataGenerationHelper();
private FinanceDBQueries dbQueries = new FinanceDBQueries();

@Test
public void shouldGenerateNewScholarshipForCurrentStudent() throws Exception {
    // Given
    HomePage homePage = commonNavigation.shouldAccessHomepageForSLSTestEnvironment(driver, financeUserUsername, financeUserPassword);
    FinanceHomePage financeHomePage = homePage.clickFinanceLink();
    // When
    ApplicantStudentPage applicantStudentPage = financeHomePage.goToGenrateScholarshipApplicationStudentPage();
    // Then
    assertThat(applicantStudentPage.selectAcademicYear("1")).isEqualTo(applicantStudentPage.getSelectedAcademicYear());
    // Given
2

You can override you private void instantiateChromeDriver() method in your child class (you have to change access modifier of the method in base class in order to do so) where you can instantiate the driver with the options you need in your child class.

Here is a simple model of your architecture:

Base class with its own initialization

public class Base {

    @BeforeMethod
    public void setUp(){
        initializeWebDriver();
    }

    protected void initializeWebDriver(){
        System.out.println("Base Initialization");
    }

    @Test
    public void testBase(){
        System.out.println("Test Base");
    }

}

Child class with overriden initialization

public class Child extends Base{

    @Test
    public void testChild(){
        System.out.println("Test Child");
    }

    @Override
    protected void initializeWebDriver() {
        System.out.println("Child Initialization");
    }
}

You can run it as is and then comment overriding in the child class and check how the output changes.

In your case I would rework your base class so that you have a separate method that is responsible for building ChromeOption and use that method in instantiateChromeDriver method. Thus you could override that new method only and either call a method from super-class and add one new option in your child class method or create new instance of ChromeOptionand build it from scratch.

|improve this answer|||||
  • Thanks @wec I'll give this a go in the next few days. – christopher harris Jan 22 at 13:14
0

Simple answer (but a lots of maintenance in the future):

  1. Create IncognitoBaseClass which extends BaseClass, override method instantiateChromeDriver() and make your TestClasses derive from one of them. As you want them.

Complex answers (one of them might be good):

  1. Create different packages for those tests, and use one for IncognitoTests and secon one for normal.
  2. Or use TestSettings to change the same test to run in different mode.
  3. Or write using reflections some custom tags, and mark specific tests to use different test settings
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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