cy.visit() 必须使用 url 或包含 url 作为第一个参数的选项对象来调用 - Cypress.env('StudentPortal')

问题描述 投票:0回答:2

我正在尝试通过插件方法使用环境变量(https://docs.cypress.io/api/plugins/configuration-api#Switch- Between-multiple-configuration-files),这以前有效,但最近已停止为我工作,任何指示都会很棒。

插件/index.js

const fs = require('fs-extra')
const path = require('path')

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('cypress', 'config', `${file}.json`)

  return fs.readJson(pathToConfigFile)
}


module.exports = (on, config) => {

  const file = config.env.configFile || 'build'

  return getConfigurationByFile(file)

}

配置文件 > build.json

{
    "env": {
        "StudentPortal": "https://www.google.co.uk"
    }
}

用法

cy.visit(Cypress.env('StudentPortal'));

正如我所说,这曾经有效并且会访问 configFile 中的 URL,现在我只收到以下错误:

赛普拉斯错误 cy.visit() 必须使用 url 或包含 url 作为第一个参数的选项对象调用了解更多 赛普拉斯/support/commands.js:17:8

15 | Cypress.Commands.add('StudentPortalLogin', (电子邮件, 密码) => { 16 |

17 | cy.visit(Cypress.env('StudentPortal'));

javascript node.js cypress
2个回答
0
投票

baseURL 似乎丢失了。

cy.visit()
正在寻找引用 baseURL 的内容。我没有看到在 build.json 文件中定义的一个,这可能会修复添加 baseURL“https://www.google.co.uk”的错误,然后将身份验证参数放入 env{} 部分,每您链接中的示例。


0
投票
It looks like you’re dealing with Cypress and encountering an issue related to cy.visit(). Let’s break down the solution step by step:
1)Configure cypress.config.js: In your cypress.config.js file, you’ll define your test configuration. Specifically, you want to set the baseUrl for your tests. Here’s the code snippet you provided:
const { defineConfig } = require("cypress");
module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // Implement node event listeners here if needed
    },
    env: {
      baseUrl: 'Use the your 'URL'',
    }
  },
});
Make sure to adjust the baseUrl to match the URL of the application you’re testing. This is the base URL that cy.visit() will use.
2)Use the baseUrl in Your Test: Now, in your test file (e.g., login.cy.js or any other spec file), you can access the baseUrl using Cypress.env('baseUrl'). Here’s an example
const url = Cypress.env('baseUrl');
describe('Login Tests', () => {
  it('should visit the login page, () => {
    cy.visit(URL); // Use the 'URL' variable here
    // Add your login-related assertions or actions
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.