Selenium

Setting Up Selenium with aqua for Automated Testing

This guide will help you set up Selenium with aqua to run automated tests in your development environment or BrowserStack. Follow the steps carefully to ensure all components are correctly configured.

Prerequisites

Ensure you have the following ready before starting the setup:

  • A running aqua PowerShell agent: here the article

  • Java JDK installed on the server where the agent is running

  • Selenium, JUnit, Hamcrest, and Apache Commons IO libraries downloaded: you can find the zip. File here:

  • Aqua test case created with necessary test data for browser and OS configurations

Steps to Set Up and Execute Tests

1. Setting Up the Environment

  1. Install Java JDK:

    • Download and install the latest version of Java JDK on the server running the PowerShell agent.

  2. Download Selenium:

    • Download the Selenium Java client library from Selenium's official website.

    • Extract the contents into a folder named Selenium inside the PowerShell agent directory.

  3. Download JUnit and Hamcrest Libraries:

  4. Download Apache Commons IO:

    • Download the org.apache.commons.io.jar and place it in the agent directory. Alternatively, ensure it is declared in the classpath.

  5. Run Selenium Server:

    • Download the Selenium standalone server from Selenium's official website.

    • Start the server with the command:

      java -jar selenium-server-4.23.0.jar standalone

2. Configuring aqua

  1. Login to aqua:

    • Access your aqua account and log in.

  2. Create a Test Case:

    • Navigate to the test cases section and create a new test case.

  3. Add a Test Step:

    • Inside the test case, create a new test step.

  4. Add PowerShell Automation:

    • Configure the test step to use PowerShell for automation.

  5. Upload PowerShell Script:

    • Copy the PowerShell script contained in execute.ps1 to the aqua PowerShell automation step.

  6. Upload Java Files:

    • Upload RemoteTest.java and Report.java as attachments to the PowerShell step. These files contain the test logic and logging utility.

3. Setting Up Test Data

  1. Add Test Data to the Test Case:

    • Include the following test data parameters to control the OS and browser configurations:

      • BrowserName

      • BrowserVersion

      • OS

      • OS_Version

    • These parameters are used in RemoteTest.java to specify which environment the test should run on.

4. Executing the Test

  1. Compile the Java Files:

    • Ensure the PowerShell script (execute.ps1) compiles the Java files using the javac command with the appropriate classpath that includes all required libraries and classes.

  2. Run the Test:

    • Execute the test through the aqua interface. The PowerShell script will run, compiling and executing the Java test files.

Example PowerShell Script (execute.ps1)

# Get Parameters from aqua
param($variables, $tempDir, $aquaCallback)  
 
$OutputEncoding = [Console]::OutputEncoding
 
####################################
# Construct classpath for Selenium #
####################################
 
# Set Java-Classpath and send to aqua-Log
# $classpath = "C:/Program Files (x86)/Selenium/libs/libs/*;C:/Program Files (x86)/Selenium/libs/selenium-java-3.141.59.jar;"+$tempDir+";"
$classpath = $tempDir+"\..\..\hamcrest_1_3\*;"+$tempDir+"\..\..\junit_4_13\*;"+$tempDir+"\..\..\Selenium\*;"+$tempDir+"\..\..\Selenium\selenium-java-4.23.0.jar;"+$tempDir+";"
$javaFiles=Get-ChildItem -File -Filter "*.java"
 
$aquaCallback.SendMessage($tempDir, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
$aquaCallback.SendMessage("Classpath: "+$classpath, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell");
 
#################################################
# Inject Parameters from aqua-Variables-Manager #
#################################################
 
$fileStr = $tempDir+"\RemoteTest.java"
#$aquaCallback.SendMessage("File: "+$fileStr, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
$tempFileStr = $tempDir+"\Temp.java"
#$aquaCallback.SendMessage("Temp-File: "+$tempFileStr, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell");
 
foreach ($var in $variables) {
	$aquaCallback.SendMessage("Set Parameter "+$var.Name+ " to "+$var.Value, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
	$replaceString = "%"+$var.Name+"%"
 
	Get-Content $fileStr | Foreach-Object {$_.Replace($replaceString, $var.Value)} | Set-Content $tempFileStr
	If (Test-Path $fileStr){
		Remove-Item $fileStr
		move-item -path $tempFileStr -destination $fileStr
	}
}
 
# replace variable %browserstack% so that settings of browserstack are used
$aquaCallback.SendMessage("Replace %browserstack% for online execution on Browserstack", [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
Get-Content $fileStr | Foreach-Object {$_.Replace("%browserstack%", "true")} | Set-Content $tempFileStr
If (Test-Path $fileStr){
    Remove-Item $fileStr
    move-item -path $tempFileStr -destination $fileStr
}
 
################
# Compile Test #
################
 
# javac -encoding UTF-8 -cp "$classpath" -d . $tempDir/*.java 2>&1 > execution.log
$aquaCallback.SendMessage($PWD, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell");
javac -cp "$classpath" -d . $javaFiles 2>&1 >> execution.log
 
################
# Execute Test #
################
 
$aquaCallback.SendMessage("Going to execute selenium tests", [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell");
 
# Run JUnit with Selenium-Script and log to test.log
java -cp "$classpath;." org.junit.runner.JUnitCore RemoteTest 2>&1 >> execution.log
 
########################
# Send results to aqua #
########################
 
# Read Execution-Result
$result = $LastExitCode
 
$aquaCallback.AddExecutionAttachment($tempDir+"/execution.log");  
$aquaCallback.SendMessage("Execution Finished with result: "+$result, [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell");
 
# Send Log to Attachments which is created by the java code
# If (Test-Path $tempDir+"/test.log"){
    $aquaCallback.AddExecutionAttachment($tempDir+"/test.log");
# }  
 
# Parse Log
foreach ($line in [System.IO.File]::ReadLines($tempDir+"/test.log")) {
	if($started) {
		if($line -cmatch "STOP") {
			$started = $true;
			break
		}
		$split = $line.Split(";");
		$date = $split[0]
		$level = $split[1]
		$message = $split[2]
		$screenshot = $split[3]
		if($level -match "INFO") {
			$levelstring = [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalInfo
		} elseif ($level -match "DEBUG") {
			$levelstring = [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug
		} elseif ($level -match "ERROR") {
			$levelstring = [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::ExecutionError
		} elseif ($level -match "WARN") {
			$levelstring = [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalWarn
		} else {
			$levelstring = [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::ExecutionError
		} 			
		if($screenshot) {
			$aquaCallback.SendMessageWithScreenshot($message, $tempDir+"/"+$screenshot, $levelstring, "Selenium");   
		} else {
			$aquaCallback.SendMessage($message, $levelstring, "Selenium");
		}
	}
	# Loop for starting Line
	if(!$started) {
		if($line -cmatch "START") {
			$started = $true;
		}
	}
}
 
# Cleanup - Does not work
Remove-Item .\screenshot* -include .png -force
Remove-Item .\* -include .class -force
Remove-Item .\test.log -force
 
# Return Result
if ($result -gt 0)
{
    $aquaCallback.SendMessage("Failures detected. See attached log for details.", [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
    return "Fail";
}
else
{
    $aquaCallback.SendMessage("All tests passed.", [aqua.ProcessEngine.WebServiceProxy.ExecutionLogMessageType]::InformationalDebug, "PowerShell"); 
    return "Ready"
}

Notes

  • Ensure the paths in the PowerShell script are correctly set to the locations of the Selenium, JUnit, Hamcrest, and Apache Commons IO libraries.

  • The RemoteTest.java file should contain logic to interpret the test data parameters and configure the test accordingly.

  • The Report.java file should handle logging and ensure logs are properly parsed and displayed in the aqua Execution Log.

By following these steps, you will successfully set up and execute Selenium tests within the aqua environment.

Last updated