Set up Running Automated UI tests on your First day at work: Part 2

Run Selenium tests from command and Visual Studio Code

Courtney Zhan
4 min readFeb 27, 2022

In the previous article, I showed you how to run automated Selenium UI tests on your first day of work with TestWise. The test scripts are in raw Selenium WebDriver (with Ruby binding); this means we can use any text editors (such as Notepad or Visual Studio Code) and run tests from the command line. This article shows how you do that.
(All these is still a part of setting up and running Selenium tests on your first day at work)

Why do we want to run tests from the command line?

Ideally, we don’t want to couple our work too tightly to a specific tool. Being able to execute tests from the command line can help remove that dependency. This is also necessary for Continuous Testing setup further down the track.

In the previous article, we installed TestWise Ruby Edition, which does most of the heavy lifting for us. This is equivalent to installing:

TestWise Standard Edition + Ruby + Testing Libraries + ChromeDriver

In this article, I will show how to write and run tests without TestWise (I highly recommend TestWise, which I use every working day. The purpose of this article is to show total freedom with the test scripts, 100% raw Selenium WebDriver, created by TestWise.). Instead, we’ll manually install Ruby, ChromeDriver & Testing Libraries independently.

Installation

We will need to install Ruby, ChromeDriver & Testing Libraries to get started.

  • Ruby
    RubyInstaller for Windows with Devkit, such as ‘Ruby+Devkit 2.6.8–1 (x64)’, and run the installer. For macOS or Linux, use the usual package installer.
  • ChromeDriver, required for driving Chrome.
    Download the version that matches your browser.
  • Testing Libraries (called gems in Ruby)
    Install selenium-webdriver , rspec , …, etc with the command like below
    gem install selenium-webdriver

See this article below for detailed instructions on how to do this and execute RSpec tests on the command line.

Run RSpec Tests from the Command Line

Now run a sample RSpec test (tests can be cloned here).

> cd C:\demo\agiletravel-ui-tests\selenium-webdriver-rspec
> rspec spec\login_spec.rb

You will see the test execute in a Chrome browser. The output in the command window will look similar to the following:

A Note on Text Editors

In Part 1, a set of sample tests are cloned and executed to demonstrate functionality. You will (most likely) be writing your tests in a programming editor of your choice before running them. Some programming editors can provide extra functions that can make your life easier. A popular choice is Visual Studio Code (download link).

Visual Studio Code (VS Code)

VS Code does have extensions that can do one-click execution for RSpec tests in the editor. This is convenient.

My favourite extension for RSpec tests is Ruby Test Runner by Mateusz Drewniak (Marketplace link here).

VS Code’s Ruby Test Runner Extension.

In VS Code, you can open the Marketplace with Ctrl + Shift + X on Windows, or Cmd + Shift + X on Mac. There, you can search for Ruby Test Runner and install it.

Once installed, you will notice that there is now a “Run Test” option before every test suite and test case. Below I have highlighted the new option (this is the sample test case used in the previous article).

After installing the VS Code “Ruby Test Runner” extension, there is a “Run test” button

Click the “Run test” button (or use the key-binding Ctrl + Shift + T) and you will see the VS Code Terminal open and execute the RSpec test.

Screenshot of the terminal after the test is triggered through Ruby Test Runner.

This is equivalent to manually entering spec <name of test file> in your terminal.

Overall, independently installing the necessary programs and executing tests on the command line is an essential part of setting up an automated test execution environment.

VS Code’s extensions make it better than some other plain text editors for writing and executing RSpec tests. However, I still prefer using TestWise, a dedicated Functional testing IDE. TestWise has some features like execution hooks that are very useful when running and debugging tests. But, if you cannot use TestWise, then VS Code is a good alternative.

--

--

Responses (1)