标签: cypress

如何通过将其指定为项目来让 Cypress 运行特定的测试文件夹?

我有以下用于使用 Cypress 的目录结构:

cypress-automation
    cypress
        fixtures
        integration
           apps
              pulse
              smhw-prod
              smhw-qa
                  folder-a
                       sample-spec.js
        examples
        plugins
        screenshots
        support
        videos
        node_modules
        cypress.json // this is where the file currently is
        package-lock.json
        package.json
Run Code Online (Sandbox Code Playgroud)

期待

我想要做的是运行smhw-qa文件夹内的所有测试(那里有许多规范文件)..并且能够--project使用 CLI使用命令传递它。

目前,如果我只运行 `--run` 没有任何其他参数所有的规范文件,从所有文件夹将开始运行这是不可取的,因为有多个应用程序“项目”(smhw-qa,smhw-prod 等)在这个结构内。这将允许我仅根据需要从单个文件夹运行规范文件。

我也知道使用--run命令来“运行”特定文件夹,但我想使用“项目”来组织这些测试,以便以后更容易识别它们。

我已经查看了显示--project命令使用的文档,但是我需要帮助了解我还需要设置什么才能完成这项工作。通过这个,我指的是package.json文件。

到目前为止,
我尝试遵循为测试“嵌套文件夹”提供的示例:https : //github.com/cypress-io/cypress-test-nested-projects

package.json:添加了以下脚本:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "smhw-qa": "cypress run --project ./cypress/integration/apps/smhw-qa"
  },
Run Code Online (Sandbox Code Playgroud)

当我像这样通过终端运行它时:

?  cypress-automation npx cypress run --project …
Run Code Online (Sandbox Code Playgroud)

javascript command-line cypress

19
推荐指数
1
解决办法
2万
查看次数

Cypress.io 如何处理异步代码

我正在将我们的旧水豚测试迁移到 cypress.io,因为我们的应用程序正在采用 SPA 方式。

在我们的例子中,我们有 2000 多个测试涵盖了很多功能。因此,测试功能的常见模式是让用户创建和发布商品。

一开始我写了一个案例,其中柏树通过页面并点击所有内容。它起作用了,但我看到要约创建 + 发布花了将近 1.5 分钟才能完成。有时我们需要多个报价。所以我们有一个需要 5 分钟的测试,我们还有 1999 年要重写。

我们想出了 REST API 来创建报价和用户,基本上是测试环境准备的快捷方式。

我到了使用async/await. 所以这就是事情。如果我想在 cypress 中使用普通的异步 JS 代码,我会得到Error: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

这是它的样子:

    const faker = require('faker')
    import User from '../../support/User';

    describe('Toggle button for description offer', () => {
      const user = new User({
        first_name: faker.name.firstName(),
        last_name: faker.name.firstName(),
        email: `QA_${faker.internet.email()}`,
        password: 'xxx' …
Run Code Online (Sandbox Code Playgroud)

javascript testing async-await cypress

18
推荐指数
4
解决办法
3万
查看次数

如何让 Cypress 只处理可见元素?

我正在尝试使用这个元素:

cy.get('[data-cy-component=single-picker-search] input').type('Live');
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它告诉我有超过 1 个,所以它不能这样做。

我尝试添加 { force: true } - 没有任何区别。

我尝试查看每个元素,但如果元素不可见,则失败:

cy.get(singlePickerSearch).each(($el) => {
  cy.wrap($el).type('Live' + '{enter}');
});
Run Code Online (Sandbox Code Playgroud)

如何让它只输入元素可见的位置?我不希望它在这方面失败。

cypress

18
推荐指数
2
解决办法
8833
查看次数

有什么方法可以在本地机器上运行并行 cypress 测试,例如 5 个流

我正在尝试在本地机器上设置 cypress 并运行并行测试。但我找不到一些信息如何做到这一点。

continuous-integration cypress

18
推荐指数
3
解决办法
1万
查看次数

赛普拉斯:如何知道在使用 If 条件时元素是否可见?

我想知道一个元素是否可见。我不知道该怎么做。我知道我们可以运行这个:

cy.get('selector').should('be.visible')

但是如果元素不可见,则测试失败。所以我只想要一个布尔值,如果元素不可见,那么我可以通过 if 条件来决定。

用例:

只有当侧边栏不可见时,我才想通过单击按钮来打开侧边菜单。

if(sidebarIsInvisible){
   cy.get('#sideMenuToggleButton').click();
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

我真的很感激任何贡献。

提前致谢

e2e-testing cypress

18
推荐指数
2
解决办法
3万
查看次数

Cypress:我们如何在 cypress 中使用不记名令牌编写 GET 请求?

我有两个请求:一个是POST请求,另一个是get请求。首先,我通过邮寄方式获取用户访问令牌,在其他情况下,我使用此 accessToken 来登录。我的代码不起作用。

我使用的是window 7和cypress 3.3.5

我的代码:

var value;
describe("Login operation", () => {
  it("Login Request with post method", () => {
    cy.request({
      method:'POST', 
      url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
      body: {
        "userName": "faizanj",
        "password": "abc"
      }
    })
      .then(function(response){
        this.value = response.body.accessToken;
        console.log("Value "+this.value);

        expect(response.body.name).to.equal('Faizan');
        expect(response.status).to.equal(200);
      });
  });

  it('Second test case', function() {

    var authHeader='bearer ${'+this.value+'}';
    const options = {
      method: 'GET',
      url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
      headers:{
        authorization:authHeader,
      }};

    cy.request(options)
      .then((response)=>{
        expect(response.status).to.equal(200);6+9
      });
  });
});
Run Code Online (Sandbox Code Playgroud)

javascript networking get request cypress

18
推荐指数
1
解决办法
3万
查看次数

如何在 Cypress 中使用模块“fs”?

我想从本地存储库导入 JSON 数据。

var fs = require('fs');
var data = fs.readFileSync('./profile.json', 'utf8');
console.log(data);

...
Run Code Online (Sandbox Code Playgroud)

然而Cypress出现了这个错误:

在此输入图像描述

这是我的 cypress 项目中的 package.json。

{
  "name": "cypress_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "npx cypress run",
    "clean-reports": "rm -rf cypress/reports",
    "merge-report": "npx mochawesome-merge --reportDir cypress/reports/separate-reports cypress/reports/full_report.json",
    "generate-report": "npx mochawesome-report-generator --reportDir cypress/reports cypress/reports/full_report.json",
    "after:tests": "npm run merge-report; npm run generate-report",
    "cypress": "npm run clean-reports; npm run test; npm run after:tests"
  },
  "keywords": [],
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@cypress/webpack-preprocessor": "^5.4.1", …
Run Code Online (Sandbox Code Playgroud)

javascript node.js cypress

18
推荐指数
1
解决办法
1万
查看次数

如何在 cypress 中执行 `cy.notContains(text)`?

我可以检查 cypress 中是否存在文本cy.contains('hello'),但现在我从页面中删除 hello,我想检查 hello 不存在,我该怎么做cy.notContains('hello')

javascript textmatching matcher cypress

18
推荐指数
2
解决办法
2万
查看次数

可能会也可能不会出现在页面开头的对话框

我需要处理我的页面可能在加载阶段显示弹出对话框或可能不出现的情况。单击任意位置都会将其删除,我对测试此对话框不感兴趣,但它阻止了我需要访问的页面,因此必须将其删除

这是在对话框出现时获取对话框的代码

cy.get('.wps_popup')
  .find('[data-wps-popup-close]')
  .click()
Run Code Online (Sandbox Code Playgroud)

但我不能把它放在测试的顶部,因为这个元素可能不会出现。

如何处理条件元素 - 我是否需要intercept更改 DOM 并将该代码放入事件侦听器中?

cypress cypress-conditional-testing

18
推荐指数
1
解决办法
457
查看次数

Cypress 并不总是执行点击元素

我正在自动化谷歌计算器。有时赛普拉斯无法执行单击按钮。测试单击按钮(0 到 9 )并执行一些简单的数学运算。并且有 30% 的机会它无法点击元素并且测试将失败。

出现问题时,我还录制了视频。

视频在这里

我的项目位于:https : //github.com/afiliptsov/test-project

To run the test run : "npm run test:e2e:functional"
Run Code Online (Sandbox Code Playgroud)

我尝试使用不同的定位器。最初我只使用 ID ex(#cwbt15),但在我制作了更具体的定位器(#cwbt15 > .cwbtpl > .cwbts)之后仍然有同样的问题。

有谁知道为什么会发生这种情况以及如何避免这种行为?

项目结构是:

  • cypress/PageObject.js - 声明所有元素的地方。
  • cypress/support/commands.js - 创建函数点击和更新值验证的地方。
  • cypress/integration/functional/delete.spec.js - 视频中的测试

javascript testing automated-tests cypress

17
推荐指数
6
解决办法
3万
查看次数