Cypress 简明教程
Cypress - Fixtures
利用 Cypress 的装置来维护并保存自动化的测试数据。 装置保存在 Cypress 项目中的装置文件夹(example.json 文件)中。基本上,它帮助我们从外部文件读取数据输入。
Cypress fixtures are added to maintain and hold the test data for automation. The fixtures are kept inside the fixtures folder (example.json file) in the Cypress project. Basically, it helps us to get the data input from external files.
Cypress 装置文件夹可以具有 JSON 或其他格式的文件,数据保存在“键:值”对中。
Cypress fixtures folder can have files in JSON or other formats and the data is maintained in "key:value" pairs.
所有测试数据都可以被多个测试使用。所有固定数据都必须在 before 钩子块内声明。
All the test data can be utilised by more than one test. All fixture data has to be declared within the before hook block.
Syntax
Cypress 数据驱动的测试语法如下所示:
The syntax for Cypress data driven testing is as follows −
cy.fixture(path of test data)
cy.fixture(path of test data, encoding type)
cy.fixture(path of test data, opts)
cy.fixture(path of test data, encoding type, options)
在此,
Here,
-
path of test data is the path of test data file within fixtures folder.
-
encoding type − Encoding type (utf-8, asci, and so on) is used to read the file.
-
Opts − Modifies the timeout for response. The default value is 30000ms. The wait time for cy.fixture(), prior throws an exception.
Implementation in example.json
以下是 example.json 在 Cypress 中使用数据驱动测试的实现 −
Given below is the implementation of data driven testing with example.json in Cypress −
{
"fullName": "Robert",
"number": "789456123"
}
Implementation of Actual Test
在 Cypress 中实际进行数据驱动测试的实现如下:
The implementation of actual data driven testing in Cypress is as follows −
describe('Tutorialspoint Test', function () {
//part of before hook
before(function(){
//access fixture data
cy.fixture('example').then(function(regdata){
this.regdata=regdata
})
})
// test case
it('Test Case1', function (){
// launch URL
cy.visit("https://register.rediff.com/register/register.php")
//data driven from fixture
cy.get(':nth-child(3) > [width="185"] > input')
.type(this.regdata.fullName)
cy.get('#mobno').type(this.regdata.number)
});
});
Execution Results
Execution Results
输出如下 −
The output is as follows −
输出日志显示值 Robert 和 789456123 分别被输入到姓名和手机号码字段。此数据已从装置传递到测试。
The output logs show the values Robert and 789456123 being fed to the Full Name and Mobile No. fields respectively. This data has been passed to the test from the fixtures.