本文章基于codeceptjs@1.0.1
這篇文章會介紹codeceptjs配置文件的基本用法握截。
codeceptjs init
創(chuàng)建的文件codecept.json
就是整個codeceptjs項目的配置文件之剧。這個文件處于整個項目的根目錄下汤踏。
其大概的內(nèi)容如下:
{
"output": "./output", //配置哪里存放失敗的測試截圖等
"helpers": { //配置helper
"WebDriverIO": { //配置webdriverio作為helpers
"url": "http://simple-form-bootstrap.plataformatec.com.br", //配置base url
"browser": "chrome", //配置運行的瀏覽器
"smartWait": 5000, //optional紊搪,配置Smart Wait時間
"restart": false, //optional,配置是否在測試場景之間重啟瀏覽器羽资,默認值是true
"keepBrowserState": false, //optional, 當restart設置為false的時候,配置是否在測試之間保持瀏覽器的狀態(tài)贮乳, 默認值是false
“disableScreenshots”: false, //optional, 配置是否在測試失敗時存儲截圖,默認值是false
"uniqueScreenshotNames": false, //optional, 如果有同名的測試場景在不同的測試套件里面恬惯,配置是否防止測試截圖的覆蓋向拆,默認值是false
"keepCookies": false, //optional, 當restart設置為false的時候,配置是否在測試之間保持cookies,默認值是false
"windowSize": "maximize", //optional, 設置瀏覽器窗口大小酪耳。值可以為"maximize"或格式為"640*480"的尺寸
"waitForTimeout": 5000, //optional, 給所有wait*()方法設定的默認等待時間. 默認值是1000
"timeouts": { // 配置超時時間亲铡。webdriverio的timeouts為鍵值對形式
"script": 60000,
"page load": 10000
},
"manualStart": false, //optional, 如果設置為true,則在測試之前不會啟動瀏覽器葡兑,而需要在helper中加上方法`this.helpers["WebDriverIO"]._startBrowser()`來人工啟動。默認值是false
"url": "YOUR_DESIRED_HOST", //需要連接到cloud provider上去進行測試 比如browserstack或sauce lab
"user": "YOUR_BROWSERSTACK_USER",
"key": "YOUR_BROWSERSTACK_KEY",
"desiredCapabilities": {
"browserName": "chrome",
// only set this if you're using BrowserStackLocal to test a local domain
// "browserstack.local": true,
// set this option to tell browserstack to provide addition debugging info
// "browserstack.debug": true,
"proxy": { 配置代理訪問selenium server
"proxyType": "manual",
"httpProxy": "http://corporate.proxy:8080",
"socksUsername": "codeceptjs",
"socksPassword": "secret",
"noProxy": "127.0.0.1,localhost"
}
}
},
"MyHelper": { //配置自定義的helper
"require": "./helpers/custom_helpers.js", //配置自定義helper的文件位置
"defaultHost": "http://mysite.com" //配置base url
},
"Mochawesome": { //在測試失敗時赞草,給mochawesome報告加上screenshot
"uniqueScreenshotNames": "true"
}
},
"include": { //配置actor和pageobject讹堤。這樣才能在測試文件中進行引用
"I": "./steps_file.js",
"landingPage": "./pages/landingPage.js", //配置Page Object的文件,可以讓測試文件直接調(diào)用
"landingPageStep": "./steps/LandingPage.js", //配置Step Object的文件厨疙,可以讓測試文件直接調(diào)用
"landingPageFragment": "./fragments/LandingPage.js" //配置Page Fragment的文件洲守,可以讓測試文件直接調(diào)用
},
"mocha": { //mocha選項,可以配置reporter
"reporterOptions": {
"reportDir": "output", //配置產(chǎn)生的測試報告文件放到output目錄下沾凄,比如Mochawesome產(chǎn)生的文件
"mochaFile": "output/result.xml" //配置mocha-junit-reporter產(chǎn)生的xml文件放到output目錄下
}
},
"bootstrap": "./util/bootstrapAndTeardown/selenium-standalone-start.js", //配置bootstrap腳本的位置梗醇,讓此腳本在全部測試運行之前運行
"teardown": "./util/bootstrapAndTeardown/selenium-standalone-stop.js", //配置teardown腳本的位置,讓此腳本在全部測試運行之后運行
"hooks": ["./util/custom_hooks/event_listener.js"], //配置自定義的hook文件來擴展codeceptjs
"tests": "./spec/s*_test.js", //配置需要運行的測試
"timeout": 10000, //默認的測試超時時間
"name": "codeceptjs-init", //項目名稱撒蟀,not used
"multiple": { //配置run-multiple 并發(fā)多個測試套件的執(zhí)行
"smoke": { //配置一個測試套件 smoke
"grep": "@smoke", //配置了該測試套件僅包含有@smoke標簽的測試場景叙谨。如果沒有定義,則代表所有測試場景
"browsers": [ //配置可以在chrome和firefox中運行測試
"chrome",
"firefox"
]
}
}
}
動態(tài)配置
override選項
通過選項--override
或-o
我們可以在運行測試的時候保屯,修改相應的配置手负,做到運行時的動態(tài)配置。
codeceptjs run -o '{ "helpers": {"WebDriverIO": {"browser": "firefox"}}}'
config選項
通過選項--config
或-c
姑尺,我們可以運行指定的配置文件竟终;這些文件可以是不同名字的或是在項目不同的目錄下
codeceptjs run --config=./path/to/my/config.json
使用codecept.conf.js
我們可以創(chuàng)建codecept.conf.js
來包含更多動態(tài)的配置項。只需要export config屬性即可切蟋。
exports.config = {
helpers: {
WebDriverIO: {
// load variables from the environment and provide defaults
url: process.env.CODECEPT_URL || 'http://localhost:3000',
user: process.env.CLOUDSERVICE_USER,
key: process.env.CLOUDSERVICE_KEY,
coloredLogs: true,
waitForTimeout: 10000
}
},
// don't build monolithic configs
mocha: require('./mocha.conf.js') || {},
// here goes config as it was in codecept.json
// ....
};
process.profile and --profile選項
通過在codecept.conf.js
中配置process.profile
统捶,我們可以在運行測試時通過--profile
制定相關的值寫入配置文件,做到動態(tài)配置柄粹。
比如下面的列子中喘鸟,--profile firefox
會被作為process.profile
的值寫入配置文件.
codeceptjs run --profile firefox
exports.config = {
helpers: {
WebDriverIO: {
url: 'http://localhost:3000',
// load value from `profile`
browser: process.profile || 'firefox'
}
}
};