Coded steps do not execute async methods

2 posts, 0 answers
  1. Ivan
    Ivan avatar
    1 posts
    Member since:
    Jan 2019

    Posted 07 Jan 2019 Link to this post

    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?



  2. Nikolai
    Admin
    Nikolai avatar
    112 posts

    Posted 10 Jan 2019 Link to this post

    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
     
Back to Top