Cypress 简明教程
Cypress - Build First Test
一旦 Cypress 配置完毕,就会在项目内创建一个框架,该框架在 Explorer 中自动可见。新的测试文件(比如 FirstTest.spec.js)应在 integration 文件夹中创建,如下所示。
Once Cypress has been configured, a framework gets created within the project which is automatically visible in the Explorer. The new test file (say FirstTest.spec.js) should be created within the integration folder, as mentioned below.
Cypress Folder Structure
让我们了解 Cypress 中的文件结构。Cypress 文件夹中包含的因素如下所述:
Let us understand the folder structure in Cypress. The factors that are included in a Cypress folder are explained below −
-
fixtures − Test data in form of key-value pairs for the tests are maintained here.
-
integration − Test cases for the framework are maintained here.
-
plugins − Cypress events (prior and post events to be executed for a test) are maintained here.
-
support − Reusable methods or customized commands, which can be utilised by test cases directly, without object creation are created here.
-
videos − Executed test steps are recorded in the form of videos and maintained here.
-
node_modules − Project dependencies from the npm are maintained in this folder.It is the heart of the Cypress project execution.
-
cypress.json − Default configurations are set in this folder. The values of the current configurations can be modified here, which overrules the default configurations.
-
package.json − Dependencies and scripts for the projects are maintained in this folder.
Structure of a Basic Test
Cypress 遵循 JavaScript 测试框架(Mocha、Jasmine 等)。要在 Cypress 中创建测试,我们必须遵守以下框架准则:
Cypress follows the JavaScript test frameworks (Mocha, Jasmine, and so on). To create a test in Cypress, we have to adhere to the below mentioned framework guidelines −
-
Test suite name has to be provided within the describe function.
-
Test case names within a test suite have to be provided within the same or you have to specify the function.
-
Test steps within a test case have to be implemented inside the it/specify block.
Basic Test Implementation
基本的测试执行可以通过使用如下命令完成:
The basic test implementation can be done by using the following command −
// test suite name
describe('Tutorialspoint Test', function () {
// Test case
it('Scenario 1', function (){
// test step for URL launching
cy.visit("https://www.google.com/");
});
});
上面使用的cy命令不需要对象调用。它在默认安装节点模块时是可用的。
The cy command used above does not require an object invocation. It becomes available by default on installing the node modules.
Test Execution
对于执行 from the command line ,运行下面给出的命令:
For execution from the command line, run the command given below −
./node_modules/.bin/cypress run
这里,集成文件夹内的所有文件都会被触发。
Here, all the files within the integration folder get triggered.
对于执行 from the Test Runner ,运行下面给出的命令:
For execution from the Test Runner, run the command stated below −
./node_modules/.bin/cypress open
然后,点击我们要触发执行的说明文件。
Then, click on the spec file that we want to trigger for execution.
要触发执行 for a specific file from command line ,运行下面给出的命令:
To trigger execution for a specific file from command line, run the command mentioned below −
cypress run --spec "<spec file path>"
以下屏幕将出现在您的计算机上:
The following screen will appear on your computer −