Grafana k6 with googleapis

Hello there, I am using k6 browser module for testing a web application. I have already created an end-to-end test script that requires to login to the web page using OTP which has to be retrieved from the gmail. We are using Google OAuth2 API to fetch the OTP from mail and have already created script to retrieve the OTP. I am trying to import that script in my k6 load test script to login, but receiving error as

ERRO[0000] GoError: The moduleSpecifier "googleapis" couldn't be recognised as something k6 supports.
        at go.k6.io/k6/js.(*requireImpl).require-fm (native)
        at file:///C:/Users/vignesh.t/Desktop/Test%20script/OAuth/getCode.js:1:0(5)
        at go.k6.io/k6/js.(*requireImpl).require-fm (native)
        at file:///C:/Users/vignesh.t/Desktop/Test%20script/script.js:81:0(23)  hint="script exception"

How can I make the googleapis supportable in my k6 load test script? Is there any way around? It would be helpful if someone can help me with this.

Thanks.

I was able to manually create the googleapis method using k6/http package. I am able to get access token using this

const payload = `client_id=${clientId}&client_secret=${clientSecret}&refresh_token=${refreshToken}&grant_type=refresh_token`;
  
  const tokenResponse = http.post(tokenUrl, payload, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  });
  
  const accessToken = tokenResponse.json().access_token;

I now need to access the mail inbox to fetch a mail

const options = {
      url: 'https://gmail.googleapis.com/gmail/v1/users/test.tests@gmail.com/messages',
      params: {
        q: `from:no-reply@rework.link to:${profilesEmailAddress}`,
      },
      headers: {
        Authorization: `Bearer ${accessToken.access_token}`,
      },
    };
    const response = http.get(options.url, options.params, options.headers)

So I am using the http.get() to access the mail. But this isn’t working, I am getting status code 401. The same logic is working in postman to get a response. Why isn’t it working here?It would be much helpful if someone can solve this problem.

Thanks.

Hi @saivignesh0303,

Ok, so firstly headers is part of params in the get API.

Secondly it might be worth taking a look at this library to help you build the url. The reason i’m suggesting this is that there is no q field in params.

Hope this helps,
Ankur

I was able to get the mail body using the following code where I only pass two parameters inside the http.get() method. As per the documentation we can only pass two parameters inside http.get() method.

const options = {
      url: 'https://gmail.googleapis.com/gmail/v1/users/skillfit.tests@gmail.com/messages',
      params: {
        // q: `from:no-reply@rework.link to:${profilesEmailAddress}`,
      // },
        headers: {
          Authorization: `Bearer ${accessToken.access_token}`,
        },
      }
    };
    const response = await http.get('https://gmail.googleapis.com/gmail/v1/users/skillfit.tests@gmail.com/messages?q=from:no-reply@rework.link%20to:skillfit.tests@gmail.com', options.params);

I inserted the query parameter to the link to search for the specified mail from the user “no-reply@rework.link”

1 Like