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;
    
    @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
    


  • 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.



Suggested Topics

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