Testing from the Command Line

To ensure that you are able to start up and run Selenium Webdriver scripts from the command line, you must create and use a command that will execute your Selenium script. The command that you use varies depending on the programming language and testing framework that you are using to conduct Selenium tests.

For example, to run NUnit in .NET, you can run a command similar to the following:

D:\tmp\selenium_wd\bin\net35\nunit3-console.exe "D:\tmp\selenium_wd\selenium_c_sharp-master\Selenium\bin\Debug\Selenium.dll"

In this example, the nunit3-console.exe is the unit test runner, and Selenium.dll is the DLL that contains the unit tests. For more examples, see Creating a Selenium Command.

Tip: You can use the POST /configuration/selenium/folder and GET /configuration/selenium/file/{foldername} API endpoints to show the full path to the files you deployed. You can use this information to update the command in the CLI. For more information, see Uploading Files to Fortify WebInspect.

Creating a Selenium Command

The Selenium command is used on the command line to execute unit tests. In most cases, the command can be found during a run of unit tests on the build server or while debugging. This command varies based on the unit test framework that you are using. Each framework has its own runner and command-line arguments. The following sections provide tips and sample commands for several frameworks in various languages.

.NET MSTest

The MSTest framework uses a tool called Vstest.console.exe with the following syntax:

<Path_to_Vstest_Executable>\Vstest.console.exe <Path_to_Unit_Test_dlls>\<TestFileNames> <Options>

In most cases, you must call this executable with a list of DLLs, which are the test file names that you want to run. The following sample code runs two test files:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\
CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
"C:\Projects\Tests\bin\TestHomepage_unittest.dll"
"C:\Projects\Tests\bin\AddCart_unittest.dll"

.NET NUnit

The NUnit framework uses a tool called nunit3-console.exe (version 3.x) with the following syntax:

NUNIT3-CONSOLE <InputFiles> <Options>

You must call this executable with a list of DLLs, which are the test file names that you want to run. The following sample code runs two test files:

C:\nunit\net35\nunit3-console.exe
"C:\Projects\Tests\bin\TestHomepage_unittest.dll"
"C:\Projects\Tests\bin\AddCart_unittest.dll"

xUnit.net

The xUnit.net framework provides two command-line runners: xunit.console.exe and xunit.console.x86.exe. You use the following syntax:

xunit.console <assemblyFile> [configFile] [assemblyFile [configFile]...] [options] [reporter] [resultFormat filename [...]]

xUnit.net accepts .json and .xml file extensions as configuration files (configFile).

You must call the appropriate executable with a list of DLLs, which are the test file names that you want to run. The following sample code runs two test files:

C:\xunit\xunit.console.exe
"C:\Projects\Tests\bin\TestHomepage_unittest.dll"
"C:\Projects\Tests\bin\AddCart_unittest.dll"

Java TestNG

The TestNG framework requires testng.jar libraries with a classpath (-cp) option and the java.exe application. In the -cp option, you must list all the library classes that you need to run your project. You use the following syntax:

java -cp "<Path_to_testngjar>/testng.jar:<Path_to_Test_Classes>" org.testng.TestNG <Path_to_Test_xml>

The following sample code runs an XML test file:

C:\Program Files\Java\jdk-12.0.1\bin\java.exe -cp 
".\libs\: C:\Program Files\jbdevstudio4\studio\plugins\*" 
org.testng.TestNG testng.xml

Java JUnit

The JUnit framework has several versions and each version has its own command to execute tests. In the -cp option, you must list all the library classes that you need to run your project.

JUnit version 5.x uses the following syntax:

java -jar junit-platform-console-standalone-<version>.jar --class-path <Path_to_Compiled_Test_Classes> --scan-class-path

JUnit version 4.x uses the following syntax:

java -cp .\libs\:<Path_to_Junitjar>\junit.jar org.junit.runner.JUnitCore [test class name]

JUnit version 3.x uses the following syntax:

java -cp .\libs\:<Path_to_Junitjar>\junit.jar junit.textui.TestRunner [test class name]

The following sample code runs a test class:

C:\Program Files\Java\jdk-12.0.1\bin\java -cp 
C\java\libs\:C:\junit\junit.jar org.junit.runner.JUnitCore 
C:\project\test.class

Python unittest and PyUnit

Python provides built-in unit test modules (-m): Python unittest and PyUnit, depending on the version of Python you are using. These frameworks use the following syntax:

python -m unittest [options] [tests]

In this syntax, the [tests] can be a list of any number of test modules, classes, and test methods. The following command displays the unittest help in Python:

python -m unittest -h

The following sample code runs a test file named tests.py in the unittest module:

C:\Python\Python37-32\python.exe -m unittest 
C:\SampleProjects\POMProjectDemo\Tests\tests.py

Ruby RSpec

The RSpec framework provides unit testing libraries for Ruby code. This framework uses the following syntax:

<Path_to_RSpec>\rspec.bat [options] [files or directories]

The following sample code runs a test library:

C:\Ruby26-x64\bin\rspec.bat  -I C:\Ruby26-x64\Project\lib\  
C:\Ruby26-x64\Project\spec\calculator_spec.rb

JavaScript Jest

Jest a JavaScript library for creating and running tests on JavaScript code. This framework uses the following syntax:

<Path_to_Jest>\jest.js [--config=<pathToConfigFile>] [TestPathPattern]

The following sample code runs a test library:

C:\Users\admin\AppData\Roaming\npm\jest.cmd" 
--config=C:\Users\admin\AppData\Roaming\npm\jest.config.js 
C:/Users/admin/AppData/Roaming/npm/sum.test.js