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

POST json in TS

3 Answers 57 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 02 Nov 2013, 02:51 AM
Hey guys

I'm  curious if there is a way to make a post with JSON data attached to it in test studio? This would make it easy for me to departmentalize some of my testing and feature sets. 

Pretty basic:

POST: http://someUrl.com
JSON: {"objectID":123}

Thanks Jon!

3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 04 Nov 2013, 06:59 PM
Hello Jon,

Test Studio does not have a built-in feature to do a POST with any random JSON data. Test Studio was designed to drive the browser, let it do all the GET's and POST's and the validate what is loaded in the browsers DOM contains the expected results.

You could write code in a coded step to do this. Normally this would be beyond our ability to help (other than pointing you in the right direction) but I happened to work on a project recently that required this. Here's the code I used in that project:

/// <summary>
/// Sends a JSON object to a JIRA server using an HTTP POST
/// and gets the JSON response from the server.
/// </summary>
/// <param name="server"></param>
/// <param name="path">The REST path send the request to e.g. /rest/api/2/project</param>
/// <param name="username">The user ID required for authentication. null or empty will use anonymous access.</param>
/// <param name="password">The password required for authentication. null or empty will use anonymous access.</param>
/// <param name="json_request">The JSON object to include in the POST</param>
/// <returns></returns>
internal static JToken PostJson(string server, string path, string username, Base64String password, JToken json_request, out string error)
{
    JToken result;
 
    error = null;
 
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(server + path);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.ProtocolVersion = HttpVersion.Version11;
    httpWebRequest.Method = "POST";
    httpWebRequest.UserAgent = agent;
    httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();
 
    // Add the required authentication header
    if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
    {
        Base64String authInfo = password.prefix(username + ":");
        httpWebRequest.Headers["Authorization"] = "Basic " + authInfo;
    }
 
    if (null != json_request)
    {
        using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(json_request.ToString());
            streamWriter.Flush();
        }
    }
 
    try
    {
        HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (JsonTextReader streamReader = new JsonTextReader(new System.IO.StreamReader(httpResponse.GetResponseStream())))
        {
            result = JToken.ReadFrom(streamReader);
        }
        return result;
    }
    catch (WebException e)
    {
        error = e.Message;
        return null;
    }
}

Regards,
Cody
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Jon
Top achievements
Rank 1
answered on 05 Nov 2013, 03:00 PM
Hi Cody

Thanks for the code! I had it working within a few minutes. =D

So in attempts to better compartmentalize our tests, I'm trying to use this post to skip a page on UI testing. Taking from your response, I assume I may be out of luck... I'm going to give you my scenario, to make sure I'm on the same page.

I have a page that contains a AJAX requested list of items, essentially each list posts to redirect the browser with an item ID. This ID signifies what you clicked on. So if I have a list of people, Adam, Bob, Alice, and I click on Alice with ID 3, it will POST with that ID to the manageuser page. What I'm trying to do is manually POST so that I don't have to go through the process of waiting for the page to load, waiting for the AJAX, looking for the object and clicking the object to make the POST and have the browser redirect.

Currently it looks like I won't be able to drive the browser this way.

Anyway thanks again for the response! I appreciate it!
Jon
0
Cody
Telerik team
answered on 05 Nov 2013, 07:52 PM
Hi Jon,

Now that you have explained the full context to me, I can think of another approach, though it's a little more complicated. You can invoke any JavaScript in the browser you can create. This includes JavaScript that will send a POST. We, the Test Studio support team, don't have the programming expertise what that JavaScript code would look like. Hopefully this is something you can figure out on your own then plug the code into our API call. Any response to the POST will be handled by the browser for you (including a redirect).

Regards,
Cody
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Jon
Top achievements
Rank 1
Answers by
Cody
Telerik team
Jon
Top achievements
Rank 1
Share this question
or