OAuth Automation Sample (Using Azure Active Directory)
This tutorial shows you how to create an automation script to retrieve tokens automatically using Azure Active Directory as an example.
Important
OAuth 2.0 in Azure requires additional parameters. To specify them in ReadyAPI, you need to configure the parameters in the OAuth 2.0 (Azure) authorization type.
Prerequisites
To create automation, configure the OAuth 2.0 authorization. For more information, see Enabling OAuth 2.0 Authentication with Azure Active Directory.
Preparation
Open any client application in a browser
To start creating automation, analyze the login and consent screens in any browser that provides development tools (for example, Chrome, Safari, Firefox, and so on).
Go through the process of getting an access token using any client application that runs in a suitable browser. Providers may also offer a playground to test an authorization process, so you can use it to explore the login and consent screens without using a client application.
In this tutorial, log in to the Microsoft account:
Open the Microsoft account portal: https://account.microsoft.com/.
Click Sign In.
Open the automation script dialog
You can open the automation editor either in the Auth Manager or in the Auth panel:
1. Login input
At this step, explore the login page and create a script that simulates a login input.
Right-click the login text box and select Inspect (or a similar menu item in your browser).
The browser displays the HTML element associated with the input field. To interact with the element from the script, obtain its
idattribute. Use thegetElementByIdmethod.The JavaScript code that enters the needed login to the input element may look like this:
document.getElementById('i0116').value = 'john.smith@hotmail.com';Emulate clicking the Next button. Right-click the button, select Inspect, and find the
idattribute of the button element - idSIButton9:To click the button, you can use the following JavaScript code:
document.getElementById('idSIButton9').click();If you test these commands in the browser’s console, you get an error. This happens because the form cannot “see” the text you set. Since the Azure login screen uses the knockout.js library, use it to enter the following JavaScript code:
ko.dataFor(document.getElementById('i0116')).usernameTextbox.value('john.smith@hotmail.com');Important
The login or consent screens may use another set of libraries. Investigate the page source to find a suitable solution.
Enter the code in the Page 1 section of the ReadyAPI Automation script panel.

2. Enter password
On the next page, enter a password. Use the same approach to obtain the needed elements and simulate the required actions.
Right-click the password field and select Inspect. Use the
idattribute to find the element.To enter the password, use the same approach you use on the login screen.
ko.dataFor(document.getElementById('i0118')).passwordTextbox.value('p@ssw0rd');Right-click the Next button and select Inspect. The ID is the same as the one on the login screen.
Use the following JavaScript code to click the button.
document.getElementById('idSIButton9').click();Enter the needed code in the Page 2 section of the ReadyAPI Automation script panel:

3. Final adjustments
If you run the script now, it won't be able to obtain an access token. When ReadyAPI runs the script from the Page 1 section, the page is not loaded. Instead, it redirects to the other page, so when the actual page loads, ReadyAPI runs the script in the Page 2 section. Additionally, Azure may ask for the login and password on the same page, so insert both scripts into the Page 2 section. Use the following script to get an access token:
//This function enters the login
function login() {
ko.dataFor(document.getElementById('i0116')).usernameTextbox.value('<your_email>');
document.getElementById('idSIButton9').click();
}
//This function enters the password
function password() {
ko.dataFor(document.getElementById('i0118')).passwordTextbox.value('<your_password>');
document.getElementById('idSIButton9').click();
}
//Runs the login function after 1 second
setTimeout(login, 1000);
//Runs the password function after 3 seconds
setTimeout(password, 3000);In some cases, Azure switches pages. Click to add a third section and insert the second script into the Page 3 section.
![]() |
When you complete the script, click to test the automation:
![]() |
Remarks
Depending on your Azure Active Directory settings, there may be an additional page that asks whether a user wants to remain signed in. Modify your script to click the needed button or disable this setting. How to disable the setting:
ReadyAPI performs authorization in the internal browser and does not store cookies. Each time ReadyAPI runs the automation, the authorization process remains unchanged. However, if the authorization server changes the process on its own end, your script may stop working.
Background redirects may trigger the next script in the list to run even when the page is not, in fact, changed. Add logic to your script to wait for the required elements. For example:
var a = 0; //This function enters the login function login() { ko.dataFor(document.getElementById('i0116')).usernameTextbox.value('<your_email>'); document.getElementById('idSIButton9').click(); } //This function waits for the login field function waitLogin() { if (document.getElementById('i0116')) { setTimeout(login, 10); } else { if (a++ < 5) { setTimeout(waitLogin, 1000); } } }Azure uses the login specified in the Login hint field to pre-fill the login and skip the login screen, so specify only the password. The automation script in this example does not work in this case. Clear the Login hint field or modify the script to check if the login is needed:
//This function enters the login function login() { ko.dataFor(document.getElementById('i0116')).usernameTextbox.value('<your_email>'); document.getElementById('idSIButton9').click(); } //This function enters the password function password() { ko.dataFor(document.getElementById('i0118')).passwordTextbox.value('<your_password>'); document.getElementById('idSIButton9').click(); } // Check if the login field exists if (document.getElementById('i0116')) { setTimeout(login, 1000); } else { setTimeout(password, 1000); }



