Vikas Thange
Vikas Thange

Reputation: 250

Using Karate as Library to my Java @Tests

The best way using karate is with Karate DSL in feature files. However I just wants to check if I can utilize karate as a library to my java based framework.

For example I know I can use below code and automate Chrome using Chrome DevTools.

public class Test {
    public static void main(String[] args) {
        Chrome chrome = Chrome.startHeadless();
        chrome.setLocation("https://github.com/login");
        ......
        chrome.quit();
    } 
}

Can I do similar for com.intuit.karate.driver.WebDriver for automating any WebDriver based Browsers (like ChromeWebDriver, GeckoWebDriver etc.) without having feature files?

Can I use similar way to automate API tests with my own @Test methods but using karate library underneath in test method?

Thanks

Upvotes: 1

Views: 765

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58128

EDIT in 2021 - Karate 1.0 will have a Java API for all the core capabilities, see: https://twitter.com/KarateDSL/status/1353969718730788865


Not recommended because you would lose things like reports and the ability to do debugging.

Here is an example for browser automation.

    Chrome driver = Chrome.start();        
    driver.setUrl("https://github.com/login");
    driver.input("#login_field", "dummy");
    driver.input("#password", "world");
    driver.submit().click("input[name=commit]");

Upvotes: 0

Vikas Thange
Vikas Thange

Reputation: 250

After discussion with Peter Thomas (The Karate Creator) I figured out a way to do it. Here is sample code to launch Chrome webdriver Browser via karate.

HashMap<String,Object> map = new HashMap<String,Object>();
map.put("type","chromedriver");
map.put("executable","/Users/vxt82/Apps/chromedriver");
HashMap httpConfig = new HashMap<String,Object>();
httpConfig.put("readTimeout", 120000);
map.put("httpConfig",httpConfig);
ChromeWebDriver driver = new ChromeWebDriver(new DriverOptions(null, map, null, 9515, "chromedriver"));

And then you can call methods like

driver.setUrl("https://github.com/login");
driver.input("#login_field", "dummy");
driver.input("#password", "world");
driver.submit().click("input[name=commit]");

P.S. : As said by Thomas this is not recommended way of using Karate but posting answer just in case anyone is trying to use karate for test automation as a dependency and write test in java instead of using feature file.

Upvotes: 1

Related Questions