Behave+Python 行為驅動測試開發用例設計
阿新 • • 發佈:2018-12-10
首先使用pip install behave
安裝behave包,官方文件請參考。從官網中瞭解到,實現一個基本的behave執行,至少需要構建如下測試目錄:
features/
features/everything.feature
features/steps/
features/steps/steps.py
即我們需要新建features資料夾,在該資料夾下要包含至少一個.feature檔案和steps資料夾,steps下包含對應執行的測試用例.py檔案。我測試的場景是百度登入,即登入百度首頁->點選登入->輸入使用者名稱/密碼提交->檢查登入狀態。場景描述檔案寫在.feature檔案裡,這類檔案可由測試、產品或任何需求相關人員完成,使用自然語言描述,如下:
Feature:i want to login on baidu
Scenario Outline: Login
Given I am on baidu homepage
When I click login button
Then I should see login page
When I enter <username> and <password> and submit
Then I should see user status
Examples: userinfo
|username |password|
|1 ****** |********|
Feature用來描述此次測試,Scenario Outlines和Examples結合使用,類似於資料驅動,適用於多次重複的測試,即作為一個基本引數化的場景模板,場景大綱生成多個場景,每個場景代表一個示例/行組合。我這裡使用的例子體現的不好,可以結合官網的例子理解:
Feature:
Scenario Outline: Wow # line 2
Given an employee "<name>"
Examples: Araxas
| name | birthyear |
| Alice | 1985 | # line 7
| Bob | 1975 | # line 8
其等同於:
Scenario Outline: Wow # features/xxx.feature:2
Given an employee "Alice"
Scenario Outline: Wow # features/xxx.feature:2
Given an employee "Bob"
要注意,引數化的資料使用<>,裡面的引數名要和Examples裡的一致。 Given,When,Then描述了測試場景,Given表示執行的前提條件,When執行操作,Then預期結果。 然後在steps資料夾下建立.py檔案編寫測試用例:
#encoding: utf-8
from behave import *
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.action_chains import ActionChains
import time
@given('I am on baidu homepage')
def step_on_baidu_homepage(context):
context.driver.get('https://www.baidu.com/')
@when('I click login button')
def step_click_login_button(context):
time.sleep(3)
login_btn = context.driver.find_element_by_link_text('登入')
login_btn.click()
time.sleep(4)
@then('I should see login page')
def step_see_login_page(context):
name_login = context.driver.find_element_by_xpath("//p[@title='使用者名稱登入']")
name_login.click()
time.sleep(2)
@when('I enter {username} and {password} and submit')
def step_enter_user_info(context,username,password):
user = context.driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_10__userName']")
user.send_keys(username)
pw = context.driver.find_element_by_xpath("//input[@name='password']")
pw.send_keys(password)
time.sleep(3)
btn = context.driver.find_element_by_xpath("//input[@type='submit']")
ActionChains(context.driver).move_to_element(btn).click(btn).perform()
@then('I should see user status')
def step_see_user_status(context):
user_status = WebDriverWait(context.driver,10).until(expected_conditions.visibility_of_element_located((By.ID,'s_username_top')))
user_status_info = user_status.text
print(user_status_info)
if user_status_info[:3] =='1**':
assert True
可以看出對應的每一個Given,When,Then場景都有一個step_方法來實現,這裡引數context我理解的是在各steps間進行連線、傳遞的物件。同時,這裡要注意的坑有:
- 傳遞的引數資料使用{}來指示,同時引數名和元素名不要重名;
- 點選登入時遇到了報錯‘ElementClickInterceptedException’,解決方法可參考stackoverflow
- 百度登入時會有圖片驗證碼進行驗證,這裡我採取的方法是延長等待時間手動輸入,還可以採用新增cookie的方法,先獲得登入後的cookie,確定登入欄位,然後
driver.add_cookie(鍵名)
,還有一些別的方法可以參考。 類似於setUp()和tearDown(),我們可以新增在測試執行前/後的一些操作,如啟動/退出瀏覽器,這類操作寫在features下的environment.py檔案中:
#encoding: utf-8
from selenium import webdriver
def before_all(context):
profile_dir = r'C:\Users\Firefox\Profiles\iewanxoz.default'
profile = webdriver.FirefoxProfile(profile_dir)
context.driver = webdriver.Firefox(profile)
context.driver.implicitly_wait(30)
context.driver.maximize_window()
def after_all(context):
context.driver.quit()
這樣就完成了一個簡單的behave測試,對於複雜的可以新增多個GivenWhenThen語句,或者多個Scenario,或者多個feature檔案,執行時在cmd視窗features資料夾上級目錄,輸入behave即可,得到測試結果如下:
新手學習,歡迎指教!