Configure and Run Selenium with Ruby on Github actions
Selenium WebDriver is a powerful tool for automating web browsers. It supports various programming languages, and in this guide, we’ll focus on setting it up with Ruby. We’ll also demonstrate how to create an HTML report using the provided code.
Prerequisites
Before you start, ensure you have the following installed:
- Ruby
- RubyGems (usually comes with Ruby)
- Chrome browser
- ChromeDriver
Installing Ruby
You can download it from the official Ruby website if you don't have Ruby installed.
Here is the website to download Ruby based on your needs.
https://www.ruby-lang.org/en/downloads/
Installing ChromeDriver
ChromeDriver is required to control the Chrome browser. You can download it from the ChromeDriver download page. Make sure to download the version that matches your Chrome browser version.
After downloading, place the Chromedriver executable in a directory included in your system’s PATH.
Setting Up the Selenium Gem
Open your terminal and install the Selenium WebDriver gem by running:
gem install selenium-webdriver
Writing the Test Script
Create a new Ruby file, test.rb, and copy the following code into it:
require 'selenium-web driver'
require 'time'
# Set up Chrome options
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument(' - headless')
options.add_argument(' - no-sandbox')
options.add_argument(' - disable-dev-shm-usage')
driver = Selenium::WebDriver.for :chrome, options: options
# File to store the HTML report
report_file = 'test_report.html'
File.open(report_file, 'w') do |file|
file.puts "<html><head><title>Test Report</title></head><body>"
file.puts "<h1>Test Report</h1>"
file.puts "<p>Test started at #{Time.now}</p>"
begin
# Navigate to a webpage
driver.navigate.to 'http://www.google.com'
file.puts "<p>Navigated to: <a href='#{driver.current_url}'>#{driver.current_url}</a></p>"
# Find the search box and enter a query
search_box = driver.find_element(name: 'q')
search_box.send_keys 'Selenium WebDriver'
search_box.submit
file.puts "<p>Search submitted at #{Time.now}</p>"
# Wait for results to load and display
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { driver.title.include? 'Selenium WebDriver' }
# Log the page title
file.puts "<p>Page title is #{driver.title}</p>"
file.puts "<p>Test completed successfully at #{Time.now}</p>"
rescue StandardError => e
# Log any errors
file.puts "<p>Error occurred: #{e.message}</p>"
ensure
# Close the browser
driver.quit
file.puts "</body></html>"
end
end
Code Explanation
- Setup Chrome Options: Configures Chrome to run in headless mode, which is useful for running tests on servers without a graphical user interface.
- Initialize WebDriver: Creates a new instance of the Chrome WebDriver with the specified options.
- HTML Report Setup: Opens a file to write the HTML report and starts the HTML structure.
- Navigate to Google: Uses the WebDriver to navigate to Google.
- Search for Selenium WebDriver: Finds the search box, enters a query, and submits the form.
- Wait for Results: Waits until the page title includes “Selenium WebDriver”.
- Log Information: Logs the page title and timestamps in the HTML report.
- Error Handling: Catches and logs any errors that occur during the test.
- Close the Browser: Ensures the browser is closed and finishes the HTML structure.
Running the Script
To run the script, navigate to the directory containing test.rb in your terminal and execute :
ruby test.rb
This will run the test, navigate to Google, perform a search, and generate an HTML report named test_report.html
in the same directory.
Running Selenium Tests on GitHub Actions
Prerequisites
Ensure you have the following in place before proceeding:
- A GitHub repository containing your Selenium test script (test_script.rb).
- A basic understanding of GitHub Actions and YAML syntax.
Creating the Workflow File
GitHub Actions workflows are defined in YAML files located in the .github/workflows directory of your repository. Let’s create a new workflow file named test.yml.
Step-by-Step Configuration
- Create the Workflow File
In your repository, create a directory structure .github/workflows/ if it doesn’t exist. Then, create a file named test.yml inside this directory.
- Define the Workflow
Open test.yml and add the following configuration:
name: Test
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
- name: Install dependencies
run: |
gem install selenium-webdriver headless
- name: Run tests
run: |
ruby test.rb
- uses: actions/upload-artifact@v3
with:
name: Test Report
path: test_report.html
Configuration Breakdown
- Trigger Events: The workflow is triggered on push or pull request events to the main branch.
- Job Definition: The test job runs on the latest Ubuntu environment.
- Checkout Repository: Uses the actions/checkout@v3 action to check out the repository’s code.
- Set Up Ruby: Uses the ruby/setup-ruby@v1 action to set up Ruby. Specify the desired Ruby version.
- Install Dependencies: Installs the necessary gems (selenium-webdriver and headless).
- Install Chrome and ChromeDriver: Updates the package list, installs Google Chrome, and installs the latest version of ChromeDriver.
- Run Tests: Executes the Selenium test script (test_script.rb).
- Upload Test Report: Uses the actions/upload-artifact@v3 action to upload the generated HTML test report.
Running the Workflow
Once you commit and push the test.yml
file to your repository, GitHub Actions will automatically trigger the workflow on the specified events (push or pull request to the main branch).
To manually trigger the workflow, navigate to the “Actions” tab of your repository, select the workflow, and click “Run workflow.”
Viewing the Test Report
After the workflow completes, you can find the uploaded test report in the “Actions” tab under the specific workflow run. The “Test Report” artifact will be available for download.
Conclusion
You’ve successfully set up a GitHub Actions workflow to run your Selenium tests with Ruby. This setup ensures that your tests are automatically executed on every push or pull request, providing continuous feedback on the health of your application. You can extend this workflow by adding more steps, integrating with other services, or running additional tests.
By following the steps outlined in this guide, you can seamlessly run your selenium automation code on the GitHub actions as part of the CI.
I have created a project on GitHub and added the code here.
Feel free to hit clap if you like the content. Happy Automation Testing :) Cheers. 👏