Bamboo

Run traces from Bamboo.

1. Navigate to the build job configuration

Build
  ↳All build plans
   ↳Select a build plan
    ↳Actions
     ↳Configure
      ↳Job

2. Add Task “Script”

Linux

Shell script to use when Bamboo is running on Linux.

wget https://usetrace.com/integration/usetrace-run.sh
# Usage: usetrace-run-sh <projectid> <baseurl> <browsers> <tags>
# Example: usetrace-run.sh <YOUR_PROJECT_ID> <YOUR_BASE_URL> chrome,firefox,ie9,ie10,ie11 smoke,regression 
# The curl command below waits until the page is up and the testing may start
curl --retry 10 --retry-delay 5 -v <YOUR_BASE_URL>
bash usetrace-run.sh <YOUR_PROJECT_ID> <YOUR_BASE_URL> chrome

Windows

Powershell script to use when Bamboo is running on Windows.

#
# Trigger Usetrace tests and poll for results.
# Save results in target/test-reports in xUnit XML format.
###########################################################

param (
    [string]$projectId = $(throw "-projectId is required."),
    [string]$baseUrl = $(throw "-baseUrl is required."),
    [array]$browsers,
    [array]$tags,
    [string]$buildUrl,
    [string]$buildNumber
)

$target="target/test-reports"
$api="http://api.usetrace.com"
$connectTimeout=10
$maxwaitSeconds=1200

Function Usage () {
    Write-Host "Usage:"
    Write-Host "./usetrace-run-ps.1 -projectId <YOUR_PROJECT_ID> -baseUrl <YOUR_BASE_URL> -browsers [chrome,firefox,ie10] -tags [smoke,flaky]"
}

Function GetCapabilities () {
    $brwsrs = @()

    foreach($browser in $browsers) {
        $brwsr = @{}
        if ($browser -like "ie*") {
            $ieVersion = $browser.Replace("ie", "")
            $brwsr["browserName"] = "internet explorer"
            $brwsr["version"] = $ieVersion
        } else {
            $brwsr["browserName"] = $browser
        }
       
        $brwsrs += $brwsr
    }
    $requiredCapabilities = $brwsrs
    return $requiredCapabilities
}

Function Execute {
    $headers = @{}
    $headers["Content-Type"] = "application/json"

    $body = @{}
    $body["commit"] = $buildNumber
    $body["commitLink"] = $buildUrl
    $body["tags"] =$tags
    $body["baseUrl"] = $baseUrl
    $requiredCapabilities = GetCapabilities
    $body["requiredCapabilities"] = @($requiredCapabilities)
    $body = (ConvertTo-Json $body)

    $loginUri = "$api/account/login"
    $uri = "$api/api/project/$projectId/execute_all"

    try {
        $r = (Invoke-WebRequest -ContentType "application/json" -Uri $uri -TimeoutSec $connectTimeout -Body $body -Method POST)
    }
    catch
    {
        write-host "Caught an exception:"
        write-host "Exception Type: $($_.Exception.GetType().FullName)"
        write-host "Exception Message: $($_.Exception.Message)"
        Exit 1
    }
    try {
        $response = ConvertFrom-Json $r
        if ($response.status -eq "error") {
            Write-Host "Execution failed:" $response.error
            Exit 1
        }
    } catch {
        return $r.Content
    }
    
}

function ReadResults([string]$batchId) {
    $result = (Invoke-WebRequest -Uri $api/api/results/$batchId/xunit -TimeoutSec $connectTimeout)
    return $result
}

Function WriteResultFile([string]$result) {
    New-Item -ItemType directory -Path $target -Force
    $dateStr = Get-Date -format "yyyy-MM-dd_HHmmss"
    $targetFile="$target/usetrace-$dateStr.xml"
    Out-File $targetFile -InputObject $result

    $errorsFound = 0
    [xml]$results = $result

    foreach ($testcase in $results.testsuite.testcase) {
        $name = $testcase.name
        $time = $testcase.time
        $message = $testcase.message
        if ($message.length -gt 0) {
            $errorsFound += 1
        }
    }

    if ($errorsFound -gt 0) {
        Write-Host "Some traces failed!"
        Exit 1
    }
    Write-Host "All traces passed!"
    Exit 0
}

Function GetResults([string]$batchId) {
    $interval=5
    $waited = 0
    while(($waited -le $maxwaitSeconds)) {
        [string]$results = ReadResults($batchId)
        if ($results.Contains("testsuite"))  {
            WriteResultFile($results)
        }
        if ($waited -ge $maxwaitSeconds) {
            write-host "Results did not finish in time. Aborting."
            Exit 1
        }     
        Write-Host "Waiting $interval seconds before polling again..."
        Start-Sleep -s $interval
        $waited = $waited + $interval
    }
   
}

$batch = Execute
GetResults($batch)

3. Add Task JUnit Parser

To show test results in Bamboo.