我需要在赛普拉斯进程内部调用外部函数。 我需要这样做,因为元素ID是动态的,因此可以从外部API中获取它。
跟随成人。 异步功能是“ helper.ElementToClick”。
const helper = require("../../../helper")
describe('Test', function() {
it('First', async function() {
cy.visit('https://www.*****.com/');
const idElement = await helper.ElementToClick();
cy.get('#' + idElement).click();
})
})
但是此代码不起作用。
答案 0 :(得分:0)
如果您让元素点击before()
是否有帮助?
const helper = require("../../../helper")
describe('Test', function() {
let idElement;
before(async () => {
idElement = await helper.ElementToClick();
});
it('First', function() {
cy.visit('https://www.*****.com/');
cy.get('#' + idElement).click();
});
});
答案 1 :(得分:0)
您可以将其包装在自定义命令中。我们在类似于以下内容的自定义命令中使用异步命令:
const helper = require("../../../helper");
describe('Test', function() {
it('First', async function() {
cy.visit('https://www.*****.com/');
cy.getElementId().then(idElement => {
cy.get('#' + idElement).click();
});
});
});
Cypress.Commands.add("getElementId", async () => {
return await helper.ElementToClick();
});