Connect & Get help from fellow developers on our Discord community.Ask the Community
Bulk export tests as code
Learn how to download and convert tests into code in multiple languages using a shell script in Low Code Automation.
Use this guide to export your Low Code Automation tests to code in different programming languages using a shell script.
Prerequisites
BrowserStack Username and Access Key from your My Profile page.
Node.js and npm must be installed.
The jq command-line JSON processor must be installed on macOS or Linux. On Windows, use Windows Subsystem for Linux (WSL) or Git Bash for compatibility.
Supported languages
Python (Pytest)
Java (JUnit)
JavaScript (Mocha)
C# (NUnit)
C# (XUnit)
Ruby (RSpec)
Shell script
Save the following shell script as side-to-code-converter.sh:
```bash
#!/bin/bash
set -e
# Validate inputs
if [ -z "$ACCESS_TOKEN" ] || [ -z "$LANGUAGE" ]; then
echo "ERROR: ACCESS_TOKEN and LANGUAGE must be set as environment variables."
exit 1
fi
# Setup environment
npm init -y --silent
npm install @seleniumhq/side-code-export --silent
# Install language-specific exporter
case "$LANGUAGE" in
Python_Pytest) npm install @seleniumhq/code-export-python-pytest --silent ;;
Java_JUnit) npm install @seleniumhq/code-export-java-junit --silent ;;
Javascript_Mocha) npm install @seleniumhq/code-export-javascript-mocha --silent ;;
Csharp_NUnit) npm install @seleniumhq/code-export-csharp-nunit --silent ;;
Csharp_XUnit) npm install @seleniumhq/code-export-csharp-xunit --silent ;;
Ruby_RSpec) npm install @seleniumhq/code-export-ruby-rspec --silent ;;
*)
echo "Invalid LANGUAGE Input"
exit 1
;;
esac
echo "Fetching the Test List..."
response=$(curl -s -u "$ACCESS_TOKEN" -X GET "https://lcnc-api.browserstack.com/api/v1/test-list")
echo "Success: Fetched the Test List"
INPUT_DIR="downloaded_side_files"
OUTPUT_DIR="converted_code_files"
mkdir -p "$INPUT_DIR" "$OUTPUT_DIR"
echo "Downloading the Test .side files..."
echo "$response" | jq -r '.[] | "\(.test_id)|\(.name)"' | while IFS='|' read -r test_id test_name; do
file_path="$INPUT_DIR/${test_name}_${test_id:0:5}.side"
curl -sS -u "$ACCESS_TOKEN" -X GET "https://lcnc-api.browserstack.com/api/v1/tests/${test_id}/export" -o "$file_path" -L
echo "Downloaded test: $test_name"
name="${test_name}_${test_id:0:5}"
case "$LANGUAGE" in
Python_Pytest)
echo "Converting to Python Pytest..."
npx side-code-export node_modules/@seleniumhq/code-export-python-pytest/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/test_defaultSuite.py" "$OUTPUT_DIR/$name.py"
;;
Java_JUnit)
echo "Converting to Java JUnit..."
npx side-code-export node_modules/@seleniumhq/code-export-java-junit/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/DefaultSuiteTest.java" "$OUTPUT_DIR/$name.java"
;;
Javascript_Mocha)
echo "Converting to JavaScript Mocha..."
npx side-code-export node_modules/@seleniumhq/code-export-javascript-mocha/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/defaultSuite.spec.js" "$OUTPUT_DIR/$name.spec.js"
;;
Csharp_NUnit)
echo "Converting to C# NUnit..."
npx side-code-export node_modules/@seleniumhq/code-export-csharp-nunit/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/DefaultSuiteTest.cs" "$OUTPUT_DIR/$name.cs"
;;
Csharp_XUnit)
echo "Converting to C# XUnit..."
npx side-code-export node_modules/@seleniumhq/code-export-csharp-xunit/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/DefaultSuiteTest.cs" "$OUTPUT_DIR/$name.cs"
;;
Ruby_RSpec)
echo "Converting to Ruby RSpec..."
npx side-code-export node_modules/@seleniumhq/code-export-ruby-rspec/dist/index.js "$file_path" "$OUTPUT_DIR"
mv "$OUTPUT_DIR/defaultSuite_spec.rb" "$OUTPUT_DIR/${name}_spec.rb"
;;
esac
done
```