google authorize | google oauth token client | Cell 2 | Search

The authorizeSelenium function uses the client object to navigate to a provided auth URL, log in using the loginGoogle function, and interact with various HTML elements on the page to retrieve the value of a textarea element.

Run example

npm run import -- "use selenium to authorize Google access"

use selenium to authorize Google access

var loginGoogle = importer.import("log in google");

function authorizeSelenium(authUrl) {
    return client
        .url(authUrl)
        .then(() => loginGoogle(client))
        .then(() => client.$('#submit_approve_access'))
        .then(el => el.waitForDisplayed(3000))
        .then(() => client.$('#submit_approve_access'))
        .then(el => el.moveTo())
        .then(() => client.$('#submit_approve_access content'))
        .then(el => el.waitForDisplayed(3000))
        .then(() => client.$('#submit_approve_access content'))
        .then(el => el.click())
        .then(() => client.$('textarea'))
        .then(el => el.waitForDisplayed(4000).then(() => el.getValue()))
};

module.exports = authorizeSelenium;

What the code could have been:

const importer = require('./importer');

/**
 * Authorizes Selenium using Google login.
 * 
 * @param {string} authUrl - Authorization URL.
 * @param {object} client - Selenium client.
 * @returns {Promise<string>} - Value of the textarea element.
 */
async function authorizeSelenium(client, authUrl, loginGoogleFunc = importer.import('log in google')) {
    // Navigate to the authorization URL
    await client.url(authUrl);
    
    // Login to Google
    await loginGoogleFunc(client);
    
    // Wait for the submit button to be displayed
    const submitButton = await client.$('#submit_approve_access');
    await submitButton.waitForDisplayed(3000);
    
    // Move to the submit button and click it
    await submitButton.moveTo().click();
    
    // Wait for the textarea to be displayed
    const textarea = await client.$('textarea');
    await textarea.waitForDisplayed(4000);
    
    // Return the value of the textarea
    return await textarea.getValue();
}

module.exports = authorizeSelenium;

Code Breakdown

Importing Modules

var loginGoogle = importer.import('log in google');

authorizeSelenium Function

function authorizeSelenium(authUrl) {
    return client
        // Navigate to the provided auth URL
       .url(authUrl)
        
        // Perform the login operation using the imported function
       .then(() => loginGoogle(client))
        
        // Retrieve the HTML element with id'submit_approve_access'
       .then(() => client.$('#submit_approve_access'))
        
        // Wait for the element to be displayed for 3 seconds
       .then(el => el.waitForDisplayed(3000))
        
        // Retrieve the'submit_approve_access' element again
       .then(() => client.$('#submit_approve_access'))
        
        // Move the cursor to the element
       .then(el => el.moveTo())
        
        // Retrieve the'submit_approve_access content' element
       .then(() => client.$('#submit_approve_access content'))
        
        // Wait for the element to be displayed for 3 seconds
       .then(el => el.waitForDisplayed(3000))
        
        // Retrieve the'submit_approve_access content' element again
       .then(() => client.$('#submit_approve_access content'))
        
        // Click the element
       .then(el => el.click())
        
        // Retrieve the HTML textarea element
       .then(() => client.$('textarea'))
        
        // Wait for the element to be displayed for 4 seconds and return its value
       .then(el => el.waitForDisplayed(4000).then(() => el.getValue()))
}

Exports

module.exports = authorizeSelenium;