Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern Web 2016 議程分享:
網站自動化測試
 - 以 PIXNET 搜尋 & 
美妝口碑大賞為例

chph
September 29, 2016

Modern Web 2016 議程分享:
網站自動化測試
 - 以 PIXNET 搜尋 & 
美妝口碑大賞為例

chph

September 29, 2016
Tweet

More Decks by chph

Other Decks in Technology

Transcript

  1. 議程講者簡介 - 劉艾霖 (Alin) 2009 Java Developer 2014/04 SDET (Software

    Design Engineer in Test) - 遠端⼯作 SDET :會寫⾃動化測試程式的⼯程師 STE, Software Test Enginner 未必會寫測試程式,⼀般的測試⼯作 2014/10 把測試環境架構解決, 整理成免費電⼦書參加鐵⼈賽 2015/01 Gap Year - 出國散⼼ 2015/11 Node.js developer (Exma-Square) 
  2. 

  3. Webdriver.IO + Mocha var assert = require('assert'); ! describe(‘PIXNET ⼤⾸⾴',

    function() { it('標題應為品牌標準字', function() { browser.url(‘https://www.pixnet.net'); var title = browser.getTitle(); assert.equal(title, ‘痞客邦 PIXNET'); }); }); 
  4. 安裝步驟 (空⺫錄) ⽤ npm init 產⽣ package.json $ npm init

    (⼀直按 enter 帶⼊預設值,或是根據需求填寫) 
  5. package.json { "name": "webdriverio-demo", "version": "1.0.0", "description": "", "main": "index.js",

    "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } 
  6. 在何處執⾏測試? ? Where do you want to execute your tests?

    (Use arrow keys) › On my local machine 個⼈電腦 In the cloud using Sauce Labs, Browserstack or Testingbot In the cloud using a different service I have my own Selenium cloud 
  7. ⽤哪款測試框架? ? Which framework do you want to use? (Use

    arrow keys) › mocha 來杯摩卡 jasmine cucumber 
  8. 印哪種報表格式? ? Which reporter do you want to use? (Press

    <space> to select) ›◯ dot - https://github.com/webdriverio/wdio-dot-reporter ̋ spec - https://github.com/webdriverio/wdio-spec-reporter ̋ junit - https://github.com/webdriverio/wdio-junit-reporter ̋ allure - https://github.com/webdriverio/wdio-allure-reporter ̋ teamcity - https://github.com/sullenor/wdio-teamcity-reporter ̋ json - https://github.com/fijijavis/wdio-json-reporter ̋ concise - https://github.com/FloValence/wdio-concise-reporter 
  9. DOT Reporter // wdio.conf.js module.exports = { // ... reporters:

    ['dot'], // ... }; ! ! !  http://webdriver.io/guide/reporters/dot.html
  10. SPEC Reporter // wdio.conf.js module.exports = { // ... reporters:

    [’spec'], // ... }; ! !  http://webdriver.io/guide/reporters/spec.html
  11. 串哪種服務? ? Do you want to add a service to

    your test setup? (Press <space> to select) ̋ sauce - https://github.com/webdriverio/wdio-sauce-service ̋ browserstack - https://github.com/itszero/wdio-browserstack-service ̋ testingbot - https://github.com/testingbot/wdio-testingbot-service ̋ appium - https://github.com/rhysd/wdio-appium-service ̋ firefox-profile - https://github.com/webdriverio/wdio-firefox-profile-service ̋ selenium-standalone - https://github.com/webdriverio/wdio-selenium- standalone-service › ̋ phantomjs - https://github.com/cognitom/wdio-phantomjs-service 
  12. 噴多細的 Log ? ? Level of logging verbosity (Use arrow

    keys) silent (不看任何 log, 預設值) › verbose (鉅細靡遺) command data result error 
  13. 螢幕截圖放哪裡 ? ? In which directory should screenshots gets saved

    if a command fails? › ./errorShots/ (預設值) 
  14. 網址前綴? ? What is the base url? › (http://localhost) 預設值

    - 本機電腦 * 只有在測試程式裡寫相對路徑 (/) 時,會⾃動帶⼊此 URL
 * 注意 80 port 有沒有被其他軟體搶⾛ 
  15. Packages installed successfully, creating configuration file... ! Configuration file was

    created successfully! To run your tests, execute: $ wdio wdio.conf.js
  16. wdio.conf.js - maxInstances exports.config = { // 如果有 3 種

    capabilities (Chrome, Firefox, Safari) // 那麼 1 個 wdio 會跑 3 個 processes; // 若有10 ⽀測試 spec,則同⼀時間會有 30 個 processes 執⾏ ! maxInstances: 10, } 
  17. wdio.conf.js - capabilities exports.config = { capabilities: [{ // 如果⾃架

    Selenium 只有 5 個 瀏覽器 instance 可⽤,那就限制它最多同 時間只能跑 5 個 instance maxInstances: 5, // 可以填 chrome 或 firefox 等 browserName: ‘phantomjs' }], } 
  18. wdio.conf.js -connection 相關 exports.config = { // 預設 waitFor* 指令的等待時間

    10 秒 waitforTimeout: 10000, // 預設的 request timeout 時間為 90 秒 connectionRetryTimeout: 90000, // 預設 request 最多重試 3 次 connectionRetryCount: 3, } 
  19. vim test/specs/first.js var webdriverio = require('webdriverio'); describe('PIXNET ⼤⾸⾴', () =>

    { it(‘<title> 應為品牌標準字', () => { const title = browser.url(‘https://www.pixnet.net').getTitle(); console.log(title); }) }) 
  20. vim test/specs/first.js var webdriverio = require(‘webdriverio’), assert = require('assert'); describe('PIXNET

    ⼤⾸⾴', () => { it(‘<title> 應為品牌標準字', () => { const title = browser.url(‘https://www.pixnet.net').getTitle(); assert.equal(title, '痞客邦 PIXNET’); }) }) 
  21. 試著故意改成錯的斷⾔ var webdriverio = require(‘webdriverio’), assert = require('assert'); describe('PIXNET ⼤⾸⾴',

    () => { it(‘<title> 應為品牌標準字', () => { const title = browser.url(‘https://www.pixnet.net').getTitle(); assert.equal(title, '痞客邦 pixnet’); }) }) 
  22. ⼤⾸⾴焦點區塊圖⽚尺⼨測試 var webdriverio = require(‘webdriverio’), assert = require('assert'); describe('PIXNET ⼤⾸⾴',

    () => { it('焦點區塊圖⽚應正常顯⽰',function() { var imgs = browser.url('https://www.pixnet.net/').getElementSize('#feature-box-ul li img'); imgs.map(function (img) { assert(590 === img.width); assert(393 === img.height); }); }); }) 
  23. ⼤⾸⾴圖⽚網址應為正式網域 var webdriverio = require(‘webdriverio’), assert = require('assert'); describe('PIXNET ⼤⾸⾴',

    () => { it('焦點區塊圖⽚網址應為 pimg.tw 正式網域',function() { var imgUrls = browser.url(‘https://www.pixnet.net/') .getAttribute('#feature-box-ul li img', 'src'); imgUrls.map(function (url) { assert(url.match(/pimg.tw\//)); }); }); }) 
  24. 講者的⼩結 • 前端測試 *真的* 沒有這麼痛苦了 ! • Geb vs WebdriverIO

    ! • 速度 - Groovy (爬) vs nodeJS (飛) • Geb page object ⽐較優美
  25. References  1. 無痛網站⾃動化測試, 劉艾霖, Modern Web 2016, http:// slides.com/alincode/deck-2

    2. modern web 2016 e2e test sample https://github.com/ alincode/modern-web-2016-e2etest