Cypress系列(72)- 詳解 Module API
阿新 • • 發佈:2020-10-23
如果想從頭學起Cypress,可以看下面的系列文章哦
https://www.cnblogs.com/poloyy/category/1768839.html
前言
- 前面介紹 Cypress 如何通過命令列執行,就是採用 cypress run 或 cypress open 命令,但這不是 Cypress 唯一的執行方式
- Cypress 還允許你將它視為一個 Node Module 來執行,然後通過Node.js執行Cypress,這種方式可以更加靈活地定製測試行為
- 當想在執行後直接訪問測試結果時,此功能很有用
如何有用
- 挑選測試用例執行
- 整合所有測試用例,提供一份完整HTML格式的測試報告
- 重新執行單個失敗的 spec 檔案
- 傳送有關測試失敗的通知,包括附帶的螢幕截圖
- 啟動其他構建行為或指令碼
重點
模組 API支援兩個命令:cypress.run()和cypress.open()
cypress.run() 命令詳解
栗子
程式碼
// 匯入 cypress 模組 const cypress = require('cypress') // 執行 run 命令 cypress.run({ spec: './cypress/integration/00_examples/actions.spec.js' }) .then((results) => { // 列印結果 console.log(results) }) .catch((err) => { // 抓取錯誤資訊並列印 console.error(err) })
執行命令
可以在 cmd 視窗或 npm 指令碼中執行下列命令
node 1_run.js
執行結果
最終執行的是actions.spec.js 這個測試用例檔案
引數列表
和 cypress run 命令列執行的引數一樣
命令返回結果
cypress.run() 返回一個Promise物件,該 Promise 包含測試結果物件(類似 json 格式的資料),典型的執行可能會返回以下內容:
{ startedTestsAt: '2020-10-23T06:22:53.210Z', endedTestsAt:'2020-10-23T06:23:23.342Z', totalDuration: 30132, totalSuites: 1, totalTests: 14, totalFailed: 0, totalPassed: 14, totalPending: 0, totalSkipped: 0, runs: [ { stats: [Object], reporter: 'cypress/reporters/custom_reporter.js', reporterStats: [Object], hooks: [Array], tests: [Array], error: null, video: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\videos\\00_examples\\actions.spec.js.mp4', spec: [Object], shouldUploadVideo: true } ], browserPath: '', browserName: 'electron', browserVersion: '85.0.4183.121', osName: 'win32', osVersion: '10.0.18363', cypressVersion: '5.4.0', config: { defaultCommandTimeout: 10000, env: { ENVIRONMENT: 'staging', dev: [Object], qa: [Object], foor: 'bar', key: '系統環境變數哦', host: 'test', api_server: 'http://localhost:8888/api/v1/' }, configFile: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress.json', version: '5.4.0', reporter: 'cypress/reporters/custom_reporter.js', baseUrls: 'http://localhost:7077/', retries: 2, targetEnv: 'dev', projectRoot: 'C:\\Users\\user\\Desktop\\py\\MyCypress', projectName: 'MyCypress', morgan: false, isTextTerminal: true, socketId: 'wj65e', report: true, browsers: [ [Object], [Object], [Object], [Object] ], animationDistanceThreshold: 5, autoOpen: false, baseUrl: null, blockHosts: null, chromeWebSecurity: true, clientRoute: '/__/', componentFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\component', execTimeout: 60000, experimentalSourceRewriting: false, experimentalComponentTesting: false, experimentalFetchPolyfill: false, experimentalNetworkStubbing: false, fileServerFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress', firefoxGcInterval: { runMode: 1, openMode: null }, fixturesFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\fixtures', hosts: null, ignoreTestFiles: '*.hot-update.js', includeShadowDom: false, integrationFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\integration', javascripts: [], modifyObstructiveCode: true, namespace: '__cypress', nodeVersion: 'default', numTestsKeptInMemory: 0, pageLoadTimeout: 60000, pluginsFile: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\plugins\\index.js', port: 59756, projectId: null, reporterOptions: null, reporterRoute: '/__cypress/reporter', requestTimeout: 5000, responseTimeout: 30000, screenshotOnRunFailure: true, screenshotsFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\screenshots', socketIoRoute: '/__socket.io', socketIoCookie: '__socket.io', supportFile: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\support\\index.js', taskTimeout: 60000, testFiles: '**/*.*', trashAssetsBeforeRuns: true, userAgent: null, video: true, videoCompression: 32, videosFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\videos', videoUploadOnPasses: true, viewportHeight: 660, viewportWidth: 1000, waitForAnimations: true, watchForFileChanges: false, xhrRoute: '/xhrs/', cypressEnv: 'production', resolved: { animationDistanceThreshold: [Object], baseUrl: [Object], blockHosts: [Object], browsers: [Object], chromeWebSecurity: [Object], componentFolder: [Object], defaultCommandTimeout: [Object], env: [Object], execTimeout: [Object], experimentalSourceRewriting: [Object], experimentalComponentTesting: [Object], experimentalFetchPolyfill: [Object], experimentalNetworkStubbing: [Object], fileServerFolder: [Object], firefoxGcInterval: [Object], fixturesFolder: [Object], hosts: [Object], ignoreTestFiles: [Object], includeShadowDom: [Object], integrationFolder: [Object], modifyObstructiveCode: [Object], nodeVersion: [Object], numTestsKeptInMemory: [Object], pageLoadTimeout: [Object], pluginsFile: [Object], port: [Object], projectId: [Object], reporter: [Object], reporterOptions: [Object], requestTimeout: [Object], responseTimeout: [Object], retries: [Object], screenshotOnRunFailure: [Object], screenshotsFolder: [Object], supportFile: [Object], taskTimeout: [Object], testFiles: [Object], trashAssetsBeforeRuns: [Object], userAgent: [Object], video: [Object], videoCompression: [Object], videosFolder: [Object], videoUploadOnPasses: [Object], viewportHeight: [Object], viewportWidth: [Object], waitForAnimations: [Object], watchForFileChanges: [Object], configFile: [Object], version: [Object] }, parentTestsFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress', parentTestsFolderDisplay: 'MyCypress\\cypress', supportFolder: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\support', integrationExampleName: 'examples', integrationExamplePath: 'C:\\Users\\user\\Desktop\\py\\MyCypress\\cypress\\integration\\examples', scaffoldedFiles: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ], resolvedNodeVersion: '12.16.3', state: {}, proxyUrl: 'http://localhost:59756', browserUrl: 'http://localhost:59756/__/', reporterUrl: 'http://localhost:59756/__cypress/reporter', xhrUrl: '__cypress/xhrs/' } }
cypress.run() 命令詳解
栗子
程式碼
// 匯入 cypress 模組 const cypress = require('cypress') // 執行 open 命令 cypress.open({ // 命令引數列表 config: { baseUrl: 'http://localhost:8080', }, env: { login_url: '/login', products_url: '/products', } })
執行命令
可以在 cmd 視窗或 npm 指令碼中執行下列命令
node 2_open.js
執行結果
在2_open.js設定的 config 或 env 會當成 CLI 模式下設定的
引數列表
和 cypress open 命令列執行的引數一樣
待更新
完整的 module api 專案