0

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();

}
Dillanm
  • 876
  • 12
  • 28
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52

1 Answers1

0

The way you have it currently you could just replace the catch (Exception) with catch (NoSuchElementException) which is a Selenium specific exception thrown when you fail to find an element. However I'd generally advise against wrapping the entire test in a try catch just for the sake of potentially not finding an element.

One thing to note (which you might have missed) is that NoSuchElementException is thrown by FindElement when it fails to find the element and will tell you the selector of the element it failed to find. So in your case if

excelSession.FindElementByName("Blank workbook").Click();

fails to find the element with name Blank workbook it will throw NoSuchElementException with the selector in the exception details.

One more thing to note: when you set the implicit wait duration for the driver it stays for the lifetime of the driver object - you can set it once at the start of your test and not touch it again. If you need to wait for varying periods throughout your test you can use explicit waits via the WebDriverWait class.

Dillanm
  • 876
  • 12
  • 28
  • Thanks for your response. I'm searching by the element name though so it doesn't give me that exception it just gives me a InvalidOperationException on that line. – Craig Gallagher Jun 27 '17 at 10:29
  • [see this image](https://i.imgur.com/vWjZqxQ.png) Not on my test - have you wrapped the driver object and implemented your own search by name rather than using the selenium one? – Dillanm Jun 27 '17 at 10:35
  • 1
    @CraigGallagher Sorry what I really meant to ask there is what exactly is your `excelSession` object? – Dillanm Jun 27 '17 at 10:46
  • I have added some code to my question to show a typical test of mine so you can see how I'm doing it – Craig Gallagher Jun 27 '17 at 10:50
  • @CraigGallagher Still doesn't really explain what excelSession is - is it a WebDriver object itself or are you wrapping it. I can only assume you've implemented your own find by name as `FindElementByName` isn't a method of any Selenium driver object. Won't be able to help until I find out either what `excelSession` is exactly or you show a definiton for `FindElementByName`. – Dillanm Jun 27 '17 at 10:58
  • My excelsessions is my driversession – Craig Gallagher Jun 27 '17 at 11:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147710/discussion-between-dillanm-and-craig-gallagher). – Dillanm Jun 27 '17 at 11:02
  • I've added some more detail on the excelsession in my question – Craig Gallagher Jun 27 '17 at 11:03
  • This isn't really a Selenium question, the issues you're having are more specific to the WinAppDriver and its' interactions with Excel than anything else. I've unfortunately not used that at all and so won't be much help. Hopefully someone else can help you - sorry. – Dillanm Jun 27 '17 at 11:12
  • Ok that's fine. Thanks for your help anyway :) – Craig Gallagher Jun 27 '17 at 11:15