Python+Behave+Allure Web UI自動化測試

基於BDD模式的Web UI自動化測試體驗,集成了python,behave,allure,非主流的一個路線,可以一起玩玩。

1. 概念解釋

Python: 大家都懂,3以上版本

Behave:行爲驅動開發(behavior driven development)是一種敏捷的軟件開發技術,該框架可以實現UI自動化,也可以實現接口自動化,我們已經開發過接口自動化工具,這個主要用來做主要功能的UI檢查。

Behave官方文檔

Allure:Allure Framework是一種靈活的輕量級多語言測試報告工具,它不僅可以以簡潔的Web報告形式非常簡潔地顯示已測試的內容,而且還允許參與開發過程的每個人從日常執行中提取最大程度的有用信息。 

Allure官方文檔

2. 安裝命令

pip install behave(我用的是pip3)

pip install allure-behave

3. 工程目錄

一個基本的behave至少需要構建以下目錄,以下我會基於一個最簡單的登錄場景描述。

features/
features/login.feature
features/steps/
features/steps/login.py

工程目錄下包含features,features文件夾裏包含目標執行的.feature文件和step文件夾裏存放對應的.py

mkdir 文件夾

touch 文件

A--login.feature文件如下:場景描述寫在這裏,這類文件可以由測試,產品或其他相關人員完成,使用的是自然語言,支持中文。

feature: 功能名稱/scenario:場景名稱/given:給出測試的前提條件/when:測試步驟/then:預期結果

Feature: UI測試

   Scenario Outline: 登錄後臺系統
      Given 打開後臺登錄頁面並且通過後臺驗證
      When 輸入<username>和<password>並點擊登錄
      Then 正確跳轉進入系統後臺

      Examples: userinfo
         |username       |password|
         |admin          |111111|

知識點】這裏對應given, when, then都有一個以step_命名的方法來對應實現,爲什麼以step開頭命名,官方文檔就是這樣給的,未深究;傳遞的參數應使用{}來指明,同時參數名和元素名不要重名

B--steps文件下,是feature文件對應的要執行的python文件

#encoding: utf-8
from behave import *
from features.utils.seleniumapi import SeleniumApi
import time


@given('打開後臺登錄頁面並且通過後臺驗證')
def step_open(self):
    SeleniumApi.step_startup(self)


@when('輸入{username}和{password}並點擊登錄')
def step_login(self, username, password):
    time.sleep(1)
    SeleniumApi.step_send_keys(self, "//*[@placeholder='郵箱']", username)
    time.sleep(1)
    SeleniumApi.step_send_keys(self, "//*[@placeholder='密碼']", password)
    SeleniumApi.step_click(self, "//*[@type='button']")
    time.sleep(3)


@then('正確跳轉進入系統後臺')
def step_forward_top(self):
    if '用戶名' in SeleniumApi.step_text_cssselector(self, "hidden-sm"):
        assert True
    else:
        assert False
    SeleniumApi.step_quit(self)

C--utils文件下我單獨建了一個seleniumapi.py 封裝了一些常用的selenuim操作,如下圖

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class SeleniumApi:
    def step_click(self, xpath):
        self.driver.find_element_by_xpath(xpath).click()

    def step_send_keys(self, xpath, xpath_value):
        self.driver.find_element_by_xpath(xpath).send_keys(xpath_value)

    def step_text_cssselector(self, cssselector):
        result = self.driver.find_element_by_css_selector(cssselector).text
        return result

    def step_text_xpath(self, xpath):
        result = self.driver.find_element_by_xpath(xpath).text

D--environment.py 可以定義一些測試執行前後的操作,如啓動退出瀏覽器,類似於selenium測試裏的setUp和tearDown,後面我的啓動瀏覽器,登陸系統都放在了environment.py 注意函數命名

【知識點】我們系統後臺每次需要輸入賬號和密碼作爲動態驗證,嘗試switchto.alert未果後,使用url直接這樣訪問http://username:password@url即可解決瀏覽器彈窗問題,其他彈窗結局比較簡單,搜索即可查看

# encoding: utf-8
from selenium import webdriver
from features.utils.seleniumapi import SeleniumApi
from features.utils.exceptioncatch import ExceptionCatch
import time
import os
import shutil


def before_feature(content, feature):
    content.driver = webdriver.Chrome()
    content.driver.get("https://tz:[email protected]")
    content.driver.implicitly_wait(10)
    content.driver.maximize_window()
    time.sleep(1)
    SeleniumApi.step_send_keys(content, "//*[@placeholder='郵箱']", '[email protected]')
    SeleniumApi.step_send_keys(content, "//*[@placeholder='密碼']", 'admin')
    SeleniumApi.step_click(content, "//*[@type='button']")
    if '後臺系統' == SeleniumApi.step_text_cssselector(content, ".navBarTitle"):
        assert True
    else:
        ExceptionCatch.catch_exception(content)
        assert False


def after_feature(content, feature):
    content.driver.quit()

E--其他文件如report是後面生成報告的,需要建立,screenshot我後面用到了截圖,非必要文件夾

多場景,多功能,可以繼續添加多個scenario,多個feature文件,feature文件下的given when then 但不是必須條件,可以直接運行 .feature文件,也可以在cmd裏工程目錄下,我的例子就是behave文件夾下執行behave命令就可以看到執行結果。以上,一個簡單的behave demo完成。

 

這是能看到輸出的結果非常的不美觀,也不容易讓其他人瀏覽你的測試結果,於是我找了好久找到了allure可以幫助生成一個html測試報告。

4. 如何產生測試報告

基於mac

$ pip install allure-behave

$ behave -f allure_behave.formatter:AllureFormatter -o report ./features

$ allure serve report

兩點解釋:

1. 第二步是爲了生成JSon報告,在用allure commandline就可以查看html報告

2. 執行時一般會報錯allure command not found, 需要執行以下步驟

How To Fix -bash: allure: command not found, OS - Mac, PyTest, Python, Report By QA COACH

新窗口打開工程,重新執行allure serve report即可生成html report

 

基於windows,未實踐,僅供參考:

1. pip install allure-behave

2. 在工程根目錄下新建一個文件夾reports

3. 根目錄下,cmd > behave -f allure_behave.formatter:AllureFormatter -o D:\path....\reports  features\first_selenium.feature" 在reports文件夾下會產生json報告

4. allure generate --clean -o "E:\path..\reports" "E:\path...\reports-JsonFiles"

第四步還是會遇到allure命令不識別的問題,https://docs.qameta.io/allure/ 在這裏找到windows對應的壓縮包,解壓,將解壓後的bin路徑配置到環境變量裏,再次執行就可以了。

 

~後續可能在優化下,目前在本地配合接口自動化測試去做一些常用功能的UI自動化足夠,測試報告也很好看,還有報錯提示~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章