You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Ruby开发的Selenium脚本与HP ALM集成(触发执行+结果回写)

Hey Brenda, great question! Integrating your Ruby-based Selenium scripts with HP ALM (now rebranded as Micro Focus ALM/Quality Center) to trigger runs directly from ALM and push results back is totally achievable. Let’s break this down into actionable steps tailored to your setup:

1. Pre-Requisites First

Before diving into code, make sure you have these bases covered:

  • ALM API Access: Grab credentials for a user with permissions to create test runs, update results, and access your target ALM project/domain.
  • Ruby Gems: You’ll need rest-client for ALM API calls (plus selenium-webdriver which you likely already have). Install them with:
    gem install rest-client selenium-webdriver
    
  • Test Case Mapping: Link each Selenium script to a corresponding test case in ALM’s Test Plan. Jot down the test case ID, project name, and domain name—you’ll need these later.
2. Triggering Selenium Scripts from HP ALM

You’ve got two solid options here, depending on your workflow needs:

Option 1: ALM Custom Test Type (Simplest for Direct Execution)

This lets you run scripts directly from ALM’s Test Lab:

  • In ALM, navigate to your project > Test Plan > Create a new test of type Custom Test.
  • In the test’s details, set the execution command to run your Ruby script, passing the ALM test case ID as a command-line argument (so your script knows which test to map results to):
    ruby /absolute/path/to/your/selenium_script.rb --testcase-id=TC12345
    
  • To trigger execution, add this custom test to a Test Lab test set, then click Run. Note: The machine running ALM’s test runner needs Ruby, Selenium dependencies, and access to your script files (use a shared network drive if running remotely).

Option 2: ALM REST API + Webhook (For Flexible Workflows)

If you want to trigger scripts via ALM workflows (e.g., when a test set is marked "Ready"), set up a lightweight Ruby web service:

  • Build a small Sinatra/Rails endpoint that listens for ALM webhook events.
  • When the endpoint receives a trigger from ALM, it executes your Selenium script in the background.
3. Writing Results Back to ALM

This is the core piece—use ALM’s REST API to push your Selenium test status and comments directly into ALM’s Test Lab. Here’s a reusable Ruby snippet to integrate into your scripts:

Step 1: ALM Authentication

First, handle login to get a valid session cookie:

require 'rest-client'
require 'json'
require 'base64'

# ALM Configuration - update these with your details
ALM_DOMAIN = "your_domain"
ALM_PROJECT = "your_project"
ALM_URL = "https://your-alm-instance.com/qcbin"
ALM_USER = "your_alm_username"
ALM_PASS = "your_alm_password"

def alm_authenticate
  # Send auth request
  response = RestClient.post(
    "#{ALM_URL}/authentication-point/authenticate",
    "",
    { 'Authorization' => "Basic #{Base64.strict_encode64("#{ALM_USER}:#{ALM_PASS}")}" }
  )
  # Return session cookie for subsequent requests
  response.cookies
end

Step 2: Update Test Results

Add this function to create a test run and update its status in ALM:

def push_result_to_alm(testcase_id, status, comments = "")
  cookie = alm_authenticate

  # Get the test instance ID linked to your test case (from Test Lab)
  test_instances = JSON.parse(RestClient.get(
    "#{ALM_URL}/rest/domains/#{ALM_DOMAIN}/projects/#{ALM_PROJECT}/test-instances?query={test-id[#{testcase_id}]}",
    cookies: cookie
  ))
  test_instance_id = test_instances['entities'][0]['id']

  # Build payload for new test run
  run_payload = {
    'fields' => {
      'test-instance' => {'id' => test_instance_id},
      'status' => status, # Valid values: "Passed", "Failed", "Not Completed", "Blocked"
      'comments' => comments
    }
  }.to_json

  # Send result to ALM
  run_response = RestClient.post(
    "#{ALM_URL}/rest/domains/#{ALM_DOMAIN}/projects/#{ALM_PROJECT}/runs",
    run_payload,
    { 'Content-Type' => 'application/json', cookies: cookie }
  )
  puts "Successfully updated ALM! Run ID: #{JSON.parse(run_response)['id']}"
end

Step 3: Hook into Your Selenium Script

Call the push_result_to_alm function at the end of your test, using the command-line argument for the test case ID:

# Example: After running your Selenium test logic
begin
  # Your Selenium test code here...
  push_result_to_alm(ARGV[0], "Passed", "Selenium test completed: All UI flows passed successfully")
rescue StandardError => e
  push_result_to_alm(ARGV[0], "Failed", "Test failed with error: #{e.message}")
end
4. Pro Tips for Reliability
  • Error Handling: Wrap your Selenium logic in try-catch blocks to map exceptions directly to ALM statuses (e.g., timeout = "Failed", missing element = "Failed").
  • Detailed Comments: Push specific failure details (like screenshot paths or step numbers) into ALM comments to speed up debugging.
  • Permission Checks: Double-check that your ALM API user has permissions to create test runs, update test instances, and access the target project/domain.
  • Logging: Log local execution details and ALM API responses to a file for troubleshooting.

内容的提问来源于stack exchange,提问作者Brenda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:24:32