DCSIMG
How can I tell a Web Test to ignore 404 errors? - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

How can I tell a Web Test to ignore 404 errors?

How can I tell a Web Test to ignore 404 errors?

Some times your web test failed because a missing image or old file that not longer exists in the IIS.

Why the Web Test fail all the test because one missing Image?  Your absolutely Right!

I'll show 3 different ways to solve this.

1. Expected HTTP Status Code

The First thing you can do is to set "Expected HTTP Status Code" to 404.

But this will not work if the bad request is a dependent request.

Expected HTTP Status Code

2. Add Dependent Request

You can add this request manually and then set the "Expected HTTP Status Code" to 404.

Add Dependent Request

3. Write WebTestPlugin

But if those not working because different requests and other parameters you can use WebTestPlugin

In the sample bellow the Plugin searches for the right dependent and remove it from the WebTest,
like this you can run a web Test with 404 request and still make the WebTest to Pass

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

using Microsoft.VisualStudio.TestTools.WebTesting;

using System.ComponentModel;

 

namespace TestProject

{

    [DisplayName("Dependent Request Plugin")]

    public class DependentPlugin : WebTestPlugin

    {

        string dependent = "badimage.gif";

 

        public override void PostRequest(object sender, PostRequestEventArgs e)

        {

            WebTestRequestCollection DependentToRemove = new WebTestRequestCollection();

 

            foreach (WebTestRequest req in e.Request.DependentRequests)

            {

                if (!string.IsNullOrEmpty(dependent) && r.Url.EndsWith(dependent))

                {

                    DependentToRemove.Add(req);

                }

            }

 

            foreach (WebTestRequest dependent in DependentToRemove)

            {

                e.Request.DependentRequests.Remove(dependent);

            }

        }

    }

}

Comments

Team System News said:

Ognjen Bajic on Oct 08 TFS Power Tools released Steve Porter on Latest Release of Team Foundation Server...

# November 13, 2008 9:50 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 28, 2008 3:32 PM

Ashok Reddy said:

How do I make web test tool wait till data gets loaded in the web page? I tried by setting the Think Time but it is not working. Could you please advice me on this issue

# December 11, 2008 4:31 AM

Shai Raiten said:

Hi,

What is precisely the problem?

When you run web test the requests are fired up only after the request before is finished loading.

# December 11, 2008 7:24 AM

Lohmar ASHAR said:

Thanks a lot, I've found on this page exactly what I've needed. You've been more helpful than the Microsoft Support.

# April 15, 2009 5:44 PM

MIck_at_Oz said:

Dependent requests that tries to hit a dynamically generated JS/CSS file could not be found. I dont want the system to just ignore these, I want the test to simulate the "real world". Any ideas ?

# July 17, 2009 2:58 AM

Shahid said:

This is great - really helped me!

One error though in your code:

if (!string.IsNullOrEmpty(dependent) && r.Url.EndsWith(dependent))

Should read:

if (!string.IsNullOrEmpty(dependent) && req.Url.EndsWith(dependent))

Not a C# programmer so a bit hard to work out for me.

# August 7, 2009 6:53 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: