Par*_*rma 2 cypress chakra-ui cypress-custom-commands
我通过commands.js下的命令loginByGoogleApi访问我的Gmail帐户,然后获取请求的电子邮件正文并从正文中提取所需的数据,并将其保存在const变量代码中,在我的testFile.cy中输入提取的电子邮件数据(代码) .js。我可以在此之后登录,但 cypress 向我抛出错误cy.type() 失败,因为它针对的是禁用元素。
命令.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)
});
});
})
})
Run Code Online (Sandbox Code Playgroud)
测试文件.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('paras@loadtap.com');
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()
})
})
Run Code Online (Sandbox Code Playgroud)
赛普拉斯错误
cy.type() 失败,因为它针对的是禁用的元素。
输入的元素是:
<input aria-label="Please enter your pin code" inputmode="numeric" type="tel" id="pin-input-2-5" autocomplete="one-time-code" placeholder="" class="chakra-pin-input css-jyb0wy" value="1" data-index="5" disabled="">
Run Code Online (Sandbox Code Playgroud)
在输入内容之前,请确保该元素没有名为“disabled”的属性。
您可以添加force: truewithtype来禁用可操作性检查 -
cy.get('#pin-input-2-0').type('text to type', {force: true});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5776 次 |
| 最近记录: |