How to add an argument to my BaseTest ChromeDriver Instance?

  • 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;
    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");
        } else if (browser.equals("chrome")) {
            System.out.println("Reached Chrome");
        } else if (browser.equals("safari")) {
            System.out.println("Reached Safari");
    private void instantiateChromeDriver() {
        ChromeOptions chromeOptions = new ChromeOptions();
        this.driver = new ChromeDriver(chromeOptions);
    //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();
    public void shouldGenerateNewScholarshipForCurrentStudent() throws Exception {
        // Given
        HomePage homePage = commonNavigation.shouldAccessHomepageForSLSTestEnvironment(driver, financeUserUsername, financeUserPassword);
        FinanceHomePage financeHomePage = homePage.clickFinanceLink();
        // When
        ApplicantStudentPage applicantStudentPage = financeHomePage.goToGenrateScholarshipApplicationStudentPage();
        // Then
        // Given

  • 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 {
        public void setUp(){
        protected void initializeWebDriver(){
            System.out.println("Base Initialization");
        public void testBase(){
            System.out.println("Test Base");

    Child class with overriden initialization

    public class Child extends Base{
        public void testChild(){
            System.out.println("Test Child");
        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.

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2