2

I use selenium-jupiter. I am getting a webdriver from method arguments like this:

@Test
public void testWithChrome(ChromeDriver chromeDriver) {
          chromeDriver.get("someUrlHere");
}

Now I want to run tests on grid so I need to use webdriver based on environment. For example when developing tests on my PC I want to use (local) ChromeDriver, but when running tests on grid with Jenkins, I want to use RemoteDriver. So I need something like this: (That gives me local Chrome when env = 0 or gives me remote Chrome when env = 1 but it's not working)

int env = 0;

@Test
public void testWithChrome(
   (env == 0 ? ChromeDriver driver : RemoteDriver driver)) {
          driver.get("someUrlHere");
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
Coder3234
  • 23
  • 3
  • Have you checked out the official documentation for configuration options? https://bonigarcia.github.io/selenium-jupiter/ – Sam Brannen Jun 14 '18 at 17:50
  • No, but I am checking it out right now, and it looks like, I can tune WebDriverManager to achieve what I want. – Coder3234 Jun 15 '18 at 05:52

3 Answers3

3

In short: When configuring your Selenium extension programmatically you can force usage of a Selenium Grid by configuring its URL as follows (using JUnit 5 annotations):

abstract class UiTest {

    @RegisterExtension
    static SeleniumExtension seleniumExtension = new SeleniumExtension();     

    @BeforeAll
    static void setUpOnce() {

        boolean isRunningInCiEnvironment = ...

        if( isRunningInCiEnvironment ) {

           // this will force Selenium Jupiter to use a RemoteWebDriver
           seleniumExtension.getConfig().setSeleniumServerUrl("http://...");

        }

        // without above condition, a FirefoxDriver will be used locally
        seleniumExtension.addBrowsers(BrowserBuilder.firefox().build(););

   }       
}


class MyTest extends UiTest {

    // Use WebDriver interface in test method: concrete browser detected
    // at execution time (via @BeforeAll, inherited from parent class)
    @Test
    void my_test_Case(WebDriver webDriver) {

       webDriver.get(...)
       Assert.(...)        
    }
}

The problem in length is decribed here.

1

I think what would be better here is to have a method that is executed before any test (annotated with @BeforeAll) that determines what environment the script is being run in. It probably reads from some config file local vs grid. Once that is determined, assign the driver variable either an instance of ChromeDriver or RemoteDriver. From then on, your tests will pass around the driver instance which will be of type WebDriver because both ChromeDriver and RemoteDriver inherit from it.

WebDriver driver;

@BeforeAll
public void setup()
{
    // read from config file, etc. to determine if local or grid
    if (local)
    {
        driver = new ChromeDriver();
    }
    else
    {
        driver = new RemoteDriver();
    }
}

@Test
public void test()
{
    driver.get("someUrlHere");
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Hey, thank you for your answer. How to do that in selenium-jupiter? – Coder3234 Jun 14 '18 at 16:33
  • No idea. I hadn't heard of it before today. It seems odd to me to pass a specific driver around and to write tests for a specific browser. That kinda defeats the whole purpose of writing a test once and running it on X browsers. I would submit an issue on the github site and maybe someone there can help you. Maybe there are some tutorials or example code that you can reference. – JeffC Jun 14 '18 at 18:28
  • I found an example that is somewhat similar to what you are looking for. It's at the bottom of the [Quick reference section](https://bonigarcia.github.io/selenium-jupiter/#quick-reference) and starts with `You can also inject WebDriver objects declaring them as constructor parameters, instead of as test method parameters.` with code below. – JeffC Jun 14 '18 at 18:32
  • Injecting WebDriver in constructor seems to work the same way as injecting in method. – Coder3234 Jun 14 '18 at 19:00
  • It seems that I will use your solutions and I will not use selenium-jupiter for now. – Coder3234 Jun 14 '18 at 19:02
  • Thank you for your help. I upvoted your answer, but I don't have enough reputation so it doesn't show. Ok, I am going to accept your answer for now, because we don't have others, but I look for a selenium-jupiter specific solution. – Coder3234 Jun 15 '18 at 05:43
0

You can do that with WebDriverManager that comes with this extension.

    @BeforeEach
    public void setUp()
    {
        switch(browser)
        {
            case "chrome" -> 
                {
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
                }
            case "firefox" ->
                {
                WebDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver();
                }
            case "edge" ->
                {
                    WebDriverManager.edgedriver().setup();
                    driver = new EdgeDriver();          
                }
        }
            driver.manage().window().maximize();
   }
Vladimir
  • 630
  • 3
  • 12
  • 26