Cypress 简明教程
Cypress - Configuration of JSON File
Cypress 配置由一些适用于框架内所有测试的键值对组成。Cypress 默认配置可在测试运行器窗口中的“设置”选项卡→“配置”(展开它)下找到。
Cypress configurations consist of some key-value pairs that are applicable to all tests within a framework. Cypress default configurations are available under the Settings tab→Configuration (expand it) in the Test Runner window.
如果我们在同一窗口中向下看,我们应该看到 Cypress 给出的多个配置的现有值,例如超时、环境变量、文件夹路径等。
If we look further down in the same window, we shall have the existing values of multiple configurations given by Cypress like the timeouts, environment variables, folder path, and so on.
它如下所示:
It is displayed below −
如果我们在同一窗口中向下看,我们应该看到 Cypress 给出的多个配置的现有值,例如超时、环境变量、文件夹路径等。
If we look further down in the same window, we shall have the existing values of multiple configurations given by Cypress like the timeouts, environment variables, folder path, and so on.
它如下所示:
It is displayed below −
Override Default values
要从 cypress.json 文件覆盖默认配置,我们必须指定键值对。
To override the default configurations from the cypress.json file, we have to specify the key-value pairs.
Implementation in cypress.json
覆盖 JSON 文件的默认值实现如下:
The implementation for overriding the default values for JSON file is as follows −
{
"baseUrl" : "https://www.google.com/"
}
此处,键是 baseUrl,值是 https://www.google.com/ 。一旦再次运行测试,就会 changes are reflected in the global configurations ,如下所示:
Here, the key is baseUrl and the value is https://www.google.com/. Once the tests are run again, the changes are reflected in the global configurations, as shown below −
Implementation of Actual Test
覆盖 JSON 文件的默认值的实际测试实现如下:
The implementation of actual test for overriding default values of the JSON file is as follows −
describe('Tutorialspoint', function () {
// test case
it('First Test', function (){
// launch application from configuration
cy.visit("/")
});
});
Execution Results
Execution Results
输出如下 −
The output is as follows −
执行日志显示 baseUrl 已从 cypress.json 文件获取,并且适用于框架内的所有测试。
The execution logs show that the baseUrl has been obtained from the cypress.json file and it is applicable to all tests within the framework.
Override Default configurations
我们可以从测试脚本覆盖默认配置,这将适用于测试用例内的单个测试步骤,而不仅适用于完整框架。
We can override the default configurations from the test scripts, which become applicable to an individual test step, within the test case and not to the complete framework.
这是在 Cypress 中借助 config 命令完成的。
This is done with the help of the config command in Cypress.
例如,如果我们希望为特定测试步骤增加默认超时,实现如下所示:
For example, if we want to increase the default timeout for a particular test step, implementation shall be as follows −
//set default time out to nine seconds from following steps in test
Cypress.config('defaultCommandTimeout',9000)
landPage.selectUser().click()
同时,如果在 cypress.json 文件中将 defaultCommandTimeout 值设置为 7 秒,那么 Cypress 将优先考虑应用于测试步骤的超时(即 9 秒)。
Simultaneously if the defaultCommandTimeout value is set to seven seconds in the cypress.json file, then Cypress shall give preference to the timeout applied to the test step(i.e nine seconds).
最后,它优先考虑默认配置。
Finally, it gives preference to the default configurations.
Disable Overriding Default configurations
我们可以禁用从 cypress.json 中覆盖默认配置的功能。
We can disable the feature to override the default configurations from the cypress.json.
cypress.json 中的配置如下:
The configuration in cypress.json is as follows −
{
"defaultCommandTimeout" : "9000"
}
要禁用以上配置,运行以下命令:
To disable the above configuration, run the below mentioned command −
npx cypress open --config-file false
在运行上述命令后,测试运行器窗口的设置选项卡将显示 config 标记设置为 false。
After running the above command, the Settings tab of the Test Runner window will show the config flag set to false.
此外,defaultCommandTimeout 被设为 4 秒,这是由默认配置设定的,并且不会被 cypress.json 的 9 秒值覆盖。
Also, defaultCommandTimeout is set to four seconds, which is set by the default configuration and not overridden by cypress.json value of nine seconds.