我们正在为我们的应用程序实现mfa(多因素身份验证),并且我正在尝试编写自动化测试来测试它。 我没有看到任何直接的方式来获取用户登录的Google身份验证器代码。
有没有人试图这样做?
答案 0 :(得分:0)
有一个用于此的程序包...
https://www.npmjs.com/package/totp-generator是一个可以帮助您使用Google身份验证令牌的软件包。请记住,您想在需要时要求这些权利。 Javascript是异步执行的,因此您需要将其包装在Promise中。这是一些示例代码,假定用户界面登录,然后输入令牌。
describe('check the tokens', function()
{
// First test
it('cy.window() - get the global window object', () => { cy.viewport(500, 780)
cy.visit('https://site.domain',)
cy.get('input[name=email]').type('email@server.io')
cy.get('input[name=password]').focus().type('qwerty123')
cy.get('.Button').click()
// Now lets wait on an object that appears on the page
// when ready to input the token
cy.get(<someElement>).then(()=>{
let token = getToken();
console.log('first token: ' + token);
})
})
//Second test
it('cy.window() - get the global window object', () => { cy.viewport(500, 780)
cy.visit('https://site.domain',)
cy.get('input[name=email]').type('email@server.io')
cy.get('input[name=password]').focus().type('qwerty123')
cy.get('.Button').click()
// Now lets wait on an object that appears on the page
// when ready to input the token
cy.get(<someOtherElement>).then(()=>{
let token = getToken();
console.log('second token: '+ token);
});
})
})
function getToken () {
const totp = require('totp-generator');
const token = totp('2CQQGPPYFE7JPJAX');
return token;
}