How to use different webdrivers based on environment

前端 未结 3 1359
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 01:50

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

@Test
public void testWithChrome(ChromeDriver chromeDriver) {
          chromeD         


        
3条回答
  •  旧巷少年郎
    2021-01-27 02:54

    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");
    }
    

提交回复
热议问题