Tree55Topz
Tree55Topz

Reputation: 1142

passing ChromeOptions without instantiating new WebDriver

I am trying to pass in ChromeOptions to my driver to allow for popups. I am using TestNG with the @BeforeClass, @Test, and @AfterClass annotations.. I am trying to enable pop ups and I have been succesful in doing so using the following method.

@BeforeClass
public void setUp(){
    if (driver instanceof ChromeDriver){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-popup-blocking");
        driver = new ChromeDriver(options);
    }
   }

While this does work, it opens up the webdriver, then opens up another with the options. I do not want two webdrivers to pop up.. I just want to pass these options to the first webdriver! I am running these using an xml and a TestExtension class where the drivers get instantiated and do not want to alter that class. Is there a way to change the driver = new ChromeDriver(options) to something that will just pass these options in? Thanks!

Upvotes: 0

Views: 1617

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

You cannot do this without altering your TestExtension class. The reason being whatever arguments you are passing gets passed to the browser being spawned at the time of instantiation. After that there is no way of altering anything to change the behavior of the spawned browser. You would need to alter your TestExtension class and then provide in a mechanism wherein a user can basically inject their own capabilities as well, which would be considered by TestExtension prior to spawning the browser. It could be as trivial as passing in the fully qualified package name of the class which when invoked can instantiate the capability object that you pass via a JVM argument.

Your TestExtension class would basically inspect the JVM argument for any custom capabilities being passed and if found, it would merge those capabilities as well into its capabilities and then spawn the browser. That is the only way of doing this.

Upvotes: 1

Related Questions