SHIFT

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

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

View All Comments

tfspsseleniumsendkey

Implement Automatic Testing using Selenium and PowerShell in TFS - More TestPlans

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 TFS
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() 
You could leave a comment if you were logged in.
tfspsseleniumsendkey.txt · Last modified: 2021/09/24 00:25 (external edit)