I’ve had some conversations around Test Automation and realized that many companies are looking to have a better test adoption across the teams and have it standartized across their pipelines. The good news: Harness is here to help you in this journey and our delegate can handle your tests!
In the example below you will see how easy is to templatize and run Selenium tests straight from the Delegate.
Selenium is an open-source automated testing framework that use real browsers to validate Web Applications across multiple platforms.
The tests scripts can be written in many different languages like Python, Java and others. The fastest way to create a test script or at least speed up your development cycle is to use a Chrome Extension like Katalon or Selenium IDE that allows you to record a real Webpage navigation and export the scripts.
Selenium allows you to run the tests with the headless mode, this means that no UI is needed, being a perfect case to run it inside the delegate.
Instructions
Create and apply a profile to your delegate (I’m using a K8s delegate):
apt-get update
apt-get install -y unzip xvfb libxi6 libgconf-2-4 wget python-pip git
pip install -U selenium
pip install -U pytest
pip install -U unittest
wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/chromedriver
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb
apt-get -fy install
google-chrome --version && which google-chrome
Create your Selenium tests and store it in a repository like Github. This is a “Hello World” test for Chrome that exemplifies the use of headless mode:
from selenium import webdriver
# ChromeOptions
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox') # required if running as root user.
# Test
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://www.google.com')
print(driver.title)
Templatizing and executing the tests
If you want to reuse the workflow across different services and teams, you can create Workflow variables with the script name, configuration, URL, etc.
To execute your test script, just create a ShellScript step in your workflow, clone your repo and execute the test. In the example below my script is using an environment variable to get the URL to be tested:
export TESTURL=${workflow.variables.URL}
rm -rf harness-selenium
git clone https://github.com/luisredda/harness-selenium
python harness-selenium/${workflow.variables.TEST_SCRIPT}
Don’t forget to check “Execute on Delegate” and use the correct Delegate selector. Your configuration will look like this:
It’s simple as that. Now you have your Selenium tests standartized and handled by our Delegate!