I'm using selenium to run tests I have created. I now want to start error handling. I want to be able to get the element that could not be found and say something like "Element start could not be found"
in my error log. If this can't be done I would like to print the whole line of code where the exception happened.
I'm not sure how to do this. At the moment I just wrap each test in a try catch and say that Couldn't find element
however this is very broad and doesn't narrow down which element it couldn't find.
My session setUp
private string excelId = CommonMethods.ExcelOfficeVersion();
private const string AppDriverUrl = "http://127.0.0.1:4723";
private static WindowsDriver<WindowsElement> excelSession;
System.Web.Caching.Cache cache = new System.Web.Caching.Cache();
xl.Workbook WB;
public static bool skipTearDown = false;
WindowsElement create;
WindowsElement blankWorkBook;
public static DesiredCapabilities appCapabilities = new DesiredCapabilities();
[TestInitialize]
public void SetUp()
{
try
{
appCapabilities.SetCapability("app", excelId);
var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);
var capabilities = new DesiredCapabilities();
capabilities.SetCapability("app", "Root");
excelSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
CommonMethods.keyCheck(excelSession);
//blankWorkBook = excelSession.FindElementByName("Blank workbook");
//cache.Insert("Create", create);
CommonMethods.checkCreateExcel();
}
catch (Exception)
{
CommonMethods.ExceptionHandler("WinApp Driver failed to load Excel", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
}
Here is an example of my code
try
{
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Blank workbook").Click();
excelSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
excelSession.FindElementByName("Create").Click();
skipTearDown = true;
}
catch (Exception)
{
CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
Another typical test
public void newExcelWorkbook()
{
try
{
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Blank workbook").Click();
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Create").Click();
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("New").Click();
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the New button");
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("E Sample Data").Click();
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the E Sample Data button");
Thread.Sleep(4000);
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
}
catch (Exception)
{
CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
TearDown();
}