karate

Two different browsers running at the same time of the Karate test


My current approach to testing is as outlined in this answer:

* configure driver = { type: 'chrome' }
* driver 'https://github.com/login'
* driver.quit()
* configure driver = { type: 'geckodriver' }
* driver 'https://google.com'

However, the pages to load are not as easy as in the previous example, so this simple switching between browsers takes awful lot of time.

What I am hoping to achieve

Have two driver instances running at the same time and switch between them using minimize() and maximize() functions

What have I tried

* configure driverChrome = { type: 'chrome' }
* configure driverFireFox = { type: 'geckodriver' }
* driverChrome 'https://example.com'
# do Chrome stuff
* driverChrome.minimize()
* driverFireFox 'https://stackoverflow.com/'
# do Firefox stuff

Result

http call failed after 767 milliseconds for url: https://example.com
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
src/test/java/testcase/web/demo/TC_multibrowser_POC.feature:17
unexpected 'configure' key: 'driverChrome'
classpath:myapp/common/driver/configureDriver.feature:12

The Question

Is it even possible to have two different browsers to be running at the same time? Or do I have to survive through long run of a testcase?


Solution

  • 💡 Short Answer:

    🔴 Karate does not officially support running two different browser instances at the same time in the same test scenario.

    BUT…

    🟡 You can achieve parallel multi-browser testing in Karate with some workarounds, using Java interop and separate driver instances.

    In your solution you've got unexpected 'configure' errors — because configure is not meant to dynamically switch like that during runtime.

    There's a potential solution for that!

    🔹 1. Create Java helper class

    import com.intuit.karate.driver.*;
    
    public class DualBrowserManager {
        public Driver chrome;
        public Driver firefox;
    
        public DualBrowserManager() {
            chrome = DriverOptions.start(null, "chrome");
            firefox = DriverOptions.start(null, "geckodriver");
        }
    
        public void visitChrome(String url) {
            chrome.gotoUrl(url);
        }
    
        public void visitFirefox(String url) {
            firefox.gotoUrl(url);
        }
    
        public void closeAll() {
            chrome.quit();
            firefox.quit();
        }
    }
    

    🔹 2. Call from Karate feature

    
    * def dual = Java.type('demo.DualBrowserManager')
    * def browsers = new dual()
    
    * eval browsers.visitChrome('https://example.com')
    * eval browsers.visitFirefox('https://google.com')
    
    # Optional: do other actions via Java methods
    
    * eval browsers.closeAll()
    

    OR

    ✅ Alternative: Use parallel scenarios

    Another idea is to structure your .feature files like this:

    Then run them in parallel:

    mvn test -Dkarate.options="--tags @parallel" -Dkarate.env=chrome
    mvn test -Dkarate.options="--tags @parallel" -Dkarate.env=firefox
    

    Use karate-config.js to switch drivers based on karate.env.