0

I am automating a test for a page that contains a URL that needs to be then tested.

I created a method that I believed was giving me the http status code:

public string ContentUrlHttpRequest()
        {
            HttpWebRequest protocolWebRequest = (HttpWebRequest)WebRequest.Create(ContentUrl());
            protocolWebRequest.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)protocolWebRequest.GetResponse();
            return response.Headers.ToString();
        }

ContentUrl() is another method i created to find the element on the page with the url to be tested and gets it's value.

I have also tried return response.StatusCode.ToString(); but the response i received was "OK".

I know that the response from that url needs to be = 200. I have this assertion that compares the response from the ContentUrlHttpRequest() to the expected results (200):

Assert.AreEqual("200", ContentUrlHttpRequest(), "The Url is not live. Http response = " + ContentUrlHttpRequest());

The response i am getting from ContentUrlHttpRequest() is not the status code but:"Date: Mon, 03 May 2021 09:07:13 GMT".

I understand why it is happening, it is getting the header of the page that is searching. But how could I get the status code? Is it possible with Selenium? Is there something wrong with my method and instead of Headers I need to use something different?

Unfortunately i am not able to provide with the urls that i am testing, or the platform with the url as they are confidential. Hopefully my issue is clear and you guys can give me some guidance.

DjNewma
  • 65
  • 7
  • 1
    Perhaps you could use `(int)response.StatusCode` as mentioned here: https://stackoverflow.com/questions/1330856/getting-http-status-code-number-200-301-404-etc-from-httpwebrequest-and-ht – DesertPride May 03 '21 at 09:30
  • Maybe response.getStatusCode ? – nuzooo May 03 '21 at 09:47

2 Answers2

2

You are not returning the response status code. You are returning the headers.

You should replace the return statement with this:

return ((int)response.StatusCode).ToString();
Rami Assi
  • 910
  • 2
  • 10
  • 19
  • The response i get from StatusCode is "OK". I forgot to mention on my post i tried such as well. I will edit the post now. – DjNewma May 03 '21 at 09:36
  • Perfect, this works for me. I am quite new to c#, could you tell me what the (int) does before the command? Or could you guide me on where i can read about it and other variants? – DjNewma May 03 '21 at 09:44
  • 1
    the `(int)` casts the enum value of status code to the int type. in other words, it converts it to int. then I take the whole result and convert it to string because this is the return type of the ContentUrlHttpRequest method. for more information: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions – Rami Assi May 03 '21 at 09:50
0

I guess you should use response.Status.ToString(); instead of response.Headers.ToString();
But the status contains not only the number like 200 or 401 but also text.
So if you are going to use response.Status.ToString(); you should Assert.True(ContentUrlHttpRequest().contains("200"))
Or you can use response.StatusCode.ToString(); this will give you the status number itself String without additional texts.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Also tried that before, sorry i did not reference it on my post. Using the status i receive "OK" instead of the status code. Unfortunately was requested to me to get the status code specifically so it can be debugged later in case the test fails – DjNewma May 03 '21 at 09:33
  • Are you sure the status / status code is `OK` only without the number? maybe it is not 200 but something else like 2XX or 3XX? – Prophet May 03 '21 at 09:39
  • So i did not notice that you said .Status before. I just assumed it was .StatusCode . The response with StatusCode is "OK". – DjNewma May 03 '21 at 09:39
  • Very sure the status code is "OK" and nothing else – DjNewma May 03 '21 at 09:39