Preeti D
Preeti D

Reputation: 31

How to work on 2FA when I am trying to log in into an account using Cypress?

I want to log in into an account, but I am receiving 2FA and to confirm the new device I am getting emails in my inbox, and I am not able to login in into account.

Anyone, can you please tell me how to handle this or if I can do something with MailSlurp in Cypress?

I short, I want to open the website, fill in username, pw, and login into the account successfully even after I get 2FA dialog box popping out, where the 2FA confirmation email is getting into my email inbox.

Thanks in advance and I appreciate your help.

Best, Preeti D

Upvotes: 3

Views: 1727

Answers (1)

Evgenii Bazhanov
Evgenii Bazhanov

Reputation: 926

MailSlurp is fine but you can use Twilio as well, here is my working example source code

const accountSid = 'AC793683c4982a14f01714321bd3f90ca7';
const authToken = '819068e54369ac58bb8aad976fa517bc';
const githubEmail = 'your_github_email'
const githubPassword = 'your_github_password'

describe('Login with github credentials', () => {
    beforeEach(()=>{
        cy.visit('https://github.com/login');
        cy.get('#login_field').type(githubEmail);
        cy.get('#password').type(githubPassword);
        cy.get('input[type="submit"]').click()
    })
    it('Get SMS and apply it in 2FA form', () => {
        cy.request({
            method: 'GET',
            url: `https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`,
            auth: {
                username: accountSid,
                password: authToken,
                AuthMethod: 'BasicAuth',
            }
        })
            .its('body').then((res) => {
            cy.wait(1500) //wait for SMS
            const otpcode = res.messages[0].body.substring(0, 6)
            cy.get('#otp').type(otpcode);
            cy.url().should('eq', 'https://github.com/');
        })
    });
});

Upvotes: 5

Related Questions