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

Coded steps do not execute async methods

1 Answer 101 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ivan
Top achievements
Rank 1
Ivan asked on 07 Jan 2019, 11:58 AM

Hi! I want to use async methods in Test Studio coded steps, but the coded step skips all async methods and everything after it and returns a successful execution of the step. 

How can I solve this problem?



1 Answer, 1 is accepted

Sort by
0
Nikolai
Telerik team
answered on 10 Jan 2019, 10:54 AM
Hello Ivan,

By design both Test Studio Ultimate and the underlying Telerik testing framework run synchronously. This said, you can still write and run "async" methods but you will have to wait for the the task(s) to complete. For example: 

 
[CodedStep(@"New Coded Step")]
 public void WebTest_CodedStep()
 {
     Task.Run(async() =>
     {
        var messageStatus = (await GetStatusForUri("https://bing.com")).StatusCode;
        this.Log.WriteLine("MessageStatus: " + messageStatus);
     }).Wait();
 }
  
 private async Task<System.Net.Http.HttpResponseMessage> GetStatusForUri(string uri)
 {
      var client = new System.Net.Http.HttpClient();
      return await client.GetAsync(uri);
 }

Here we create a task that will synchronously wait blocking execution until the async one is completed. 

Hope this helps. Please, let me know if you have further questions.

Regards,
Nikolai
Progress Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Ivan
Top achievements
Rank 1
Answers by
Nikolai
Telerik team
Share this question
or