SHIFT

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

User Tools

Site Tools


Sidebar

Sponsor:


Recently Changed Pages:

View All Pages


View All Tags







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

PageDateDiscussionTags
2019/11/18 13:52 1 Comment

View All Comments

tfspsseleniumauth

Implement Automatic Testing With Authentication using Selenium and PowerShell in TFS

This page is a followup on Implement Automatic Testing using Selenium and PowerShell in TFS and will only note the differences.

PowerShell Script

First thing is to add the PowerShell script to the TFS Code Repostory. Go to Code → PowerShell folder (create it if it doesn't exist yet). Create a new file as seleniumtestauth.ps1 and add the code (see below the code for necessary explanation:

Param(
   [string]$linkedinuser,
   [string]$linkedinpass
)
 
# 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.linkedin.com/uas/login"
$testname = "linkedin"
$resultsdir = "D:\selenium\testresults"
[OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png
 
### Firefox testplan #########################################################################################################
# Start firefox headless 
Write-Host "Start firefox testplan, starting firefox headless"
[OpenQA.Selenium.Firefox.FirefoxOptions]$ffoptions = new-object OpenQA.Selenium.Firefox.FirefoxOptions
$ffoptions.addArguments('-headless')
$ffdriver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver ($ffoptions)
# Sometimes headless doesn't work through arguments. Setting the headless environment variable to make sure we're headless
$env:MOZ_HEADLESS = 1
 
# Start firefox testplan
Write-Host "Go to url $testurl"
$ffdriver.Navigate().GoToURL($testurl)
Write-Host "Log in at $testurl"
$ffdriver.FindElementByName("session_key").SendKeys($linkedinuser) 
$ffdriver.FindElementByName("session_password").SendKeys($linkedinpass) 
$ffdriver.FindElementByName("session_password").Submit() 
# This might take a few moments
sleep 5
 
# Get Evidence that the website works
$ffcurrenturl = $ffdriver.url
$ffcurrenturltitle = $ffdriver.title
Write-Host "Current FF url  $ffcurrenturl and title $ffcurrenturltitle"
# Get Pagesource
Write-Host "Create pagesource $ffcurrenturl"
$ffdriver.PageSource | Out-File "$resultsdir\ff$testname.html" -Force
# Get Screenshots
Write-Host "Create screenshot $ffcurrenturl"
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($ffdriver)
$Screenshot.SaveAsFile("$resultsdir\ff$testname.png", $ImageFormat)
 
# Close firefox
Write-Host "Close and quit firefox browser and selenium webdriver. "
$ffdriver.Close() 
$ffdriver.Quit()
 
### 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 "Log in at $testurl"
$chromeDriver.FindElementByName("session_key").SendKeys($linkedinuser) 
$chromeDriver.FindElementByName("session_password").SendKeys($linkedinpass) 
$chromeDriver.FindElementByName("session_password").Submit() 
# This might take a few moments
sleep 5
 
# 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() 

The script performs the following steps:

  • Check for the required .Net version
    • You can do that manually using the following PowerShell command: Get-WindowsFeature -name NET* | where installed
  • Add the required path variables for selenium. I put the required files in D:\selenium and created the directory D:\selenium\results
  • Start the testplan per browser:
    • Go to url
    • Log in on LinkedIn
    • Output some information like the url and the pagetitle to the logfile
    • Download the pagesource and make screenshots of the page

Manual Testing

While testing manually you can use the following code to enter your credentials:

# Get LinkedIn Credentials
$PSCred = Get-Credential -Message 'Enter LinkedIn Username and Password'

And then use this code for logging in with firefox:

$ffdriver.FindElementByName("session_key").SendKeys($PSCred.Username) 
$ffdriver.FindElementByName("session_password").SendKeys($PSCred.GetNetworkCredential().password) 
$ffdriver.FindElementByName("session_password").Submit() 

And then use this code for logging in with chrome:

$chromeDriver.FindElementByName("session_key").SendKeys($PSCred.Username) 
$chromeDriver.FindElementByName("session_password").SendKeys($PSCred.GetNetworkCredential().password) 
$chromeDriver.FindElementByName("session_password").Submit() 

Create Build Tasks

We need to set the script on the build server so it can be run during the release. We need to do so now because the powershell folder is not included in the artifact. In the build definition, click “+ Add Task” and search for “Copy Files”. Click the “Copy Files” task and click Add. Configure the task like this:

  • Display Name: Copy Files to: D:\selenium
  • Source Folder: Powershell (use the icon with the three dots to navigate to the correct folder)
  • Contents: seleniumtestauth.ps1
  • Target Folder: D:\selenium
  • Advanced settings:
    • Overwrite: Enabled

Create Release Tasks

Variables

Create two variables in the Release definition and name them linkedinuser and linkedinpass and provide them with the correct values. Don't forget to set the linkedinpass as a secret (by clicking the lock).

PowerShell Task

In the release definition, in the newly created Agent Phase, click “+” and search for “PowerShell”. Click the “PowerShell” task and click Add. Configure the task like this:

  • Display name: PS Start Selenium Test
  • Type: File Path
  • Script Path: d:\selenium\seleniumtestauth.ps1
  • Arguments: -linkedinuser $(linkedinuser) -linkedinpass $(linkedinpass)
  • Advanced settings:
    • Working Folder: D:\Selenium
  • Control Options:
    • Continue on error: Enabled (The firefox browser produces output in headless mode that is being diagnosed as an error in TFS. By enabling this option these errors are ignored)
Again, check Implement Automatic Testing using Selenium and PowerShell in TFS for all otherv steps that are required.

Discussion

Enter your comment. Wiki syntax is allowed:
G R T G J
 
tfspsseleniumauth.txt · Last modified: 2019/11/18 12:25 (external edit)