WebTest.Outcome & WebTestRequest.Outcome

You can now determine whether a Request or Test passed or failed from within a Coded WebTest or a WebTest Plug-in by using the Outcome property on the WebTest and WebTestRequest objects. This can be used to do some branching based on whether or not a request passes, as well as detect at the end of a test whether it passed or failed and take some action based on the result, here’s an example if the GetRequestEnumerator method from a coded webtest making use of these two properties:

 

public override IEnumerator<WebTestRequest> GetRequestEnumerator()

{

    // Initialize validation rules that apply to all requests in the WebTest

    if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))

    {

    ValidateResponseUrl validationRule1 = new ValidateResponseUrl();

        this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);

    }

    WebTestRequest request1 = new WebTestRequest("https://site/default.aspx");

    request1.ThinkTime = 4;

    request1.RecordedResponseUrl = "https://site/default.aspx";

    yield return request1;

    if (request1.Outcome == Outcome.Fail)

    {

         WebTestRequest request2 = new WebTestRequest("https://site/a.aspx");

         request2.ThinkTime = 3;

         request2.RecordedResponseUrl = "https://site/a.aspx";

         yield return request2;

    }

    else

    {

         WebTestRequest request3 = new WebTestRequest("https://site/b.aspx");

         request3.RecordedResponseUrl = "https://site/b.aspx";

         yield return request3;

    }

    if (this.Outcome == Outcome.Fail)

    {

         //do some logging stuff

         MessageBox.Show("Test Failed, doing some logging now.");

    }

}

If request1 fails then the next request yielded is request2, otherwise request3 is yielded. At the end of the test we check the overall test outcome and throw up a message box in case of failure, normally we might do some kind of additional logging or something at that point.