testing - 如何在 CI 环境中集成 cy.visit(local vs stage url)?

标签 testing continuous-integration cypress

我有一个测试套件,它在对某些 DOM 元素做出一堆断言之前打开一个本地 url。

现在我想将 Cypress 集成到我的 CI/CD 中,但我不确定如何告诉 Cypress 访问暂存 url 而不是本地 url。

有什么想法吗?

it('The body of the page is in viewport', function() {
    cy.visit(
      'http://mylocalurltovisit.local.com.au:8666/foo'
    );
    let initialPosition;
    const body = cy.get('body');
    body.should('have.class', 'pdfView');
    cy.get('.body-wrapper').should($el => {
      initialPosition = $el.position();
      expect(initialPosition.top).equal(0);
      expect(initialPosition.left).equal(0);
    });
  });

我希望访问 url 在 CI 环境中自动切换为暂存地址(可能是 http://staging.com.au/foo)。

最佳答案

一种方法是尝试在 cypress.json 文件中为 local 和 staging 站点创建两个不同的 url,如下所示。

{
    "env": {
        "project1_user": "admin",
        "project1_password": "password",
        "localSite" : {
            "url" : "http://mylocalurltovisit.local.com.au:8666/foo"            
        },
        "stagingSite" : {
            "url" : "http://staging.com.au/foo"

        }

    }
}

然后在测试中接收到 const 的 url;

const localUrl = Cypress.env('localSite').url;
const stagingUrl = Cypress.env('stagingSite').url;

你可以在beforeEach中调用或者直接在测试中使用。您可以使用同样的方法来设置临时站点。

beforeEach( function() {
    cy.visit(localUrl + '/somelogin.php' );

} );

关于testing - 如何在 CI 环境中集成 cy.visit(local vs stage url)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509153/

相关文章:

perl - 在 Perl 单元测试中 fork 子进程停止证明;测试::线束退出

android - 如何检测 Android 中的 i18N UI 问题

python - python包开发如何实现跨平台持续集成?

javascript - 访问函数打开无效的 URL

cypress - 如果 Cypress 中的 HTTP 请求失败,则测试失败

cypress - 无法运行 Cypress Open

variables - 如何在 PowerShell 中测试变量是否超过八个字符?

oracle - 测试 Delphi 应用程序的最佳方法

unit-testing - 在单个单元测试失败时失败 TFS 构建

c# - Visual Studio Online Build - web.config 连接字符串 configSource