This tutorial guides you through the process of integrating your Selenium UI tests into an Azure DevOps pipeline. By automating your UI tests, you can ensure the quality and stability of your web applications with every code change.
Ensure your Selenium test project is well-structured. Common project types include NUnit, xUnit, MSTest (for .NET), TestNG, JUnit (for Java), Pytest, Unittest (for Python), or Mocha, Jest (for JavaScript).
Your tests should be designed to run independently and report results in a standard format (like JUnit XML).
For running tests in a CI environment, it's highly recommended to configure Selenium to run in headless mode. This means the browser window won't be displayed visually, making it faster and suitable for server environments.
Example for Chrome (C#):
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu"); // Often needed for headless mode
options.AddArgument("--window-size=1920,1080");
IWebDriver driver = new ChromeDriver(options);
In your Azure DevOps project, go to Pipelines > Pipelines and click New pipeline.
Select your repository location (e.g., Azure Repos Git, GitHub) and authenticate if prompted.
Choose Starter pipeline or select a template relevant to your project.
You'll be presented with a YAML file that defines your pipeline. This is where you'll add tasks to build, test, and deploy.
Within your YAML pipeline, you need to add a task that will execute your Selenium tests. The specific task depends on your project type and the agent you're using.
If you're using .NET projects, the VSTest
task is commonly used.
pool:
vmImage: 'windows-latest' # Or 'ubuntu-latest' if your tests support it
steps:
- task: VSBuild@1
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"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
testResultsFiles: '**\*test*.trx'
mergeTestResults: true
failOnMinTestsNotRun: true
testFiltercriteria: '| Category=UITest' # Optional: filter tests by category
displayName: 'Run Selenium UI Tests'
- task: PublishTestResults@2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: '**\*test*.trx'
mergeTestResults: true
failOnMinTestsNotRun: true
displayName: 'Publish Test Results'
You would typically use a script defined in your package.json
and run it using the npm
or yarn
task.
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '18.x' # Specify your Node.js version
displayName: 'Install Node.js'
- script: |
npm install
npm run test:ui
displayName: 'Install Dependencies and Run UI Tests'
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit' # Assumes your test runner outputs JUnit XML
testResultsFiles: '**/TEST-*.xml'
mergeTestResults: true
failOnMinTestsNotRun: true
displayName: 'Publish Test Results'
Ensure that the correct WebDriver executable (e.g., chromedriver, geckodriver) is available on the build agent. You might need to install it using a package manager or download it as part of your pipeline. Some tasks or extensions can help manage this.
For example, you can use the WebDriverInstaller task from the Azure DevOps Marketplace.
Depending on your test setup, you might need to configure specific environments on your build agents:
Save your YAML pipeline and trigger a new run. Monitor the pipeline execution to identify any failures.
After the pipeline completes, navigate to the Tests tab in your pipeline run summary to view detailed test results, including any failures and screenshots.
Failing UI tests can indicate regressions in your application or issues with the test environment itself.
data-testid
attributes) and add waits.