owned this note changed 4 years ago
Linked with GitHub

從內部工具開始的網頁自動化測試人生 / Set Mao(毛太一)

歡迎來到 Modern Web 2020 共筆 :mega:
共筆入口:https://hackmd.io/@ModernWeb/2020
手機版請點選上方 按鈕展開議程列表。

簡報下載

共筆從這開始

怎麼確保功能運作正常

  1. 選擇並上傳檔案
  2. 上傳中
  3. 確認並匯入資料
  4. 匯入完成
  5. 檢視完整資料

這樣有什麼問題?

  • 無聊、浪費⼈⼒(錢)、沒有我就只是想玩看看 selenium
  • ⽽且隨著⽀援平台的增加、測試檔案的增加、測試所花的⼈⼒成本也會跟著越來越⾼

怎麼開始的?

How?

  • Python + Selenium

Result

  1. 選擇並上傳檔案
  2. 上傳中
  3. 確認並匯入資料
  4. 匯入完成
  5. 檢視完整資料

減少了 10~15 分鐘的⼿動上傳操作時間,但是仍然需要⼈⼒比對答案,⽽且要從CLI 介⾯啟動,對沒有程式背景的⼈來說不那麼友善

人是貪心的,能不能更好用一點?

難⽤的點

  • 只有上傳功能,沒有⾃動按匯入的功能-> 寫
  • 還是需要⼈⼒檢查答案-> 只判斷資料筆數就好
  • 要從 CLI 啟動-> 包成 exe
  • 測試檔案要⾃⼰下載-> 串 google drive api 直接從雲端下載檔案

測試流程全部⾃動化了(⼤約減少 1 時/天)
⼯讀⽣也掰掰了

一個你就滿足了嗎?有没有想過提高測試範圍?

在寫扣之外

  • 測試⽂件
  • 測試檔案
  • 測試流程
  • 測試環境

在寫扣之前

  • 現有的扣有什麼問題?
  • 你有聽過 Page Objects 嗎?

  • Benefits of using page object pattern:
    • Creating reusablecode that can be shared across multiple test
      cases
    • Reducing the amount of duplicated code
    • If the user interface changes, the fix needs changes in only one place

Page Objects 是什麼?

Selenium(Python) 的官⽅⽂件中 Page Objects 會包含以下幾種
東⻄

  1. Test case
  2. Page object classes
  3. Page Element
  4. Locators

第⼀步,寫好測試流程

  1. 進到儀表板⾴⾯
  2. 定位到想要看的圖表
  3. 確認圖表中的數字是否正確

第⼆步,把網⾴切成⼀塊⼀塊的

第三步,抓到每個要⽤的元素的位置

替代⽅案

  • Selenium IDE
    1. 建立專案
    2. 錄腳本
    3. 輸出程式碼

    Result

⽤ Page Object 改寫完了看起來⼀切都很完美
所以我們可以開始寫下⼀個測試了( 嗎?)
這個東⻄看起來很讚,但是可以讓它每天⾃⼰跑嗎?
啊,跑完記得要回傳測試結果喔

這個東西很讚,可以讓它每天自己跑嗎?

  • crontab
  • Selenium hub
  • Docker
  • Slack
  • Result

自動化測試該怎麼跟手動測試合作、溝通?

來⾃⼿動測試⼼中的疑惑

  • 你們在幹嘛?

    1. 你們做了哪些 Test case?
    2. 測試覆蓋度多⼤?
    3. 可以教我怎麼操作⾃動化測試嗎?
    • 教他看 Code?
    • 寫⽂件?
    • Gherkin(Python: Behave)

    Gherkin

    • Gherkin uses a set of special keywords to give structure and meaning to executable specifications.
    • Each keyword is translated to many spoken languages.
    • Given(假如) -> 前置條件
    • When(當) -> 動作
    • Then(那麼) -> 結果
    • And(⽽且)
    • But(但是)

    Given I go to login page
    When I login with correct account and password
    Then I should login success
    And I will redirect to index page

    Behave

    • behave is behaviour-driven development, Python style.
    • behave uses tests written in a natural language style, backed upby Python code.
    • Result
      • Before: Test Case -> Page -> Locator/Element
      • After: Feature -> Step -> Page -> Locator/Element
  • 這個怎麼⽤?

    1. 我要怎麼使⽤⾃動化測試?
    2. 我要怎麼知道執⾏結果是成功還是失敗?
    3. 我要怎麼知道是哪個測試失敗了?

    Jenkins

    Result

Conclusion

  • Python + Selenium
  • Page-Object
  • Gherkin
tags: MW20 維運與測試 自動化測試
Select a repo