PowerShell Code Example -Testautomation from an SAP UI5 Application with Selenium
For the testautomation comes SAP UI5 applications more and more in the focus. That's why I create an easy example with an SAP HANA On Demand demo application, which bases on UI5.
The example uses Google Chrome browser. The sections of the code are very easy to understand. At first we load the Selenium drivers and then the executes the Main routine. The Main routine opens Chrome with the SAP demo application URL. It selects the Shopping Card link and orders three products with the ShoppingCard function. Then it opens the orders and wait a few seconds. All in all nothing special or exciting, but a good knowledge base to handle UI5 applications.
It could be possible that the code contains language specific part, like the word 'Zurück'. Please pay attention for that.
#-Begin-----------------------------------------------------------------
#-Sub LoadSelenium------------------------------------------------------
#-
#- Selenium 3.12.0
#-
#-----------------------------------------------------------------------
Function LoadSelenium {
$Libraries = @(
"Selenium.WebDriverBackedSelenium.dll",
"WebDriver.dll",
"WebDriver.Support.dll"
);
$script:SeleniumPath = "C:\Program Files (x86)\Selenium";
ForEach($Library In $Libraries) {
[Void][System.Reflection.Assembly]::LoadFrom("$script:SeleniumPath\$Library");
}
}
#-Sub WaitUntilComplete-------------------------------------------------
Function WaitUntilComplete {
Do {
Start-Sleep -Milliseconds 250;
} Until ($script:driver.ExecuteScript("return document.readyState;") -eq "complete");
Start-Sleep -Milliseconds 1000;
}
#-Sub OpenChrome--------------------------------------------------------
#-
#- Google Chrome 67.0.3396.62
#-
#-----------------------------------------------------------------------
Function OpenChrome {
Param(
[String]$Url
)
$Browser = (Get-ItemProperty -Literalpath `
"Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe").'(default)';
$Options = New-Object OpenQA.Selenium.Chrome.ChromeOptions;
$Options.BinaryLocation = "$Browser";
$Options.AddArguments("--window-size=1440,900","--no-default-browser-check");
$script:driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($script:SeleniumPath, $Options);
If($script:driver.SessionId -eq $null) {
Exit;
}
$script:driver.Navigate().GoToUrl($Url);
WaitUntilComplete;
}
#-Sub ShoppingCart------------------------------------------------------
#-
#- URL checked 2018/06/13 with SAPUI5 v1.54.5
#-
#-----------------------------------------------------------------------
Function ShoppingCart {
Param(
[String]$Category,
[String]$Product
)
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementExists([OpenQA.Selenium.By]::XPath("//div[contains(text(),'$Category')]")));
$webElement.Click();
WaitUntilComplete;
If($script:driver.FindElementById("cart---categoryView--page-title-inner").Text -ne $Category) {
$script:driver.ExecuteScript("arguments[0].scrollIntoView(true);", $webElement);
$webElement.Click();
}
WaitUntilComplete;
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementExists([OpenQA.Selenium.By]::XPath("//span[contains(text(),'$Product')]")));
$script:driver.ExecuteScript("arguments[0].scrollIntoView(true);", $webElement);
$webElement.Click();
WaitUntilComplete;
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementExists([OpenQA.Selenium.By]::XPath("//button[@title='Add the product to your shopping cart']")));
$webElement.Click();
WaitUntilComplete;
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementIsVisible([OpenQA.Selenium.By]::XPath("//div[starts-with(text(), 'Product ') and contains(text(), ' added to your shopping cart')]")));
Write-Host $webElement.Text;
$script:driver.FindElementByXPath("//button[@title='Zurück']").Click();
Start-Sleep -Milliseconds 250;
}
#-Sub Main--------------------------------------------------------------
Function Main {
$Url = "https://sapui5.hana.ondemand.com/demoapps.html";
OpenChrome;
$script:Wait = [OpenQA.Selenium.Support.UI.WebDriverWait]::`
New($script:driver, [System.TimeSpan]::FromSeconds(30));
$script:Action = [OpenQA.Selenium.Interactions.Actions]::new($script:driver);
$script:driver.Navigate().GoToUrl($Url);
WaitUntilComplete;
#-Shopping Cart-------------------------------------------------------
$App = "Shopping Cart";
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementExists([OpenQA.Selenium.By]::XPath("//span[contains(text(),'$App')]")));
$script:driver.ExecuteScript("arguments[0].scrollIntoView();", $webElement);
$Parent = $webElement.FindElementByXPath("..").FindElementByXPath("..").FindElementByXPath("..")
$Parent.FindElementByLinkText("Open Demo App").Click();
WaitUntilComplete;
#-Switch to new tab---------------------------------------------------
$newTab = $script:driver.WindowHandles[1];
[void]$script:driver.SwitchTo().Window($newTab);
WaitUntilComplete;
#-Category in alphabetical order--------------------------------------
ShoppingCart -Category "Computer System Accessories" -Product "USB Stick";
ShoppingCart -Category "Keyboards" -Product "Media Keyboard";
ShoppingCart -Category "Telecommunication" -Product "Wireless DSL Router / Repeater";
#-Close new tab-------------------------------------------------------
[void]$script:driver.SwitchTo().Window($newTab).Close();
$newTab = $script:driver.WindowHandles[0];
[void]$script:driver.SwitchTo().Window($newTab);
WaitUntilComplete;
#-Browse Orders-------------------------------------------------------
$App = "Browse Orders";
$webElement = $script:Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::`
ElementExists([OpenQA.Selenium.By]::XPath("//span[contains(text(),'$App')]")));
$script:driver.ExecuteScript("arguments[0].scrollIntoView();", $webElement);
$Parent = $webElement.FindElementByXPath("..").FindElementByXPath("..").FindElementByXPath("..")
$Parent.FindElementByLinkText("Open Demo App").Click();
WaitUntilComplete;
#-Switch to new tab---------------------------------------------------
$newTab = $script:driver.WindowHandles[1];
[void]$script:driver.SwitchTo().Window($newTab);
WaitUntilComplete;
Start-Sleep -Seconds 5;
#-Close new tab-------------------------------------------------------
[void]$script:driver.SwitchTo().Window($newTab).Close();
$newTab = $script:driver.WindowHandles[0];
[void]$script:driver.SwitchTo().Window($newTab);
WaitUntilComplete;
$script:driver.Close();
$script:driver.Quit();
}
#-Main------------------------------------------------------------------
LoadSelenium;
Main;
#-Error Routine---------------------------------------------------------
Trap {
$date = Get-Date -Format "yyyMMdd";
$time = Get-Date -Format "HHmmssfff";
$FolderPath = "$($env:USERPROFILE)\Desktop\Errors";
If(!(Test-Path -Path $FolderPath)) {
New-Item -ItemType Directory -Path $FolderPath > $Null;
}
If($script:driver.SessionId -ne $null) {
$ScreenShot = $script:driver.GetScreenshot();
$ScreenShot.SaveAsFile("$($FolderPath)\$($date)_$($time).jpg", `
[OpenQA.Selenium.ScreenshotImageFormat]::Jpeg);
$script:driver.Close();
$script:driver.Quit();
}
$ErrorText = "Error " + $_.Exception.GetType().FullName + `
" in line " + $_.InvocationInfo.ScriptLineNumber + `
"`r`n" + $_.Exception.Message;
Set-Content -Path "$($FolderPath)\$($date)_$($time).txt" `
-Value $ErrorText > $Null;
Write-Host "Failed";
Write-Host $ErrorText
Exit;
}
#-End-------------------------------------------------------------------