Skip to main content

Run Automation Tests on HyperExecute using JUnit


HyperExecute lets you run E2E (End-to-End) Selenium tests at the fastest possible speed. Unlike normal Selenium grid that involves multiple hops during the process of test execution, speed of execution on HyperExecute Grid is super fast as the code is executed on a secure cloud.

End-to-end encryption of the data (including the source code) ensures that the data is secure whether it is at rest or in transit. YAML-based workflow helps in realizing the benefits of optimal test execution and orchestration. Along with the unique features offered by HyperExecute, you also get access to a host of LambdaTest cloud features like detailed logs, Smart CI features, network insights, video recording, access to a range of browsers & platforms on the cloud, amongst others.

HyperExecute has several state of the art features to help you optmize your testing process. Go through the features page to take a look at all the tools that HyperExecute offers.

HyperExecute is compliant with leading security standards - SOC2, GDPR, and CCPA. Refer to HyperExecute Getting Started Guide for more information about features offered by HyperExecute.

All the code samples in this documentation can be found in the JUnit HyperExecute GitHub repository. You can either download or clone the repository to run tests on the HyperExecute Grid.

Gitpod


Follow the below steps to run Gitpod button:

  1. Click 'Open in Gitpod' button (You will be redirected to Login/Signup page).
Gitpod popup
  1. Login with Lambdatest credentials. You will be redirected to HyperExecute dashboard with pop-up confirming to 'Proceed' to Gitpod editor in the new tab and current tab will show hyperexecute dashboard.

Run in Gitpod

Getting Started with JUnit Tests on HyperExecute


The HyperExecute CLI is used for triggering tests on HyperExecute Grid. The CLI provides a host of other useful features that accelerate test execution. You should download the HyperExecute CLI binary on the host system for running tests on HyperExecute. Shown below is the HyperExecute CLI download location for different platforms:

PlatformHyperExecute CLI download location
Windowshttps://downloads.lambdatest.com/hyperexecute/windows/hyperexecute.exe
macOShttps://downloads.lambdatest.com/hyperexecute/darwin/hyperexecute
Linuxhttps://downloads.lambdatest.com/hyperexecute/linux/hyperexecute

For detailed information about HyperExecute CLI, please refer to HyperExecute CLI section in the HyperExecute getting started guide.

The fundamental difference between running Selenium tests on a cloud Selenium Grid and HyperExecute Grid is that you need not have any configurations on the local machine (i.e. the machine from where HyperExecute CLI is triggered). This is because the source code from the local machine will be zipped and securely uploaded to the cloud where the execution will be performed on the remote Virtual Machine (VM).

Prerequisites for running JUnit tests on HyperExecute Grid


Before using HyperExecute, you have to download HyperExecute CLI corresponding to the host OS. You also need to export the environment variables LT_USERNAME and LT_ACCESS_KEY that are available in the LambdaTest Profile page. Follow the below mentioned steps to set the environment variables LT_USERNAME & LT_ACCESS_KEY from the terminal.

For macOS:

export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY

For Linux:

export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY

For Windows:

set LT_USERNAME=LT_USERNAME
set LT_ACCESS_KEY=LT_ACCESS_KEY

Dependency caching is enabled in the YAML file to ensure that the package dependencies are not downloaded in subsequent runs. The first step is to set the Key used to cache directories. The directory m2_cache_dir is created in the project's root directory.

env:
CACHE_DIR: m2_cache_dir

# Dependency caching for Windows
cacheKey: '{{ checksum "pom.xml" }}'
cacheDirectories:
- $CACHE_DIR

Steps (or commands) that must run before the test execution are listed in the pre run step. In the example, the Maven packages are downloaded in the m2_cache_dir. To prevent test execution at the pre stage, the maven.test.skip parameter is set to true so that only packages are downloaded and no test execution is performed.

pre:
- mkdir m2_cache_dir
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install

To reduce the upload size, it is recommended to include files to be added in the ignore list in .hyperexecuteignore which is located at the root of the project. You can modify the content of .hyperexecuteignore as per your project requirements:

hyperexecute.exe
*.zip
*.log
artifacts
logs

Running JUnit Tests on HyperExecute Grid using Matrix Execution


Shown below is the HyperExecute YAML file for matrix execution:

---
version: 0.1
globalTimeout: 150
testSuiteTimeout: 150
testSuiteStep: 150

runson: win
retryOnFailure: true

maxRetries: 5
concurrency: 4

env:
# PAT: ${{ .secrets.testKey }}
CACHE_DIR: m2_cache_dir
TARGET_OS: Windows 11

# Dependency caching for Windows
cacheKey: '{{ checksum "pom.xml" }}'
cacheDirectories:
- $CACHE_DIR

matrix:
classname: ["ToDoTest", "SelPlayGroundTest1", "SelPlayGroundTest2" ]

# shell: bash

pre:
# Create the Cache directory
- mkdir m2_cache_dir
# Download and install packages in the CACHE_DIR.
# Skip execution of the tests in the pre step
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install

post:
- cat yaml/win/junit_hyperexecute_matrix_sample.yaml

mergeArtifacts: true

uploadArtefacts:
- name: Final Report
path:
- target/site/**
- name: Surefire Report
path:
- target/surefire-reports/**

testSuites:
- mvn `-Dplatname=win `-Dmaven.repo.local=m2_cache_dir `-Dtest=$classname test site surefire-report:report

Here are the major pointers that you should know for executing JUnit tests using matrix execution:

  • Global timeout, test suite timeout, and test suite step timeout : 150 minutes
version: 0.1
globalTimeout: 150
testSuiteTimeout: 150
testSuiteStep: 150
  • The target platform is set to win using the runson key
runson: win
  • The matrix constitutes of the following entries - project and project classes. This is because parallel execution will be achieved at the class level.
matrix:
classname: ["ToDoTest", "SelPlayGroundTest1", "SelPlayGroundTest2" ]
  • Test dependencies and required packages are installed as a part of the pre step. The maven.repo.local parameter in Maven is used for overriding the location where the dependent Maven packages are downloaded. In the current example, the packages are downloaded from the Maven repository in the m2_cache_dir
pre:
- mkdir m2_cache_dir
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install
  • Commands that have to run after test execution are listed in the post step. In the example, we cat the contents of yaml/win/junit_hyperexecute_matrix_sample.yaml
post:
- cat yaml/win/junit_hyperexecute_matrix_sample.yaml
  • The testSuites object contains a list of commands (that can be presented in an array). In the current YAML file, commands for executing the tests are put in an array (with a '-' preceding each item). The Maven command mvn test is used to run tests located in the current project.

In the current project, parallel execution is achieved at the testname level. The maven.repo.local parameter in Maven is used for overriding the location where the dependent Maven packages are downloaded.

testSuites:
- mvn `-Dplatname=win `-Dmaven.repo.local=m2_cache_dir `-Dtest=$classname test site surefire-report:report

Test Execution using Matrix Multiplexing

The CLI option --config is used for providing the custom HyperExecute YAML file (i.e. yaml/win/junit_hyperexcecute_matrix_sample.yaml). Run the following command on the terminal to trigger the tests on the HyperExecute Grid. The --download-artifacts option is used to inform HyperExecute to download the artifacts for the job.

Shown below is HyperExecute CLI command that has to be triggered from the root folder of the project (in case the execution platform is Windows):

./hyperexecute --platname=win --config yaml/win/junit_hyperexecute_matrix_sample.yaml --download-artifacts --force-clean-artifacts

Visit HyperExecute Automation Dashboard to check the status of execution:

automation-dashboard

Shown below is the execution screenshot when the YAML file is triggered from the terminal:

JUnit HyperExecute Terminal LogsJUnit HyperExecute Terminal Logs

Running JUnit Tests on HyperExecute Grid using Auto-split Execution


Shown below is the HyperExecute YAML file for auto-split execution:

---
version: 0.1
globalTimeout: 150
testSuiteTimeout: 150
testSuiteStep: 150

runson: win

autosplit: true
retryOnFailure: true

maxRetries: 5
concurrency: 4

env:
# PAT: ${{ .secrets.testKey }}
CACHE_DIR: m2_cache_dir
TARGET_OS: Windows 10

# Dependency caching for Windows
cacheKey: '{{ checksum "pom.xml" }}'
cacheDirectories:
- $CACHE_DIR

# shell: bash

pre:
- mkdir m2_cache_dir
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install

post:
- cat yaml/win/junit_hyperexecute_autosplit_sample.yaml

mergeArtifacts: true

uploadArtefacts:
- name: Final-Report
path:
- target/site/**
- name: Surefire-Report
path:
- target/surefire-reports/**

testDiscovery:
type: raw
mode: static
command: grep 'public class' src/test/java/hyperexecute/*.java | awk '{print$3}'

testRunnerCommand: mvn `-Dplatname=win `-Dmaven.repo.local=m2_cache_dir `-Dtest=$test test site surefire-report:report

Here are the major pointers that you should know for executing JUnit tests using auto-split execution:

  • The target platform is set to win using the runson key
runson: win
  • Auto-split is set to true in the YAML file
autosplit: true
  • retryOnFailure is set to true to instruct HyperExecute to retry the failed commands. The retry operation is carried out till the number of retries mentioned in maxRetries are exhausted or the command execution results in a Pass.
autosplit: true
retryOnFailure: true
maxRetries: 5
  • The concurrency (i.e. number of parallel sessions) is set to 5.
concurrency: 4
  • Steps (or commands) that must run before the test execution are listed in the pre run step. In the example, the Maven packages are downloaded in the m2_cache_dir. To prevent test execution at the pre stage, the maven.test.skip parameter is set to true so that only packages are downloaded and no test execution is performed.
pre:
- mkdir m2_cache_dir
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install
  • Commands to be run after test execution are listed in the post step. In the example, we cat the contents of yaml/junit_hyperexecute_autosplit_sample.yaml
post:
- cat yaml/win/junit_hyperexecute_autosplit_sample.yaml

The testDiscovery directive contains the command that provides the class names.

testDiscovery:
type: raw
mode: static
command: grep 'public class' src/test/java/hyperexecute/*.java | awk '{print$3}'

Running the above command on the terminal will give the test names fetched from pom.xml:

* SelPlayGroundTest1
* SelPlayGroundTest2
* ToDoTest

Command to trigger Autosplit execution

Shown below is HyperExecute CLI command that has to be triggered from the root folder of the project:

./hyperexecute --platname=win --config yaml/win/junit_hyperexecute_autosplit_sample.yaml --download-artifacts --force-clean-artifacts

Visit HyperExecute Automation Dashboard to check the status of execution:

automation-dashboardautomation-dashboard

Shown below is the execution screenshot when the YAML file is triggered from the terminal:

JUnit HyperExecute Terminal LogsJUnit HyperExecute Terminal Logs

Pre Steps, Post Steps, Dependency Caching, and Artifacts Management


Here are some of the advanced features (i.e. dependency caching, retries, post, and artifacts management) of HyperExecute that must be leveraged for accelerated package installation and verifying the results of the test execution. All the advanced features are applicable to Matrix and Auto-split modes.

Pre Steps and Dependency Caching

Dependency caching is enabled in the YAML file to ensure that the package dependencies are not downloaded in subsequent runs. The first step is to set the Key used to cache directories.

cacheKey: '{{ checksum "pom.xml" }}'
cacheDirectories:
- m2_cache_dir

The Maven packages are downloaded in the m2_cache_dir. To prevent test execution at the pre stage, the maven.test.skip parameter is set to true so that only packages are downloaded and no test execution is performed.

pre:
- mkdir m2_cache_dir
- mvn -Dmaven.repo.local=$CACHE_DIR -Dmaven.test.skip=true clean install

Post steps

Steps (or commands) that need to run after the test execution are listed in the post step. In the example, we cat the contents of yaml/win/junit_hyperexecute_matrix_sample.yaml

post:
  - cat yaml/win/junit_hyperexecute_matrix_sample.yaml

Retries

The retryOnFailure directive when set to true instructs HyperExecute to retry failed command(s). The retry operation is carried out till the number of retries mentioned in maxRetries are exhausted or the command execution results in a pass. Here is the combination of retryOnFailure and maxRetries that can be used for retrying command (or test) execution:

retryOnFailure: true
maxRetries: 5

Artifacts Management

The mergeArtifacts directive (which is by default false) is set to true for merging the artifacts and combing artifacts generated under each task.

The uploadArtefacts directive informs HyperExecute to upload artifacts [files, reports, etc.] generated after task completion. In the example, path consists of a regex for parsing the site and sure-fire reports (i.e. target/site/ and target/surefire-reports/) directory.

mergeArtifacts: true

uploadArtefacts:
- name: Final Report
path:
- target/site/**
- name: Surefire Report
path:
- target/surefire-reports/**

HyperExecute also facilitates the provision to download the artifacts on your local machine. To download the artifacts, click on Artifacts button corresponding to the associated TestID.

automation-dashboard

You can download the artifacts by clicking on the Download button as shown below:

automation-dashboard

Secrets Management

In case you want to use any secret keys in the YAML file, the same can be set by clicking on the Secrets button the dashboard.

secret management

Now create secrets that you can use in the HyperExecute YAML file.

secret management

Every test run on the HyperExecute Grid has a unique jobId associated with it. Each jobId can in turn constitute single (or multiple) groupId(s). You can visit HyperExecute automation dashboard for checking the status of the test execution.

HyperExecute lets you seamlessly navigate between jobId's and taskId's. The same can be done by navigating to Automation -> HyperExecute logs -> Corresponding jobId on the HyperExecute automation dashboard.

automation-dashboard

The snapshot below shows how to navigate to the respective testID for viewing the Selenium logs:

automation-dashboard

For any query or doubt, please feel free to contact us via 24×7 chat support or you can also drop a mail to support@lambdatest.com.
Happy testing!