简体   繁体   English

如何使用cypress在网页上运行javascript

[英]How to run javascript on a webpage using cypress

I'm writing a cypress test and following javascript works in the chrome console我正在写一个赛普拉斯测试并在 javascript 之后在 chrome 控制台中工作

$.event.trigger({
type: "Scan",
message: "scanned",
time: new Date()
});

Is there a way I can run the command above in cypress when I get to the specific page?当我到达特定页面时,有没有一种方法可以在柏树中运行上面的命令?

I believe you can do this within a .then() block.我相信您可以在.then()块中执行此操作。 Since cy commands run asynchronously and get queued up with all the other cy commands, .then() will delay the execution of the JavaScript within until the cy command actually does its thing.由于 cy 命令异步运行并与所有其他 cy 命令排队, .then()将延迟 JavaScript 的执行,直到 cy 命令实际执行它的操作。

So like this...所以像这样...

cy.visit('/my-special-page').then(() => {
   // Do your JavaScript here
})

Try this:试试这个:

Cypress.$.event.trigger({ type: "Scan", message: "scanned", time: new Date() }); Cypress.$.event.trigger({ type: "Scan", message: "scanned", time: new Date() });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM