Forum Discussion

MoreK's avatar
MoreK
New Contributor
3 years ago
Solved

Covert some code from javascript to Groovy script

Working code in Groovy

def handler = [(~/^Mac OS.*/) : { "open $it".execute() }, (~/^Windows.*/) : { "cmd /C start $it".execute() },
(~/.*/) : {
//--- Open chrome in desktop machine
def browsers =[ 'firefox'.'chrome' ]
//--- find a browser we know the location of
def browser = browsers.find {
"which $it".execute().waitFor() == 0
}
//--- and run it if one found
if( browser )
"$browser $it".execute()
}
]
def k = handler.find { k, v -> k.matcher( System.properties.'os.name' ).matches() }
k?.value( delegate )
}
"URL".browse()

 

Need to convert javascript to in groovy script

 

document.getElementById("username-field").value = "UserName";
document.getElementById("password-field").value = "Password";
document.getElementById("login_submit").click();

 

console.log(localStorage.getItem("access_token"));

  • MoreK ,

    controlling the browser and the DOM inside via Groovy from ReadyAPI seems to me too difficult.

     

    I would suggest doing the authentication directly within ReadyAPI (using OAuth or other authorization method which is in place), save the token and use it by your test steps. 

     

    Best regards,

    Karel

     

5 Replies

  • KarelHusa's avatar
    KarelHusa
    Champion Level 2

    MoreK ,

    it would help if you describe your case more in detail.

    Do you want to populate a form in the browser from ReadyAPI Groovy script?

     

    Best regards,

    Karel

     

    • MoreK's avatar
      MoreK
      New Contributor

      Hi Karel,

      Yes.

      As in my case I have to take access token from UI, because of that I want to open application with credentials and then that generated token is store in localStorage of application and get that token from DOM.

  • KarelHusa's avatar
    KarelHusa
    Champion Level 2

    MoreK ,

    controlling the browser and the DOM inside via Groovy from ReadyAPI seems to me too difficult.

     

    I would suggest doing the authentication directly within ReadyAPI (using OAuth or other authorization method which is in place), save the token and use it by your test steps. 

     

    Best regards,

    Karel

     

    • MoreK's avatar
      MoreK
      New Contributor

      I have tried but still not use 


      Token is not fetch by using OAuth as that login site is 3 party application which dynamical changes

        

      • KarelHusa's avatar
        KarelHusa
        Champion Level 2

        MoreK ,

        it's harder to do the authorization in ReadyAPI in case of proprietary methods, but you can capture the HTTP requests in your browser (e.g. Ctrl+Shift+I in Chrome) and do the same in ReadyAPI.

         

        For dynamic parts you can use properties. This may need some effort, but for longer term it pays off.

         

        Best regards,

        Karel