我的测试将灯具存储在 cypress/fixtures 中,但 Cypress 无法找到它们。这是项目结构。我使用的是 cypress 12.14.0 版本
__tests__
cypress
e2e
mySpec.ts
fixtures
accounts.json
support
command.ts
types.d.ts
tsconfig.ts
cypress.config.ts
Run Code Online (Sandbox Code Playgroud)
__tests__
cypress
e2e
mySpec.ts
fixtures
accounts.json
support
command.ts
types.d.ts
tsconfig.ts
cypress.config.ts
Run Code Online (Sandbox Code Playgroud)
这是我尝试访问夹具的方法
// tsconfig.js
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"types": ["cypress","./support"]
},
"include": ["/**/*cy.ts", "**/**/*.ts"]
}
//cypress.config.ts
import { defineConfig } from "cypress";
export default defineConfig({
projectId: …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要在 cypress 中检查以下流程:
我尝试测试的第一件事很简单:
it('should display spinner during page load', () => {
cy.visit(myPageUrl);
cy.get(selectors.spinner).should('exist');
cy.get(selectors.spinner, { timeout: 5000 }).should('not.exist');
});
Run Code Online (Sandbox Code Playgroud)
然而,这会产生竞争条件。在 cypress 断言旋转器存在之前,页面可能会加载并且旋转器将消失。这是一个直观的解释:
这是我的预期: 预期结果
以下是实际可能发生的情况,这将导致测试失败: 可能的结果
因此,经过一番研究后,我尝试了以下方法来解决竞争条件:
it('should display spinner during page load', () => {
let sendResponse;
const trigger = new Promise((resolve) => {
sendResponse = resolve;
});
cy.intercept('GET', '/myRequiredPageData', (req) => {
return trigger.then(() => {
req.reply();
});
});
cy.visit(myPageUrl);
cy.get(selectors.spinner).should('exist').then(() => {
sendResponse();
cy.get(selectors.spinner, { timeout: 5000 }).should('not.exist');
});
});
Run Code Online (Sandbox Code Playgroud)
但是,现在我有时会收到此错误:
A …
首先单击并从下拉列表中选择如何上传计算机或驱动器等文件,选择计算机后我必须从赛普拉斯的计算机上传。我正在尝试这种方式,但不起作用,点击后无任何操作
cy.get(this.business_registration).click()
cy.fixture('images.png').then(fileContent => {
cy.get(this.select_browse).attachFile({
fileContent,
fileName: 'images.png',
mimeType: 'image/png'
});
});
Run Code Online (Sandbox Code Playgroud)
Cypress 的做法是什么?
我正在使用 cypress-wait-until 插件在我们的框架中应用显式等待。当我使用它时,给定的最大时间是 30000 毫秒,因此理想情况下,它应该等待最多 300000 毫秒(30 秒)才能使元素可见,但它会在 4 秒后超时,这是 cypress 命令的默认超时。
cy.waitUntil(() => cy.get('div.tabs div:nth-child(3)').should('be.visible') ,{timeout:30000})
Run Code Online (Sandbox Code Playgroud)
我想知道我应该在哪个文件中进行哪些更改,以便我可以覆盖为 cypress 规定的默认超时。如果社区在这方面提供一些解决方案,那就太好了。
javascript visibility ui-automation cypress cypress-wait-until
我正在测试一个带有弹出窗口的 Web 应用程序表单,需要花费大量时间来加载另一个需要填写的弹出表单
目前,我等待 20 秒,这主要有效,但有时弹出窗口需要 20 秒以上才能加载,这会使我的测试脚本失败
弹出窗口是使用按钮触发的,当单击按钮时,会发出 ajax 请求,当响应到来时,会加载(渲染)弹出窗口
弹出窗口通常需要这么多时间,因为 Ajax 请求需要很长时间,所以我想创建一个动态函数来等待该弹出窗口呈现
所以我想创建一个动态等待函数,它将等待弹出窗口呈现(注意:它是弹出窗口而不是另一个页面)
代码:
cy.get("div[class='question-btn'] a[class='btn btn-primary btn-lg cat-new-question-sortable']").click()
// button which triggers the popup
cy.wait(9000)
//multiple input, types get filled in the poup
cy.get("input[value='Submit']").click() // button which closes or submits the popup
cy.wait(9500)
cy.get("div[class='question-btn'] a[class='btn btn-primary btn-lg cat-new-question-sortable']").click()
// button which triggers the next popup and cycle repeats
Run Code Online (Sandbox Code Playgroud) 我的写入文件: cy.writeFile("cypress/fixtures/xlsxData.json", Newdata , { flag: 'a+' })
Newdata - 让 Newdata = { FirstName:F_jsonData[i][0], MiddleName:F_jsonData[i][1], LastName:F_jsonData[i][2] }
xlsxdata.json 将是:
[ {
"FirstName": "ABC",
"MiddleName": "K",
"LastName": "edf"
}{
"FirstName": "sss",
"MiddleName": "g",
"LastName": "efg"
} ]
Run Code Online (Sandbox Code Playgroud)
如何在 json 文件中的 2 个对象之间添加逗号?
无法使用下面的 else if 条件并始终坚持使用 if
cy.get("#result").iframeOnload().find(".text-error").invoke('text').as('iframeText');
cy.get('@iframeText').then((iframeText) => {
if(iframeText === expectedFailText){
ResultPage
.getResultErrorMessage()
.should("have.text", expectedFailText);
}
else if(expectedPassText){
ResultPage
.getResultPassedMessage()
.should("have.text", expectedPassText);
}
});
Run Code Online (Sandbox Code Playgroud) 我有一个可以访问 API 的 Cypress 测试。返回一个项目列表,我希望它基本上进行轮询,直到我得到正确的原因(最多 30 次)。
返回的示例是
{
"items":[
{
"reason":"incorrect"
},
{
"reason":"correct"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有以下函数,如果返回正确的原因,它会轮询并返回响应。否则会抛出错误
function pollStatusReason(id, reason, attempts = 0) {
const url = `http://someurl/${id}`;
if (attempts > 30) throw `Too many attempts to get reason: ${reason}`;
return cy.request({
method: 'GET',
url: url,
}).then((response) => {
if (response.status != 200) {
pollStatusReason(id, reason, attempts + 1);
} else {
const items = response.body.items;
for (let i = 0; i < items.length; i++) {
if (items[i].reason …Run Code Online (Sandbox Code Playgroud) 我有一个包含这样的列的表(简化):
\n| ... | ... | Venter p\xc3\xa5 s\xc3\xb8ker | ... |\n| ... | ... | Ikke p\xc3\xa5begynt | ... |\nRun Code Online (Sandbox Code Playgroud)\n该表有 21 行。
\n相关列可以按字母顺序、降序或升序排序。
\n我想做的是让 Cypress 验证排序、升序和降序。
\n到目前为止我所得到的:
\ncy.get([data-e2e-selector=tabell]).get('[data-e2e-selector=kolonne]')\n .then(items => {\n const unsortedItems = items.map((index, html) => Cypress.$(html).text()).get();\n const sortedItems = unsortedItems.slice().sort()\n expect(unsortedItems, 'Items are sorted').to.deep.equal(sortedItems);\n });\nRun Code Online (Sandbox Code Playgroud)\n我想做的是从列中获取所有项目并将它们存储在列表中。然后创建一个包含相同项目但已排序的列表。然后比较两者。我已经成功地对日期戳(数字)做了类似的事情,但按字母顺序排序似乎让我困惑。
\n上面的代码会导致此错误(升序或降序相同):
\nassert expected Items are sorted: to deeply equal [ Array(21) ]\nRun Code Online (Sandbox Code Playgroud)\n为了验证我是否确实将元素放入列表中 - 如果我比较 unsortedItems[] 和sortedItems[] 列表的第一个或最后一个项目,它们是相同的:
\nexpect(unsortedItems, …Run Code Online (Sandbox Code Playgroud) 在我们的应用程序中,我们的屏幕具有多个标题标题(.tasktable__header.tasktable__header--title),我想验证这些标题。我的代码没有完成这项工作,因为它总是查看第一个可用的项目。
verifySubIncidentTitleMatch(incident: string): void {
cy.get(this.#subDossierTitle).each(ele => {
expect(ele.text()).to.equal(incident);
})
}
Run Code Online (Sandbox Code Playgroud)
但我想将标题标题存储在数组中,然后比较它们,但我不知道如何。
这是 HTML。表类位于 div 'top20' 中(第一个屏幕截图),并且 'tasktable__header--title 位于该类的标题内。
cypress ×10
javascript ×4
alphabetical ×1
automation ×1
cypress-component-test-runner ×1
end-to-end ×1
for-loop ×1
iframe ×1
recursion ×1
sorting ×1
testing ×1
typescript ×1
visibility ×1
writefile ×1