如何让赛普拉斯在页面上执行 API 脚本?

Posted

技术标签:

【中文标题】如何让赛普拉斯在页面上执行 API 脚本?【英文标题】:How to make Cypress execute an API script on a page? 【发布时间】:2021-09-06 09:20:21 【问题描述】:

我目前正在尝试将我的 Protractor 代码转换为 Cypress 代码。例如,我的一些量角器代码涉及使网页执行 API 脚本:

import  browser  from “protractor”; // this is the import I used

browser.executeScript(‘arguments[0].click()’;, this.closeButton); // this is for button clicking

browser.executeScript(‘localStorage.setItem(“example-boolean”, “false”)’); // this is for setting a value to false

这些代码行是否有 Cypress 等效项?

【问题讨论】:

【参考方案1】:

一些灵感:

import "cypress-localstorage-commands";

cy.get('#yourCloseBtnId').click();  // Clicking on the element with an ID #yourCloseBtnId

cy.setLocalStorage("example-boolean", false);  // Setting an item in the local storage

【讨论】:

以上是关于如何让赛普拉斯在页面上执行 API 脚本?的主要内容,如果未能解决你的问题,请参考以下文章