Failing a scripted test using ActiveBrowser.ContainsText()

4 posts, 0 answers
  1. eugene
    eugene avatar
    13 posts
    Member since:
    Jul 2012

    Posted 18 Sep 2014 Link to this post

    I know this question has been asked and answered, but either I'm not understanding how to address my specific situation, or I am asking for something slightly different.

    I have a scripted test that looks for specific text on screen, and if it does find that text, the test will Log.WriteLine and continue on with the test. What I would also like to happen, is for the test to be marked with a rex X as a fail, but still continue on with the test.

    If I could please get some assistance, that would be great.

    foreach (HtmlAnchor Reports in creativeReportsAnchors)
    {
        try
        {
            Log.WriteLine(Reports.HRef);
     
            Reports.Click();
     
            //Refresh DOM tree
            System.Threading.Thread.Sleep(7000);
            ActiveBrowser.RefreshDomTree();
     
            if (ActiveBrowser.ContainsText(productionError) == true)
            {
                Log.WriteLine("ERROR: ");
            }
            else if (ActiveBrowser.ContainsText(stagingError) == true)
            {
                Log.WriteLine("ERROR: ");
            }
     
            System.Threading.Thread.Sleep(2000);
        }
        catch (Exception)
        {
     
        }
        finally
        {
            ActiveBrowser.NavigateTo(CurrentSiteUrl + "reports-creative.aspx");
        }
    }
  2. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 23 Sep 2014 Link to this post

    Hi Eugene,

    I see your code is doing more than just looking for specific text. It's also iterating through a list of links, clicking on those link then looking for the text. Let's modify the code to fail the test if the text is found, after it's done iterating through all the links:

    bool textFound = false;
    foreach (HtmlAnchor Reports in creativeReportsAnchors)
    {
        try
        {
            Log.WriteLine(Reports.HRef);
     
            Reports.Click();
     
            //Refresh DOM tree
            System.Threading.Thread.Sleep(7000);
            ActiveBrowser.RefreshDomTree();
     
            if (ActiveBrowser.ContainsText(productionError))
            {
                Log.WriteLine("ERROR: ");
                textFound = true;
            }
            else if (ActiveBrowser.ContainsText(stagingError))
            {
                Log.WriteLine("ERROR: ");
                textFound = true;
            }
     
            System.Threading.Thread.Sleep(2000);
        }
        catch (Exception)
        {
     
        }
        finally
        {
            ActiveBrowser.NavigateTo(CurrentSiteUrl + "reports-creative.aspx");
        }
    }
    if (textFound)
    {
        throw new ApplicationException("The error text was found");
    }

    The above code will iterate through all the links, clicking on them and look for the error text. If found it sets a flag. When done iterating the flag is tested and if it was set a new ApplicationException is throw. This will cause the test to fail and it will immediately stop at that point.

    Do you need the test to continue any further?

    Regards,
    Cody
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
  3. eugene
    eugene avatar
    13 posts
    Member since:
    Jul 2012

    Posted 25 Sep 2014 in reply to Cody Link to this post

    Well that was easy!!

    I had tried something similar, but instead of adding the ApplicationException outside of the try, I was adding inside the catch.

    Thank you Cody.
  4. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 25 Sep 2014 Link to this post

    Hi Eugene,

    I am glad that works for you! Thank you for the update.

    Regards,
    Cody
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
Back to Top