Sauce Labs with Azure DevOps
Azure DevOps (formerly Visual Studio Team Services or VSTS) is a Microsoft product that provides version control, reporting, requirements management, project management, automated builds, testing and release management capabilities.
What You'll Need
- A Sauce Labs account (Log in or sign up for a free trial license)
- Your Sauce Labs username and access key
- An existing Azure DevOps pipeline
Using Azure DevOps
Follow the instructions below to integrate Sauce Labs testing into your Azure pipeline.
- Sign in to your Azure DevOps organization and go to your project.
- Go to Pipelines > New pipeline.
- Link the new pipeline to your repository (see Azure Pipelines Documentation for guidance). You'll likely need to provide permissions for Azure Pipelines to access your repository management system.
- Set your Sauce Labs username and access key as environment variables in your pipeline by clicking Pipeline > Variables, and then pasting the values of your username and access key.
- In your source code, you'll need to reference the Sauce Labs environment variables that you set in Azure DevOps. For example:
C# example
var sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");
-
Create a YAML file using one of the templates below. You'll also need to reference your Sauce Labs environment variables here.
- Node.js
- Java
- C#
trigger:- mainpool:vmImage: ubuntu-latest# Multiple pipelines can re-use variables# that are stored in a variable groupvariables:- group: sauce-labs-variablessteps:- task: NodeTool@0inputs:versionSpec: '14.x'displayName: 'Install Node.js'- script: |# Navigate to the working directorycd ./webdriverio/webdriver/examples/w3c/# Install node packagesnpm install# Run tests on Sauce and enables a high level of logging for CInpm run test.saucelabs.us -- --logLevel "debug"|env:# Reads the value from 'sauceUsername' in Azure DevOps and# stores it into SAUCE_USERNAME env variableSAUCE_USERNAME: $(sauceUsername)SAUCE_ACCESS_KEY: $(sauceAccessKey)displayName: 'install and run WebdriverIO tests in Sauce Labs'# Build your Java project and run tests with Apache Maven.trigger:– mainpr:– mainpool:vmImage: 'ubuntu-latest'# Sets the environment variables for the pipeline.# We create a variable sauce_user and assign it a value of $(SAUCE_USERNAME), which comes from the Azure DevOps.variables:– name: sauce_uservalue: $(SAUCE_USERNAME)– name: sauce_keyvalue: $(SAUCE_ACCESS_KEY)steps:– bash: echo $SAUCE_USER– bash: echo $SAUCE_KEY# Builds and runs the tests in the Maven project.– task: Maven@3inputs:mavenPomFile: 'pom.xml'mavenOptions: '-Xmx3072m'javaHomeOption: 'JDKVersion'jdkVersionOption: '1.8'jdkArchitectureOption: 'x64'publishJUnitResults: truetestResultsFiles: '**/surefire-reports/TEST-*.xml'goals: 'package'pool:name: Hosted VS2017demands:– msbuild– visualstudio– vsteststeps:– script: setdisplayName: print all variables– task: NuGetToolInstaller@0displayName: 'Use NuGet 4.4.1'inputs:versionSpec: 4.4.1– task: NuGetCommand@2displayName: 'NuGet restore'inputs:restoreSolution: '**\*.sln'– task: VSBuild@1displayName: 'Build solution'inputs:solution: '**\*.sln'msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactstagingdirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'logProjectEvents: true– task: PowerShell@2displayName: 'Set Sauce Environment Variables'inputs:targetType: filePathfilePath: ./setEnvironmentVariables.ps1arguments: '$env:SAUCE_USER $env:SAUCE_KEY'# Using powershell ##vso command to set an environment variable in the system– powershell: |Write-Host "Sauce Username stored in Azure DevOps variables is=>$($env:SAUCE_USER)";Write-Host "Sauce Access Key stored in Azure DevOps variables is=>$($env:SAUCE_KEY)";Write-Host "Sauce Username stored in Env variables is=>$($env:SAUCE_USERNAME)";Write-Host "Sauce Access Key stored in Env variables is=>$($env:SAUCE_ACCESS_KEY)";Write-Host "Sauce Build Repository URI stored in Env variables is=>$($env:BUILD_REPOSITORY_URI)";|# Checking to make sure that environment variables were set between yml tasks– powershell: |Write-Host "Sauce Username stored in Env Variables variables is=>$($env:SAUCE_USERNAME)";Write-Host "Sauce Access Key stored in Azure DevOps variables is=>$($env:SAUCE_ACCESS_KEY)";displayName: display env variables bw posh tasks– task: VSTest@2displayName: 'Run Best Practices Framework'inputs:searchFolder: '$(System.DefaultWorkingDirectory)'testSelector: 'testAssemblies'testAssemblyVer2: |**\*Selenium*.dll!**\SauceExamples\packages\*.dll!**\packages\*.dlltestFiltercriteria: 'TestCategory=BestPractices'runInParallel: truecodeCoverageEnabled: truetestRunTitle: 'NUnit Automation Framework'rerunFailedTests: truererunFailedThreshold: 10rerunMaxAttempts: 2failOnMinTestsNotRun: true