Karate Vs. Selenium

Reading Time: 3 minutes

Hi there,

This blog aims to throw some light on the comparison between Karate UI testing framework like Selenium. In this blog, we will try to explore both the frameworks and the shortcoming that we may or may not face while doing some hands-on work on these.

Why Karate?

Karate is a relatively new open source framework for testing Web services. Even though Karate is written in Java, its main advantage is that testers don’t need to do any Java programming in order to write fully automated tests. Instead, testers use a Gherkins like language with steps for making requests and validating responses.

When we look at testing the application we need to test the web services first . And then after integrating those web services with a UI we need to do web UI testing. This where Karate gets an Upper hand, it is basically a unified framework for API functional testing, performance and UI testing. This alone, is a major reason for one to at-least have a look at it.

Selenium

Almost everyone from the testing community knows what selenium is. It has been there for like more than 10 years or so. Selenium is an open-source tool that is used for automating the tests carried out on web browsers. Only testing of web applications is possible with Selenium. We can neither test any desktop (software) application nor test any mobile application using Selenium.

Moving on, without getting much involved about the background of these frameworks, let’s have a look over their advantages.

Karate over Selenium

There are three points that needs to be discussed where selenium have shortcomings, which are

  • Selenium “Waits” are hard to understand.
  • Executing JavaScript in the Browser is hard.
  • And HTML reporting/ report generation.

Waits

Selenium waits, needs a study book of their own, at-least that’s the case for a beginner. Waiting for DOM elements to become available for interaction or to disappear from the DOM tree to continue the test is hard to do for some web applications. Sometimes you just end up with hard waits: waiting for some milliseconds or seconds and then continue with the test. This is bad because these idle times add up over the test run and can significantly increase the runtime of a complete test suite.

On the other hand, Karate gives a large variations of waits, which can be easily understood by anyone. Like we have

ScriptDescription
waitFor(#id)This is typically used for the first element you need to interact with on a freshly loaded page
retry(10).waitFor(#id)like the above, but temporarily over-rides the settings to wait for a longer time.
waitForUrl(url)for convenience, this uses a string contains match – so for example you can omit the http/https prefix
waitForAny(‘#id’,’#tile’)handle if an element may or may not appear
waitUntil(expression)wait until any user defined JavaScript statement to evaluate to true in the browser
waitForText(‘#id’,’appeared’)frequently needed short-cut for waiting until a string appears – and this uses a “string contains” match for convenience
waitForEnabled(‘#id’,’4′)wait until a certain number of rows of tabular data is present

This is enough to make statement that Karate bests Selenium in the waits.

Reporting

One of the main issue with selenium is that it does not generates test report on its down. For this thing to happen, we will have to depend on TestNg, ReportNg and other plugins. In Karate we can have a more detailed report through JUnit and Cucumber-reporting. As compared to the reports generated through selenium, Karate bests selenium.

JavaScript Executor

In the Selenium world, you will find plenty of examples and references to the “JavaScriptExecutor” and the various equivalents in the different language. But all that is a little complicated. What if we can execute JavaScript on a reference to HTML DOM element, wouldn’t it be great and dead simple?

Karate lets you define and call JavaScript functions directly in your feature files. Just call the script() function and give your JavaScript snippet as an argument. For example

And match script('#eg01WaitId', "function(e){ return e.innerHTML }") == 'APPEARED!'
# which can be shortened to:
And match script('#eg01WaitId', '_.innerHTML') == 'APPEARED!' 

This article is neither a full comparison of Selenium and Karate, nor is it a complete reference to learn Karate. This was written with an to arouse interest in Karate for Selenium Users. An individual have to decide which framework will be according to their usage.

Thanks for reading. I hope you would have found this informative.

Reference

https://intuit.github.io/karate/karate-core/

https://hackernoon.com/the-world-needs-an-alternative-to-selenium-so-we-built-one-zrk3j3nyr

Written by 

Sparsh is a QA Consultant having experience of more than 2 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.