Skip to content

karatelabs/intellij-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 

Repository files navigation


IntelliJ Plugin

PLUS PRO ULTIMATE Enterprise
    (includes all in PLUS)     (includes all in PRO)

This plugin works fully-featured on IntelliJ Community Edition. JS support is built-in and does not require you to be running IntelliJ IDEA Ultimate.

By using this plugin, you agree to the Karate Labs EULA. Data on a few user actions is collected and subject to our Privacy Policy. HTML reports generated by the Karate open-source library has additional analytics.

License Activation

License activation and status is unified within the IntelliJ settings UI. Go to Settings -> Languages & Frameworks -> Karate and you should see something like this.

 

Click the Sign In link and you will be taken through the usual flow linked to your existing subscription details.

Once you have authenticated successfully, copy the session ID from the browser and paste it into the input-box now showing in the IntelliJ settings view. Click Apply to complete the sign-in.

Once signed-in you can work offline. You can always open this settings page to see how many days are left in your session.

If you need an offline license because of strict security or similar restrictions in your environment, please contact us. Please note that this option is available only for enterprise customers of Karate Labs.

Run From Editor

You can right-click a *.feature file in the explorer or within the editor and run it. An IntelliJ run-configuration will be created which you can customize later if needed.

You can also run a single Scenario by right-clicking on it or using the "gutter" icon. Right-clicking on the gutter-icon also brings up the option to debug instead of run.

 

You can even run a single "example" in a Scenario Outline by right-clicking on one of the data-rows within an Examples table.

Troubleshooting

If you don't see the options to run a test (described above) make sure that the Karate icon shows for open *.feature files in the editor and in the project-explorer. If the "Cucumber for Java" plugin is installed, it can conflict with Karate.

 

To make sure that the Karate plugin can run *.feature files, you can go to Settings -> Editor -> File Types -> Recognized File Types.

If you really need to mix Cucumber tests and Karate tests in the same workspace, refer to this post on Stack Overflow.

Run Configurations

IntelliJ run-configurations are useful for being able to re-run tests with specific parameters. All the typical Karate options you need are supported, including control over the JVM parameters and working directory.

Structure View

The IntelliJ structure view is supported so you can navigate large files with ease.

Code Formatting

Intellij code-formatting shortcuts work and even JSON within doc-string blocks will be formatted correctly.

Test Results

Test results integrate into the IntelliJ test-results view. The HTML report is one-click away.

 

When there are test errors - you can navigate directly to the feature file and line that failed by clicking on hyperlinks in the log.

Auto Complete

Besides the syntax validation, you have auto-complete for the most commonly used keywords.

Code Folding

You can collapse sections to make it easier to deal with long tests.

References

You can click to navigate to called files. Or hover to see context. Currently this is supported for the read() and Java.type() syntax. More coming soon, including auto-complete.

JSON Re-formatting

Place the cursor within JSON to see options to re-format it, you will see a "bulb" icon.

 

The following options are possible (depending on context):

  • Lenient - just like JS, where single-quotes are used and no quotes are needed for property keys
  • Strict - Strict JSON, using double-quotes
  • Make single Line - convert multi-line JSON to a single line
  • Make multi-line - convert a single line of JSON to multi-line, including triple-quotes

Run Folder

As a convenience, you can right-click and run a folder from the explorer view.

Debug

You can set break-points on Karate feature files in debug mode. The Karate debugger can even step-back and hot-reload simple edits to your test. Note that the options for "Hot Reload" and "Step Back" may be hidden away in some versions of the IntelliJ UI.

Debug Java from Karate

A Karate debug session will even stop at Java breakpoints.

Debug Karate from Java

You can also start a normal Java debug session that uses the Karate Runner Java API but still stop at Karate breakpoints. This requires you to be using Karate version 1.5.0 or greater. The two things to note are:

  • You have to use the Karate Runner Java API like in this example.
  • You have to use an IntelliJ Java or JUnit Run Configuration but in Debug mode. Right-clicking on a JUnit class (or method) or a Java class with a main method is normally how you create one.

Since the Runner API is a pure Java API - it does not get in the way of your flow or infrastructure. For example, your Java or JUnit code can start a server and then start your Karate test suite, and the debug session will stop at both Java and Karate breakpoints.

Debug Gradle Projects

If you have a Gradle project, the Run Configuration that IntelliJ creates may default to a Gradle specific one and not the IntelliJ native Java Run Configuration needed to honor breakpoints in Karate feature files. The default is easy to change by going to Settings > Build, Execution, Deployment > Build Tools > Gradle > Build and run. Set the Run tests using option to Choose per test as shown below.

Now when right-clicking on a JUnit test or Java class, you will have the option to choose the Java Run Configuration instead of the Gradle one.

Offline License

Important

The Offline License is a paid feature for enterprises. Please make sure you know who your designated admin is before you perform this step.

On the License Activation settings page, you should see a link called Offline License. When you click it you should see something like this.

A unique code for your system will be shown which you can cut and paste. In the example above, it is: DhHOFKHvd7XYTi+rQnNTJQ==

Send that unique code to your designated admin. You will be issued a license that is tied to this unique code. To apply the license, copy all the text from the license file (which should start with: -----BEGIN LICENSE FILE-----) and paste it into the text-area provided. Now you should see something like this.

Click [Apply] and you should a confirmation message and your license details.

OpenAPI Import

Right-clicking on any file in the Project Explorer brings up a menu called Karate Import as shown below. OpenAPI or Swagger files of any version are supported.

When successful, you will see a Karate tool-window typically on the top-right of your IntelliJ layout, shown below. You can exand to the level of HTTP methods per API path such as GET, POST etc.

When you click on an HTTP method, you can preview the equivalent Karate snippet.

Create Tests From Spec

Creating a Karate test from an Open API or Swagger file is much quicker now. Just click and drag from the tree into the editor window.

This way you have full control over the sequence of API calls you make within a Karate test.

Spec Payload Chooser

A challenge with complex APIs is that the schemas include all possible variations and permutations even though only some fields are commonly used. To solve this problem, you can right-click on an HTTP method in the tree. This gives you a menu to view the Request or Response when available.

This brings up a dialog where you can un-select the JSON keys you need. It is very easy to choose payload sub-sets using this UI.

Once satisfied you can cut and paste into a test or wherever you need the payload.

Explore API From Spec

Coming soon.

Create Mock From Spec

You can export all data within an imported schema into a plain-vanilla JS file that can be used as a Karate mock. A detailed explanation and sample can be found at this GitHub project: karate-oas-demo.

Use the Save Mock button at the bottom of the tool-window.

Spec Impact Analysis

You can also export a plain-vanilla JS file that dumps all API and schema information in a format designed to make impact analysis easier using your existing IDE diff features. The format is designed to flatten all metdata into single lines in a text file.

Once you have exported this file, you can compare two of these using the IntelliJ Compare With menu and view the diff in an intuitive manner.

You can scroll-right and view the differences even for very large API schemas.

Test Impact Analysis

Coming soon - the capability to show which tests and mocks are impacted due to changes in the API spec.