Commit 8c6d9107 authored by xie.qin's avatar xie.qin

To support download openapi file.

parent 2b4e6142
......@@ -2,7 +2,7 @@
## 关键词:
- [Cypress](https://www.cypress.io/)
- [Cucumber](https://cucumber.io)
- [Cucumber](https://cucumber.io)
## 说明
```text
Cypress几年前还属于一个比较小众的Web测试方案,现如今已有替代Selenium成为Web自动化测试头把交椅的实力。
......
{
"baseUrl": "http://172.22.18.152/",
"env": {
"TAGS": "@dryrun"
"TAGS": "@openapi",
"combinedDeployment": true,
"service": {
"backend": {
"httpsEnable": false,
"host": "172.22.18.152",
"port": "2345",
"openapiUrl": "/doc.html"
},
"app": {
}
},
"saveOpenApiFileTo": "./"
},
"retries": {
"runMode": 0,
......@@ -9,5 +22,6 @@
},
"pageLoadTimeout": 10000,
"defaultCommandTimeout": 5000,
"testFiles": "**/*.{feature,features}"
"testFiles": "**/*.{feature,features}",
"chromeWebSecurity": false
}
......@@ -13,67 +13,14 @@
"uri": "case-948.feature",
"elements": [
{
"id": "end-to-end-test-case-948;hello-world",
"keyword": "Scenario",
"line": 8,
"name": "hello world",
"tags": [
{
"name": "@case-948",
"line": 1
},
{
"name": "@dryrun",
"line": 7
}
],
"type": "scenario",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 4,
"name": "Home page opened",
"result": {
"status": "passed",
"duration": 555000000
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 5,
"name": "Get testing data file \"accounts.json\" and save in context",
"result": {
"status": "passed",
"duration": 7000000
}
},
{
"arguments": [],
"keyword": "Given ",
"line": 9,
"name": "Home page opened",
"result": {
"status": "passed",
"duration": 121000000
}
}
]
},
{
"id": "end-to-end-test-case-948;case-948",
"keyword": "Scenario",
"line": 16,
"line": 10,
"name": "case-948",
"tags": [
{
"name": "@case-948",
"line": 1
},
{
"name": "@login",
"line": 15
}
],
"type": "scenario",
......@@ -84,7 +31,8 @@
"line": 4,
"name": "Home page opened",
"result": {
"status": "skipped"
"status": "passed",
"duration": 154000000
}
},
{
......@@ -93,16 +41,18 @@
"line": 5,
"name": "Get testing data file \"accounts.json\" and save in context",
"result": {
"status": "skipped"
"status": "passed",
"duration": 2000000
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 17,
"name": "login after input credential with testData number \"case-948\"",
"line": 11,
"name": "Login after input credential with testData number \"case-948\"",
"result": {
"status": "skipped"
"status": "failed",
"error_message": "AssertionError: Timed out retrying after 5000ms: expected 'http://172.22.18.152/login' to include 'home'\n at Context.eval (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case-948.feature:86:12)\n at Context.resolveAndRunStepDefinition (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case-948.feature:10572:29)\n at Context.eval (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case-948.feature:9893:35)"
}
}
]
......
[
{
"keyword": "Feature",
"name": "end to end test case 948",
"line": 1,
"id": "end-to-end-test-case-948",
"tags": [],
"uri": "case948.feature",
"elements": [
{
"id": "end-to-end-test-case-948;case-948",
"keyword": "Scenario",
"line": 3,
"name": "case-948",
"tags": [
{
"name": "@dryrun",
"line": 2
}
],
"type": "scenario",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 4,
"name": "Home page opened",
"result": {
"status": "failed",
"duration": 52000000,
"error_message": "CypressError: `cy.visit()` must be called with a `url` or an `options` object containing a `url` as its 1st argument\n at Context.visit (http://172.22.18.152/__cypress/runner/cypress_runner.js:143674:19)\nFrom Your Spec Code:\n at Context.eval (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case948.feature:77:6)\n at Context.resolveAndRunStepDefinition (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case948.feature:10515:29)\n at Context.eval (http://172.22.18.152/__cypress/tests?p=cypress\\integration\\case948.feature:9836:35)"
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 5,
"name": "Click link to register",
"result": {
"status": "skipped"
}
}
]
}
]
}
]
\ No newline at end of file
......@@ -4,15 +4,9 @@ Feature: end to end test case 948
Given Home page opened
Then Get testing data file "accounts.json" and save in context
@dryrun
Scenario: hello world
Given Home page opened
@register
Scenario: case-948
Then Register with testData number "case-948"
@login
Scenario: case-948
Then login after input credential with testData number "case-948"
Then Login after input credential with testData number "case-948"
import { Then } from "cypress-cucumber-preprocessor/steps";
import { Given, Then } from "cypress-cucumber-preprocessor/steps";
Given('Home page opened', () => {
cy.visit('/')
cy.title().should('include', 'BAAS系统')
})
Given('Get testing data file {string} and save in context', (file) => {
cy.log('Loading testing file: ' + file)
cy.fixture('testdatafiles/' + file).as('testFile')
})
Then('Register with testData number {string}', (caseNum) => {
cy.log('click link "没有账号,去注册"')
......@@ -28,3 +41,18 @@ Then('Register with testData number {string}', (caseNum) => {
//cy.url().should('include', 'login')
})
Then('Login after input credential with testData number {string}', (caseNum) => {
//cy.url().should('include', 'login')
cy.get('@testFile').then((testFile) => {
cy.get('form').should('have.class', 'login-form').within(() => {
debugger
var data = testFile[caseNum]
cy.xpath('div[1]//input').type(data.name)
cy.xpath('div[2]//input').type(data.password)
cy.xpath('div[3]//input').type(data.authCode)
cy.xpath('div[4]//button').click()
})
})
cy.url().should('include', 'home')
})
\ No newline at end of file
import { Given, Then } from "cypress-cucumber-preprocessor/steps";
Given('Home page opened', () => {
cy.visit('/')
cy.title().should('include', 'BAAS系统')
})
Then('login after input credential with testData number {string}', (caseNum) => {
//cy.url().should('include', 'login')
cy.get('@testFile').then((testFile) => {
cy.get('form').should('have.class', 'login-form').within(() => {
debugger
var data = testFile[caseNum]
cy.xpath('div[1]//input').type(data.name)
cy.xpath('div[2]//input').type(data.password)
cy.xpath('div[3]//input').type(data.authCode)
cy.xpath('div[4]//button').click()
})
})
cy.url().should('include', 'home')
})
\ No newline at end of file
import { Given } from "cypress-cucumber-preprocessor/steps";
/**
* Delete the downloads folder to make sure the test has "clean"
* slate before starting.
*/
export const deleteDownloadsFolder = () => {
const downloadsFolder = Cypress.config('downloadsFolder')
Given('Get testing data file {string} and save in context', (file) => {
cy.log('Loading testing file: ' + file)
cy.fixture('testdatafiles/' + file).as('testFile')
})
\ No newline at end of file
cy.task('deleteFolder', downloadsFolder)
}
\ No newline at end of file
@openapi
Feature: Download latest OpenApi file from running service
Scenario: Download latest OpenApi file from backend service
Given swagger-ui page of "backend" service is avaliable
Then Download openapi file and save
\ No newline at end of file
import { Given, Then } from "cypress-cucumber-preprocessor/steps";
import { deleteDownloadsFolder } from '../common/utils'
const path = require('path')
Given('swagger-ui page of {string} service is avaliable', (serviceName) => {
var url = ''
debugger
if (Cypress.env('combinedDeployment')) {
if (Cypress.env('service.backend.httpsEnable')) {
url += 'https://'
}
else {
url += 'http://'
}
let backendService = Cypress.env('service')['backend']
url += backendService['host'] + ':' + backendService['port'] + backendService['openapiUrl']
cy.log('swagger-ui url=', url)
}
else {
switch(serviceName) {
case 'app':
break
}
}
cy.visit(url)
cy.title().should('include', 'BAAS接口文档')
})
Then('download openapi file and save', () => {
deleteDownloadsFolder
const downloadsFolder = Cypress.config('downloadsFolder')
cy.xpath('//div[1]/div/section/aside/div/div[2]/ul/li[3]/div[1]').click()
cy.xpath('//div[1]/div/section/aside/div/div[2]/ul/li[3]/ul/li[2]/a').click()
cy.xpath('//div[1]/div/section/section/div/div[3]/div[2]/main/div/div[2]/button[4]').click()
cy.log('**read downloaded file**')
// file path is relative to the working folder
const filename = path.join(downloadsFolder, '2.X版本_OpenAPI.json')
cy.readFile(filename, { timeout: 15000 }).should('have.length.gt', 1500000)
})
\ No newline at end of file
......@@ -17,12 +17,28 @@
*/
// eslint-disable-next-line no-unused-vars
const cucumber = require('cypress-cucumber-preprocessor').default
const fs = require('fs');
const { fs, rmdir } = require('fs')
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('file:preprocessor', cucumber())
on('task', {
deleteFolder (folderName) {
console.log('deleting folder %s', folderName)
return new Promise((resolve, reject) => {
rmdir(folderName, { maxRetries: 10, recursive: true }, (err) => {
if (err) {
console.error(err)
return reject(err)
}
resolve(null)
})
})
},
})
on('after:run', (results) => {
if (results) {
fs.mkdirSync("cypress/.run", { recursive: true });
......
......@@ -27,5 +27,6 @@
"filePrefix": "",
"fileSuffix": ".cucumber"
}
}
},
"dependencies": {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment