如何使用命令。
我正在命令 commands.js下访问我的gmail帐户,然后获取所请求的电子邮件主体并将所需的数据从身体中提取,然后将其保存在const variable code >>现在,我想在我的testfile.cy.js中.Type type tulled的电子邮件数据(代码)。
commands.js
/// <reference types="cypress"
import { parseString } from "xml2js";
Cypress.Commands.add('loginByGoogleApi', () => {
cy.request({
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
body: {
grant_type: 'refresh_token',
client_id: Cypress.env('googleClientId'),
client_secret: Cypress.env('googleClientSecret'),
refresh_token: Cypress.env('googleRefreshToken'),
},
}).then(({ body }) => {
const { access_token, id_token } = body
cy.log('Opening emails including code to verify')
cy.request({
method: 'GET',
url: 'https://mail.google.com/mail/feed/atom/verifyCode',
headers: { Authorization: Bearer ${access_token} },
}).then(({ body }) => {
parseString(body, function (err, results) {
let data = JSON.stringify(results)
let codeTitle = JSON.parse(data).feed.entry[0].title[0];
let code = codeTitle.replace('Chaine confirmation code: ','');
cy.log(code)
});
});
})
})
testfile.cy.js
const { Code } = require("@chaine/keychaine");
describe('Open login page', () => {
it('Enter your email to login', () => {
cy.visit('https://chaineapp.com/staging/login%27)
cy.get('#field-1').click().type('[email protected]');
cy.get('[class*="chakra-button css-yg51i0"]').click();
cy.get('#pin-input-2-0').type(<need to put code here>);
})
it('get code', () => {
cy.loginByGoogleApi()
})
})
I am accessing my Gmail account by command loginByGoogleApi
under commands.js, then getting the requested email body and pulling the required data from the body, and saving it in a const variable code
>> now I want to .type the pulled email data (code) in my testFile.cy.js.
commands.js
/// <reference types="cypress"
import { parseString } from "xml2js";
Cypress.Commands.add('loginByGoogleApi', () => {
cy.request({
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
body: {
grant_type: 'refresh_token',
client_id: Cypress.env('googleClientId'),
client_secret: Cypress.env('googleClientSecret'),
refresh_token: Cypress.env('googleRefreshToken'),
},
}).then(({ body }) => {
const { access_token, id_token } = body
cy.log('Opening emails including code to verify')
cy.request({
method: 'GET',
url: 'https://mail.google.com/mail/feed/atom/verifyCode',
headers: { Authorization: Bearer ${access_token} },
}).then(({ body }) => {
parseString(body, function (err, results) {
let data = JSON.stringify(results)
let codeTitle = JSON.parse(data).feed.entry[0].title[0];
let code = codeTitle.replace('Chaine confirmation code: ','');
cy.log(code)
});
});
})
})
testFile.cy.js
const { Code } = require("@chaine/keychaine");
describe('Open login page', () => {
it('Enter your email to login', () => {
cy.visit('https://chaineapp.com/staging/login%27)
cy.get('#field-1').click().type('[email protected]');
cy.get('[class*="chakra-button css-yg51i0"]').click();
cy.get('#pin-input-2-0').type(<need to put code here>);
})
it('get code', () => {
cy.loginByGoogleApi()
})
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
cy.wrap(code)
,类似的内容:在您的测试文件中,它就是这样:
您还可以使用
cypress.env
进行全球保存并获得价值。为了节省您可以写的:
获取值
You can use
cy.wrap(code)
, something like this:In your test file, it will be like this:
You can also use the
Cypress.env
to globally save and get the value.To save you can write:
To get the value