This is a migrated thread and some comments may be shown as answers.

Failing a scripted test using ActiveBrowser.ContainsText()

3 Answers 75 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
eugene
Top achievements
Rank 1
eugene asked on 18 Sep 2014, 05:42 PM
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");
    }
}

3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 23 Sep 2014, 03:58 PM
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!
0
eugene
Top achievements
Rank 1
answered on 25 Sep 2014, 08:07 PM
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.
0
Cody
Telerik team
answered on 25 Sep 2014, 10:46 PM
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!
Tags
General Discussions
Asked by
eugene
Top achievements
Rank 1
Answers by
Cody
Telerik team
eugene
Top achievements
Rank 1
Share this question
or