0

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!

Tree55Topz
  • 1,102
  • 4
  • 20
  • 51
  • You must be calling `new ChromeDriver()` somewhere else in your code. Where? – SiKing Feb 29 '16 at 18:57
  • @SiKing Correct, we use a seperate class, TestExtension that handles all the preliminary set ups, browserstack params, etc. This is where it gets instantiated. However I would prefer not to alter that.. – Tree55Topz Feb 29 '16 at 19:04
  • Every time you call `new ...Driver()`, a new browser will open. That is how Selenium works! – SiKing Feb 29 '16 at 19:16
  • @SiKing I am aware of that and that is why I am trying to find a way to just pass these options in.. like if there is a way to do driver.addOptions() or something rather than create a new driver with the options passed in. – Tree55Topz Feb 29 '16 at 19:20
  • These options are passed to the **browser** at startup. Selenium has nothing to do with these. – SiKing Feb 29 '16 at 19:29
  • OP, if you don't want to alter your TestExtension class because this is a non-standard test, then fundamentally you will have to create a new dedicated test that opens a second browser. Else, make TestExtension smart enough to deal with this situation. You can't have it both ways. – Andrew Regan Feb 29 '16 at 22:44

1 Answers1

1

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.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • This is what I was afraid of but I was aware that this would be more than likely be the only way... Oh well, it was worth an ask. Thanks :) – Tree55Topz Mar 02 '16 at 16:41