wiki.getshifting.com

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


tfspsseleniumsendkey

Implement Automatic Testing using Selenium and PowerShell in Azure DevOps Server - More TestPlans

Summary: How to Implement Automatic Testing using Selenium and PowerShell in Azure DevOps Server - pt 3.
Date: Around 2020
Refactor: 29 April 2025: Checked links and formatting. Rebranded Team Foundation Server (TFS) to Azure Devops Server.

In this article I will use the Selenium webdriver for Chrome in PowerShell to implement automatic testing. For firefox or the original testplan, see Implement Automatic Testing using Selenium and PowerShell in Azure DevOps Server
Used technologies:

  • Selenium webdriver for Chrome
  • PowerShell
Note that I only list chrome here. Chrome works a bit cleaner and the code is exactly the same.

PowerShell Script - Send Keys

# Check for.net version of minimum 4.5:
if (!(Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' |  Get-ItemPropertyValue -Name Release | Foreach-Object { $_ -ge 394802 })){
	exit
}else{
	write-host "mimimum .net 4.5 version found. Continue"
}
 
# Load the Selenium .Net library and paths to the various test libraries
# webdriver & support dll
Add-Type -Path "D:\selenium\WebDriver.dll"
Add-Type -Path "D:\selenium\WebDriver.Support.dll"
# Add path for chromedriver.exe and firefox geckodriver.exe
$env:PATH += ";D:\selenium"
 
# Testplan variables
$testurl = "https://www.google.com"
$testname = "google"
$resultsdir = "D:\selenium\testresults"
[OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png
 
### Chrome testplan #########################################################################################################
# Start Chrome headless
Write-Host "Start chrome testplan, starting chrome headless"
$chromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$chromeOptions.addArguments('headless')
$chromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeOptions)
 
# Start chrome testplan
Write-Host "Go to url $testurl"
$chromedriver.Navigate().GoToURL($testurl)
Write-Host "Start Searching"
$ChromeDriver.FindElementByName("q").SendKeys("Getshifting")
$ChromeDriver.FindElementByName("btnK").Submit()
 
# Get Evidence that the website works
$chromecurrenturl = $chromeDriver.url
$chromecurrenturltitle = $chromeDriver.title
Write-Host "Current Chrome url  $chromecurrenturl and title $chromecurrenturltitle"
# Get Pagesource
Write-Host "Create pagesource $chromecurrenturl"
$chromeDriver.PageSource | Out-File "$resultsdir\chrome$testname.html" -Force
# Get Screenshots
Write-Host "Create screenshot $chromecurrenturl"
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($chromeDriver)
$Screenshot.SaveAsFile("$resultsdir\chrome$testname.png", $ImageFormat)
 
# Close Chrome
Write-Host "Close and quit chrome browser and selenium webdriver. "
$chromeDriver.Close()
$chromeDriver.Quit()
tfspsseleniumsendkey.txt · Last modified: by 127.0.0.1