Last active
December 17, 2015 10:59
-
-
Save ivlists/5598756 to your computer and use it in GitHub Desktop.
Includes - A Number of useful functions and utilities to find WebElement effectively and reliably using selenium web driver for UI automation. ElementLocator : 1. FindElementSmart() - Which finds WebElement on web page intelligently using Multiple search criteria like by Xpath, Id, Name, ClassName, Value, LinkText, PartialLinkText etc.
2. Good e…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------------------------------------------------------------------------------- | |
// <summary> | |
// @Description - browser class contains browser specific methods such as launching the browser | |
// and checking whether the browser is exist or not. | |
// @Author - Vaibhav | |
// @Date - 01 / 03 / 2012 | |
// </summary> | |
// -------------------------------------------------------------------------------------------------------------------- | |
namespace BVT.TestCases.Utilities | |
{ | |
using System; | |
using System.Diagnostics; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Firefox; | |
using OpenQA.Selenium.IE; | |
using OpenQA.Selenium.Remote; | |
/// <summary> | |
/// @Description - browser class contains browser specific methods such as launching the browser | |
/// and checking whether the browser is exist or not. | |
/// @Author - Vaibhav | |
/// @Date - 01 / 03 / 2012 | |
/// </summary> | |
public class Browser | |
{ | |
#region Static Fields | |
/// <summary> | |
/// The web driver. | |
/// </summary> | |
private static IWebDriver webDriver = null; | |
#endregion | |
#region Public Methods and Operators | |
/// <summary> | |
/// @Description - GetBrowser function launches a new firefox browser instance. | |
/// </summary> | |
/// <returns>Object of IWebDriver</returns> | |
public static bool CloseBrowserInstances() | |
{ | |
string browser; | |
bool isResult = true; | |
browser = DataLocators.configData["browser"]; | |
try | |
{ | |
// Verify browser | |
if (browser == "iexplorer") | |
{ | |
// Collect All Running Processes | |
Process[] localByName = Process.GetProcesses(); | |
// Traverse each process | |
foreach (Process item in localByName) | |
{ | |
// Verify iexplore process | |
if (item.ProcessName.Equals("iexplore")) | |
{ | |
// kill brower | |
item.Kill(); | |
} | |
} | |
} | |
} | |
catch (InvalidOperationException) | |
{ | |
// Write to Log | |
Logger.Log.InfoFormat("IE: Failed to Close IE... FAIL"); | |
} | |
catch (NotFoundException nf) | |
{ | |
// Write to Log | |
Logger.Log.InfoFormat("IE: No instances of IE is Opened..." + nf.Message + " FAIL"); | |
} | |
catch (Exception e) | |
{ | |
// Write to Log | |
// GlobalController.WriteTCLogs("IE", "", "Failed to Close IE..." + e.Message + " FAIL"); | |
Logger.Log.InfoFormat("IE: No instances of IE is Opened..." + e.Message + " FAIL"); | |
} | |
return isResult; | |
} | |
/// <summary> | |
/// @Description - GetBrowser function launches a new browser instance. | |
/// </summary> | |
/// <returns>Object of IWebDriver</returns> | |
public IWebDriver GetBrowser() | |
{ | |
string browser; | |
browser = DataLocators.configData["browser"]; | |
Process[] localByName; | |
switch (browser) | |
{ | |
case "iexplorer": | |
// kill existing iexplore processes | |
localByName = Process.GetProcesses(); | |
foreach (Process item in localByName) | |
{ | |
if (item.ProcessName.Equals("iexplore")) | |
{ | |
item.Kill(); | |
} | |
} | |
// Ignore protected mode settings for internet explorer | |
var options = new InternetExplorerOptions(); | |
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; | |
webDriver = new InternetExplorerDriver(options); | |
break; | |
case "chrome": | |
localByName = Process.GetProcesses(); | |
// kill existing firefox processes | |
foreach (Process item in localByName) | |
{ | |
if (item.ProcessName.Contains("firefox")) | |
{ | |
// item.Kill(); | |
} | |
} | |
webDriver = new ChromeDriver(); | |
break; | |
case "firefox": | |
localByName = Process.GetProcesses(); | |
// kill existing firefox processes | |
foreach (Process item in localByName) | |
{ | |
if (item.ProcessName.Contains("firefox")) | |
{ | |
item.Kill(); | |
} | |
} | |
webDriver = new FirefoxDriver(); | |
break; | |
} | |
return webDriver; | |
} | |
/// <summary> | |
/// @Description - IsFirefoxBrowserExist function checks whether the firefox browser | |
/// exist or not. | |
/// </summary> | |
/// <returns>True if browser exist else returns false</returns> | |
public bool IsFirefoxBrowserExist() | |
{ | |
bool firefoxBrowserStatus = false; | |
Process[] localByName = Process.GetProcesses(); | |
foreach (Process item in localByName) | |
{ | |
if (item.ProcessName.Equals("firefox")) | |
{ | |
firefoxBrowserStatus = true; | |
} | |
} | |
return firefoxBrowserStatus; | |
} | |
/// <summary> | |
/// Gets the browser. | |
/// </summary> | |
/// <returns> | |
/// The <see cref="string"/>. | |
/// </returns> | |
public static string GetBrowser() | |
{ | |
string browser = ConfigParameters.Browser; | |
string programFiles = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | |
if (ConfigParameters.Browser.Equals("iexplore")) | |
{ | |
browser = "iexplore"; | |
} | |
if (ConfigParameters.Browser.Equals("firefox")) | |
{ | |
if (Directory.Exists(programFiles + " (x86)")) | |
{ | |
browser = "firefox3 " + programFiles + " (x86)\\Mozilla Firefox\\firefox.exe"; | |
} | |
else | |
{ | |
browser = "firefox3 " + programFiles + "\\Mozilla Firefox\\firefox.exe"; | |
} | |
} | |
if (ConfigParameters.Browser.Equals("googlechrome")) | |
{ | |
browser = "googlechrome"; | |
} | |
return browser; | |
} | |
#endregion | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------------------------------------------------------------------------------- | |
// <summary> | |
// A generic Method logger, inspired from: | |
// Author: vaibhav | |
// http://blogs.msdn.com/b/morgan/archive/2008/12/18/method-entry-exit-logging.aspx | |
// use this in your method by(note caller need to pass method in to avoid relfection, which is slow): | |
// <c> | |
// public void YourCode() | |
// { | |
// using (MethodLogger.Log("YourCode", LogOptions.All, logger)) | |
// { | |
// // Write your stuff here | |
// } | |
// } | |
// </c> | |
// </summary> | |
// -------------------------------------------------------------------------------------------------------------------- | |
namespace BVT.TestCases.Utilities | |
{ | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
using log4net; | |
/// <summary> | |
/// Options used when logging a method | |
/// </summary> | |
[Flags] | |
public enum LogOptions | |
{ | |
/// <summary> | |
/// Log entry into the method | |
/// </summary> | |
Entry = 0x01, | |
/// <summary> | |
/// Log exit from the method | |
/// </summary> | |
Exit = 0x02, | |
/// <summary> | |
/// Log the execution time of the method | |
/// </summary> | |
ExecutionTime = 0x04, | |
/// <summary> | |
/// Log all data | |
/// </summary> | |
All = 0x07 | |
} | |
/// <summary> | |
/// A generic Method logger, constructor was made private to prevent user from creating it directly, | |
/// use this in your method by(note caller need to pass method in to avoid relfection, which is slow): | |
/// <c> | |
/// public void YourCode() | |
/// { | |
/// using (MethodLogger.Log("YourCode", LogOptions.All, logger)) | |
/// { | |
/// // Write your stuff here | |
/// } | |
/// } | |
/// </c> | |
/// </summary> | |
public sealed class MethodLogger : IDisposable | |
{ | |
/// <summary> | |
/// The _method name to log | |
/// </summary> | |
private readonly string methodName; | |
/// <summary> | |
/// The <see cref="BVT.TestCases.Utilities.LogOptions" /> | |
/// </summary> | |
private readonly LogOptions options; | |
/// <summary> | |
/// The stopwatch for calculating method execution time | |
/// </summary> | |
private readonly Stopwatch stopwatch; | |
/// <summary> | |
/// The log4net logger to do actual logging | |
/// </summary> | |
private readonly ILog source; | |
/// <summary> | |
/// Prevents a default instance of the <see cref="MethodLogger" /> class from being created. | |
/// </summary> | |
/// <param name="methodName">The name of the method being logged</param> | |
/// <param name="options">The log options</param> | |
/// <param name="source">The source.</param> | |
private MethodLogger(string methodName, LogOptions options, ILog source) | |
{ | |
this.methodName = methodName; | |
this.options = options; | |
this.source = source; | |
if ((this.options & LogOptions.ExecutionTime) == LogOptions.ExecutionTime) | |
{ | |
this.stopwatch = new Stopwatch(); | |
this.stopwatch.Start(); | |
} | |
if ((this.options & LogOptions.Entry) == LogOptions.Entry) | |
{ | |
this.source.InfoFormat("### Entering method {0}....", methodName); | |
} | |
} | |
/// <summary> | |
/// Log method entry | |
/// </summary> | |
/// <param name="methodName">The name of the method being logged</param> | |
/// <param name="options">The log options</param> | |
/// <param name="source">The ILog that events are written to</param> | |
/// <returns> | |
/// A disposable object or none if logging is disabled | |
/// </returns> | |
public static IDisposable Log(string methodName, LogOptions options, ILog source) | |
{ | |
IDisposable logger = null; | |
// Check if ExecutionTime logging is requested, and if so log if Verbose | |
// logging (or greater) is chosen | |
bool shouldCreate = ((options & LogOptions.ExecutionTime) == | |
LogOptions.ExecutionTime) && IsVerbose(source); | |
// If not logging ExecutionTime, log only if Entry or Exit tracing is requested | |
if (!shouldCreate) | |
{ | |
shouldCreate = (((options & LogOptions.Entry) == LogOptions.Entry) | |
| ((options & LogOptions.Exit) == LogOptions.Exit)); | |
} | |
// Check if we actually need to log anything | |
if (shouldCreate) | |
{ | |
logger = new MethodLogger(methodName, options, source); | |
} | |
// Will return null if no method logger was needed - which will | |
// effectively be ignored by a using statement. | |
return logger; | |
} | |
/// <summary> | |
/// Tidy up | |
/// </summary> | |
public void Dispose() | |
{ | |
this.stopwatch.Stop(); | |
if ((this.options & LogOptions.ExecutionTime) == LogOptions.ExecutionTime) | |
{ | |
this.source.InfoFormat("### Method {0} execution time {1}ms", this.methodName, this.stopwatch.ElapsedMilliseconds); | |
} | |
// report exception if any got hitted hitted | |
int exceptionCode = Marshal.GetExceptionCode(); | |
if (exceptionCode != 0) | |
{ | |
this.source.InfoFormat("### Method {0} exit with exception hit, code:{1}, exception:{2}", this.methodName, exceptionCode, Marshal.GetExceptionForHR(exceptionCode)); | |
} | |
if ((this.options & LogOptions.Exit) == LogOptions.Exit) | |
{ | |
this.source.InfoFormat("### Exiting method {0}...", this.methodName); | |
} | |
} | |
/// <summary> | |
/// Customize whether the specified log is verbose here | |
/// </summary> | |
/// <param name="log">The log.</param> | |
/// <returns> | |
/// <c>true</c> if the specified log is verbose; otherwise, <c>false</c>. | |
/// </returns> | |
private static bool IsVerbose(ILog log) | |
{ | |
return (log.IsInfoEnabled); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------------------------------------------------------------------------------- | |
// <summary> | |
// @Description - ElementLocators class contains functions for finding elements on a page. | |
// @Author - Vaibhav | |
// @Date - 01 / 03 / 2012 | |
// </summary> | |
// -------------------------------------------------------------------------------------------------------------------- | |
namespace BVT.TestCases.Utilities | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Xml.Linq; | |
using OpenQA.Selenium; | |
using System.Windows.Automation; | |
using OpenQA.Selenium.Support.UI; | |
/// <summary> | |
/// @Description - ElementLocators class contains functions for finding elements on a page. | |
/// @Author - Vaibhav | |
/// @Date - 01 / 03 / 2012 | |
/// </summary> | |
public class ElementLocators | |
{ | |
#region Static Fields | |
/// <summary> | |
/// The implicit wait time secs. | |
/// </summary> | |
private static int implicitWaitTimeSecs = 0; | |
/// <summary> | |
/// The page load time out. | |
/// </summary> | |
private static int pageLoadTimeOut = 0; | |
/// <summary> | |
/// The web element. | |
/// </summary> | |
private static IWebElement webElement = null; | |
#endregion | |
#region Constructors and Destructors | |
/// <summary> | |
/// Initializes static members of the <see cref="ElementLocators"/> class. | |
/// @Description - Static constructor for initializing timeout | |
/// variables | |
/// </summary> | |
static ElementLocators() | |
{ | |
implicitWaitTimeSecs = Convert.ToInt32(DataLocators.configData["timeout"]); | |
pageLoadTimeOut = Convert.ToInt32(DataLocators.configData["timeout"]); | |
// Logger.Log.InfoFormat("ElementLocators 1: " + implicitWaitTimeSecs); | |
// Logger.Log.InfoFormat("ElementLocators 2: " + pageLoadTimeOut); | |
} | |
#endregion | |
#region Public Methods and Operators | |
/// <summary> | |
/// @Description - FindElementSmart function finds element on a page, returns | |
/// the object of IWebElement | |
/// </summary> | |
/// <param name="webDriver"> | |
/// Object of IWebDriver | |
/// </param> | |
/// <param name="message"> | |
/// Error message for the element | |
/// </param> | |
/// <param name="fileName"> | |
/// Name of the xml file | |
/// </param> | |
/// <param name="givenName"> | |
/// Given name in the xml file | |
/// </param> | |
/// <returns> | |
/// IWebElement object | |
/// </returns> | |
public static IWebElement FindElementSmart( | |
IWebDriver webDriver, string message, string fileName, string givenName) | |
{ | |
string attributeValue = string.Empty; | |
string attributeName = SymformEnumerations.AttributeNames.Xpath.ToString(); | |
Logger.Log.InfoFormat("Finding element: " + givenName); | |
for (int attributeCounter = 0; attributeCounter < 8; attributeCounter++) | |
{ | |
switch (attributeCounter) | |
{ | |
case 0: | |
attributeName = SymformEnumerations.AttributeNames.Xpath.ToString(); | |
break; | |
case 1: | |
attributeName = SymformEnumerations.AttributeNames.Xpath1.ToString(); | |
break; | |
case 2: | |
attributeName = SymformEnumerations.AttributeNames.Xpath2.ToString(); | |
break; | |
case 3: | |
attributeName = SymformEnumerations.AttributeNames.Id.ToString(); | |
break; | |
case 4: | |
attributeName = SymformEnumerations.AttributeNames.Name.ToString(); | |
break; | |
case 5: | |
attributeName = SymformEnumerations.AttributeNames.ClassName.ToString(); | |
break; | |
case 6: | |
attributeName = SymformEnumerations.AttributeNames.LinkText.ToString(); | |
break; | |
case 7: | |
attributeName = SymformEnumerations.AttributeNames.PartialLinkText.ToString(); | |
break; | |
} | |
try | |
{ | |
attributeValue = DataLocators.GetAttributeValue(fileName, givenName, attributeName); | |
if (attributeValue.Equals("null")) | |
{ | |
continue; | |
} | |
else | |
{ | |
WebDriverWait wait = new WebDriverWait( | |
webDriver, TimeSpan.FromSeconds(Convert.ToInt32(DataLocators.configData["timeout"]))); | |
webElement = wait.Until<IWebElement>( | |
(d) => | |
{ | |
if (attributeName.Equals(SymformEnumerations.AttributeNames.Xpath.ToString())) | |
{ | |
return (IWebElement)d.FindElement(By.XPath(attributeValue)); | |
} | |
else if (attributeName.Equals(SymformEnumerations.AttributeNames.Id.ToString())) | |
{ | |
return (IWebElement)d.FindElement(By.Id(attributeValue)); | |
} | |
else if (attributeName.Equals(SymformEnumerations.AttributeNames.Name.ToString())) | |
{ | |
return (IWebElement)d.FindElement(By.Name(attributeValue)); | |
} | |
else if (attributeName.Equals( | |
SymformEnumerations.AttributeNames.ClassName.ToString())) | |
{ | |
return (IWebElement)d.FindElement(By.ClassName(attributeValue)); | |
} | |
else if ( | |
attributeName.Equals( | |
SymformEnumerations.AttributeNames.LinkText.ToString())) | |
{ | |
return (IWebElement)d.FindElement(By.LinkText(attributeValue)); | |
} | |
else | |
{ | |
return (IWebElement)d.FindElement(By.PartialLinkText(attributeValue)); | |
} | |
}); | |
if (webElement != null) | |
{ | |
int time = Convert.ToInt32(DataLocators.configData["timeout"]); | |
int attemp = 0; | |
do | |
{ | |
attemp++; | |
} | |
while (!webElement.Displayed && attemp < time); | |
} | |
break; | |
} | |
} | |
catch (NoSuchElementException) | |
{ | |
Logger.Log.InfoFormat("NoSuchElement Exception"); | |
continue; | |
} | |
catch (StaleElementReferenceException) | |
{ | |
attributeCounter = 0; | |
Logger.Log.InfoFormat("StaleElementReference Exception"); | |
continue; | |
} | |
catch (WebDriverException) | |
{ | |
Logger.Log.InfoFormat("WebDriver Exception"); | |
continue; | |
} | |
catch (TimeoutException te) | |
{ | |
Logger.Log.InfoFormat(message + " with attribute " + attributeName + " not found..." + te.Message); | |
} | |
} | |
return webElement; | |
} | |
/// <summary> | |
/// @Description - FindElementSmart function finds element on a page for Dashboard, returns | |
/// the object of IWebElement | |
/// </summary> | |
/// <param name="webdriver">The webdriver.</param> | |
/// <param name="message">Error message for the element</param> | |
/// <param name="filename">The filename.</param> | |
/// <param name="givenname">The givenname.</param> | |
/// <param name="timeOut">Optional timeout parameter, in seconds, if not specified, the timeout will be taken from config</param> | |
/// <returns>IWebElement object</returns> | |
/// <exception cref="System.Exception">ThreadStateException caught while finding element. + threadStateException.Message</exception> | |
/// <exception cref="OpenQA.Selenium.NoSuchElementException"> | |
/// Couldn't find the element givenname = ' + givenname + ' with any of the provided properties on page ' + filename + '. | |
/// </exception> | |
public static IWebElement FindElementSmartOnDashboard( | |
IWebDriver webdriver, string message, string filename, string givenname, int timeOut = 0) | |
{ | |
Dictionary<string, string> elementdetails = null; | |
int timoutDurationMS = timeOut == 0 ? Convert.ToInt32(DataLocators.configData["timeout"]) * 1000 : timeOut * 1000; | |
elementdetails = LoadNodeDetails(filename, givenname); | |
IWebElement element = null; | |
foreach (KeyValuePair<string, string> keyvalue in elementdetails) | |
{ | |
string propertyName = keyvalue.Key.TrimEnd('1', '2', '3', '4'); | |
element = null; | |
try | |
{ | |
int counter = 0; | |
do | |
{ | |
try | |
{ | |
counter += 1; | |
Thread.Sleep(500); | |
switch (propertyName) | |
{ | |
case "Xpath": | |
element = (IWebElement)webdriver.FindElement(By.XPath(keyvalue.Value)); | |
break; | |
case "Id": | |
element = (IWebElement)webdriver.FindElement(By.Id(keyvalue.Value)); | |
break; | |
case "Name": | |
element = (IWebElement)webdriver.FindElement(By.Name(keyvalue.Value)); | |
break; | |
case "ClassName": | |
element = (IWebElement)webdriver.FindElement(By.ClassName(keyvalue.Value)); | |
break; | |
case "LinkText": | |
element = (IWebElement)webdriver.FindElement(By.LinkText(keyvalue.Value)); | |
break; | |
case "PartialLinkText": | |
element = (IWebElement)webdriver.FindElement(By.PartialLinkText(keyvalue.Value)); | |
break; | |
} | |
} | |
catch (NoSuchElementException) | |
{ | |
Logger.Log.WarnFormat( | |
"Try " + counter | |
+ ": NoSuchElementException caught while finding element with givenname = " | |
+ givenname + " using parameter = \"" + keyvalue.Key + "\" with value = \"" | |
+ keyvalue.Value + "\""); | |
} | |
catch (ThreadStateException threadStateException) | |
{ | |
throw new Exception( | |
"ThreadStateException caught while finding element. " + threadStateException.Message); | |
} | |
catch (Exception uncatogarizedException) | |
{ | |
Logger.Log.InfoFormat( | |
"Exception caught while finding element with givenname = " + givenname | |
+ " using parameter = \"" + keyvalue.Key + "\" with value = \"" + keyvalue.Value | |
+ "\". Innerexception --> " + uncatogarizedException.Message); | |
break; | |
} | |
} | |
while (element == null && counter < timoutDurationMS / 500); | |
if (element != null) | |
{ | |
int time = Convert.ToInt32(DataLocators.configData["timeout"]); | |
int attemp = 0; | |
do | |
{ | |
attemp++; | |
} | |
while (!element.Displayed && attemp < time); | |
break; | |
} | |
} | |
catch (StaleElementReferenceException) | |
{ | |
Logger.Log.InfoFormat("StaleElementReference Exception"); | |
} | |
catch (TimeoutException) | |
{ | |
Logger.Log.InfoFormat( | |
"Operation timed out while finding element givenname = " + givenname | |
+ " using parameter = \"" + keyvalue.Key + "\" with value = \"" + keyvalue.Value | |
+ "\" from file '" + filename + "'"); | |
} | |
} | |
return element; | |
} | |
/// <summary> | |
/// @Description - WaitForPageToLoad function waits for a particular page | |
/// to load. | |
/// </summary> | |
/// <param name="webDriver"> | |
/// Object of IWebDriver | |
/// </param> | |
/// <param name="pageTitle"> | |
/// Title of the page to be loaded | |
/// </param> | |
public static void WaitForPageToLoad(IWebDriver webDriver, string pageTitle) | |
{ | |
int catchCount = 0; | |
string title = string.Empty; | |
do | |
{ | |
try | |
{ | |
title = webDriver.Title.ToString(); | |
Thread.Sleep(1000); | |
++catchCount; | |
} | |
catch (WebDriverException) | |
{ | |
continue; | |
} | |
} | |
while (!title.Equals(pageTitle) && catchCount < pageLoadTimeOut); | |
} | |
/// <summary> | |
/// Gets the inner custom box control from node software | |
/// </summary> | |
/// <param name="deviceSoftware">Automation Element</param> | |
/// <returns>Object of Automation Element</returns> | |
public static AutomationElement GetInnerCustomBox(AutomationElement deviceSoftware) | |
{ | |
AutomationElement innerCustomBox = null; | |
AutomationElement customBoxes = null; | |
int wait = 0; | |
do | |
{ | |
++wait; | |
Thread.Sleep(1000); | |
customBoxes = deviceSoftware.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)); | |
AutomationElement pane = customBoxes.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane)); | |
innerCustomBox = pane.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)); | |
} | |
while (innerCustomBox == null && wait < 10); | |
return innerCustomBox; | |
} | |
/// <summary> | |
/// @Description - HandleBrowserAlert function handles browser Alert while moving from one page to another. | |
/// </summary> | |
public static void HandleBrowserAlert() | |
{ | |
AutomationElement browser = null; | |
AutomationElement modaldialog = null; | |
int wait = 0; | |
do | |
{ | |
++wait; | |
Thread.Sleep(1000); | |
browser = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "IEFrame")); | |
} | |
while (browser == null && wait < 15); | |
wait = 0; | |
if (browser != null) | |
{ | |
do | |
{ | |
wait++; | |
Thread.Sleep(1000); | |
// verify alert window | |
modaldialog = browser.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "Dialog")); | |
} | |
while (modaldialog == null && wait < 5); | |
if (modaldialog != null) | |
{ | |
// Verify button on alert box | |
AutomationElement button = modaldialog.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "OK")); | |
InvokePattern leavepage = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern); | |
leavepage.Invoke(); | |
} | |
} | |
else | |
{ | |
// write to Log | |
Logger.Log.InfoFormat("alert window not present"); | |
} | |
} | |
/// <summary> | |
/// @Description - VerifyWebUIAlerts function verifies alert message on WebUI platforms. | |
/// </summary> | |
/// <param name="message">Message that needs to verify on popup</param> | |
public static void VerifyWebUIAlerts(string message) | |
{ | |
AutomationElement desktop = AutomationElement.RootElement; | |
AutomationElement browser = null; | |
AutomationElement modaldialog = null; | |
AutomationElementCollection button = null; | |
int wait = 0; | |
do | |
{ | |
browser = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "IEFrame")); | |
++wait; | |
} | |
while (browser == null && wait < 60); | |
wait = 0; | |
if (browser != null) | |
{ | |
wait = 0; | |
do | |
{ | |
// verify alert window | |
modaldialog = browser.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "Dialog")); | |
wait++; | |
Thread.Sleep(1000); | |
} | |
while (modaldialog == null && wait < 5); | |
if (null != modaldialog) | |
{ | |
AutomationElement warningMessage = modaldialog.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, message)); | |
if (warningMessage != null) | |
{ | |
// Verify button on alert box | |
button = modaldialog.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Button")); | |
AutomationElement buttonclick = button[0]; | |
InvokePattern leavepage = (InvokePattern)buttonclick.GetCurrentPattern(InvokePattern.Pattern); | |
// Click on Ok button | |
leavepage.Invoke(); | |
Logger.Log.InfoFormat("Warning alert verified"); | |
} | |
else | |
{ | |
// write to Log | |
Logger.Log.ErrorFormat("Warning Alert shows wrong message"); | |
throw new Exception("Warning Alert shows wrong message"); | |
} | |
} | |
} | |
else | |
{ | |
// write to Log | |
Logger.Log.InfoFormat("alert window not present"); | |
} | |
} | |
#endregion | |
#region Methods | |
/// <summary> | |
/// The load node details. | |
/// </summary> | |
/// <param name="filename">The filename.</param> | |
/// <param name="givenname">The givenname.</param> | |
/// <returns> | |
/// The <see cref="Dictionary" />. | |
/// </returns> | |
/// <exception cref="System.Exception">No entry found in the object repository for the element with givenname = + givenname | |
/// + in filename = + filename</exception> | |
/// <exception cref="Exception"></exception> | |
internal static Dictionary<string, string> LoadNodeDetails(string filename, string givenname) | |
{ | |
Dictionary<string, string> elementDetails = new Dictionary<string, string>(); | |
try | |
{ | |
filename = Environment.CurrentDirectory + "\\ObjectRepository\\" + filename; | |
XElement rootElement = XElement.Load(filename); | |
IEnumerable<XElement> elementNode = null; | |
elementNode = from elements in rootElement.Elements("Element") | |
where elements.Element("GivenName").Value == givenname | |
select elements; | |
if (elementNode != null) | |
{ | |
XElement element_node = null; | |
foreach (XElement element in elementNode) | |
{ | |
element_node = element; | |
} | |
foreach (XElement element in element_node.Elements()) | |
{ | |
if (element.Name.LocalName != "GivenName" && element.Value != "null") | |
{ | |
elementDetails.Add(element.Name.LocalName, element.Value); | |
} | |
} | |
} | |
else | |
{ | |
throw new Exception( | |
"No entry found in the object repository for the element with givenname = " + givenname | |
+ " in filename = " + filename); | |
} | |
} | |
catch (Exception exception) | |
{ | |
throw new Exception( | |
"Exception while loading the element details. Innerexception -->" + exception.Message); | |
} | |
return elementDetails; | |
} | |
#endregion | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<Elements> | |
<Element> | |
<GivenName>paneltitle</GivenName> | |
<Xpath>//span[contains(@id,'addLocalFolder')]</Xpath> | |
<Id>ui-dialog-title-addLocalFolder</Id> | |
<Name>null</Name> | |
<ClassName>null</ClassName> | |
<LinkText>null</LinkText> | |
<PartialLinkText>null</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>closebutton</GivenName> | |
<Xpath>//body[@id='folder']/div[8]/div/a</Xpath> | |
<Id>null</Id> | |
<Name>null</Name> | |
<ClassName>null</ClassName> | |
<LinkText>null</LinkText> | |
<PartialLinkText>null</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>localfolder</GivenName> | |
<Xpath>//input[contains(@class,'localFolder')]</Xpath> | |
<Id>null</Id> | |
<Name>null</Name> | |
<ClassName>localFolder</ClassName> | |
<LinkText>null</LinkText> | |
<PartialLinkText>null</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>cloudfolder</GivenName> | |
<Xpath>//input[contains(@class,'cloudFolder')]</Xpath> | |
<Id>null</Id> | |
<Name>null</Name> | |
<ClassName>cloudFolder</ClassName> | |
<LinkText>null</LinkText> | |
<PartialLinkText>null</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>browsebutton</GivenName> | |
<Xpath>//button[contains(@id,'browseForLocalFolder')]</Xpath> | |
<Id>browseForLocalFolder</Id> | |
<Name>null</Name> | |
<ClassName>null</ClassName> | |
<LinkText>Browse</LinkText> | |
<PartialLinkText>Browse</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>okbutton</GivenName> | |
<Xpath>//span[text()='Ok']</Xpath> | |
<Id>null</Id> | |
<Name>null</Name> | |
<ClassName>null</ClassName> | |
<LinkText>Ok</LinkText> | |
<PartialLinkText>Ok</PartialLinkText> | |
</Element> | |
<Element> | |
<GivenName>cancelbutton</GivenName> | |
<Xpath>//span[text()='Cancel']</Xpath> | |
<Id>null</Id> | |
<Name>null</Name> | |
<ClassName>null</ClassName> | |
<LinkText>Cancel</LinkText> | |
<PartialLinkText>Cancel</PartialLinkText> | |
</Element> | |
</Elements> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment