Create Gatling Scripts Using VS Code

Translation of the article prepared in advance of the start of the course “Stress Testing”.


Foreword

Recently, thanks to a comment by one of the students studying my course Gatling fundamentals, I found out that you can create gatling scripts using Visual studio code. I honestly had no idea that this was possible – but I was pleasantly surprised to find how well it works!

In this post we will look at how to set up your Gatling script development environment in VS Code. We will look at the build tools. Maven and SBT.

Install Metals

The first thing to do, whether you plan to work with Maven or SBT, is to install the plugin Scala metals inside VS Code. This plugin will allow the Scala language server to work in VS Code and provide the typical features that you expect from a modern IDE.

Install the plugin from VS Code in the most standard way by going to the tab Extensions and doing a search Scala (Metals):

With Metals installed, let’s first see how to run Gatling in VS Code using Maven.

Gatling VScode with Maven

First you need install maven – You can download Maven from the official site and follow the installation instructions on its page.

Then install the Maven for Java plugin inside VS Code:

Still inside VS Code, open Command Pallette (View > Command Pallette) and select Maven: Update Maven Archetype Catalog:

As expected, this will update the catalog of available Maven archetypes.

Now we want to create a new Gatling project from the archetype of Gatling Maven. To do this, first open Command Pallette and select Maven: Create Maven Project. When choosing an archetype, click more. Enter Gatlingafter which the Gatling archetype should appear. Then watch the video below:

Save the project in a suitable place on your computer. Then open the project as usual in VS Code. You may have to import the assembly at this point. To do this, go to the Metals tab in VS Code and click Import Build:

This will force Maven to build your project.

Now, in the most ordinary way, add your Gatling simulation code. If you just follow my example and you need an example of a Gatling script, you can use this basic script below:

package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class BasicSimulation extends Simulation {

  val httpProtocol = http
    .baseUrl("https://computer-database.gatling.io") 
// Здесь находится корень для всех относительных URL
    .acceptHeader(
      "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

// Вот общие заголовки
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader(
      "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0"
    )

  val scn =
    scenario("Scenario Name") // A scenario is a chain of requests and pauses
      .exec(
        http("request_1")
          .get("/")
      )
      .pause(7) // Note that Gatling has recorder real time pauses

  setUp(scn.inject(atOnceUsers(1)).protocols(httpProtocol))
}

To run the script, open a terminal in VS Code, and enter mvn gatling:test. If you want to run a specific test case, you can run instead

mvn gatling:test -Dgatling.simulationClass=computerdatabase.BasicSimulation

I advise you to learn more about Gatling Maven plugin.

Gatling VScode with SBT

If you prefer to run and create your Gatling projects using the Scala Build Tool (SBT), I find it easiest to clone a project first Gatling SBT Plugin Demo.

Once you have cloned a project, open it as usual in VS Code. Go to the Metals tab in VS Code and click Import Build:

VS Code should now build your Gatling project using SBT.

To run all the tests in your project, open a terminal and type sbt gatling:test. Or, to run a specific test script, you can run the command sbt gatling:testOnly computerdatabase.BasicSimulation.

You can learn more about Gatling SBT plugin in his documentation.

Summary

In this article, we learned how to use Visual Studio Code to create our Gatling scripts. We looked at how to create and run a Gatling project with the Maven and SBT build tools.

Although IntelliJ IDEA remains my preferred development environment for developing Scala and Gatling code, it’s great to be able to use the more popular VS Code!


Learn more about the course “Stress Testing”


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *