Forum Discussion

aa1's avatar
aa1
Contributor
8 months ago

Automation script in ReadyAPI for Authentication

Hi, I am new to Ready API. I am trying to authenticate the user credentials and set it up as the script inside Auth Manager so that we can run the ready API test suites from the pipeline automatically. Manual set up for authentication is working fine and we are able to call the APIs and get results. We use Oauth2.0 authentication. The automation script that I have is not working. I am not sure what I am missing.

This is what I tried referring to this document:  https://support.smartbear.com/readyapi/docs/requests/auth/types/oauth2/automate/index.html

var loginForm = document.querySelectorAll(".ab.cd");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var loginBtnForm = document.querySelector(".c2");
var loginBtn = document.querySelectorAll(".c3.c4.c5.c6.c7");

document.getElementById("username").value = "testusername";
document.getElementById("password").value = "testpassword";

loginBtn.click();

 

 

 

 

  • Humashankar's avatar
    Humashankar
    Champion Level 3

    Hi aa1 

    Uncaught Typeerror:loginBtn.click in not a function

    suggests that the click() function is not available on the loginBtn element. 

    Which means that the loginBtn variable is not referencing the correct element or it's not being selected properly.

    So basically ReadyAPI operates outside of a web browser context and typically uses Groovy scripting rather than JavaScript, directly interacting with DOM elements like you would in a web browser won't work

    Recommending to execute automate OAuth 2.0 authentication in ReadyAPI using Groovy scripting

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!

    Warm regards

     

    • aa1's avatar
      aa1
      Contributor

      Hi Humashankar ,

      Thank you for your reply.

      I looked into Groovy script. Did you mean something like this?

      @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
      import groovyx.net.http.RESTClient

      def clientId = "a1b2"
      def redirectUri = "https://readyapi/callback"
      def authorizationUrl = "https://abc.com/authorize"

      def authUrl = "${authorizationUrl}?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token"

      def accessToken = ""
      def url = new URL(redirectUri)
      def params = url.fragment.split('&')
      params.each { param ->
          def parts = param.split('=')
          if (parts[0] == 'access_token') {
              accessToken = parts[1]
          }
      }

      if (accessToken) {
          def restClient = new RESTClient('')
          restClient.auth.oauth2(accessToken)
          
         
          def response = restClient.get(path: '/resource', contentType: 'application/json')
          
          println "Response: ${response.status} - ${response.data}"
          
      } else {
          println "Failed to retrieve access token"
      }

       

      Best regards,

      aa1

      • Humashankar's avatar
        Humashankar
        Champion Level 3

        Hi aa1 

         

        Your script targetting for OAuth 2.0 access token from an authorization server, then using that token to make authenticated requests to a resource server. It should work / if not try make some adjustments by

        Replace 'your_access_token_here' with the actual access token you have obtained from your OAuth flow