前言
python的BDD框架搪锣,參考這里
除了官方提供的Appium-Python-Client框架,還有一個非官方由網(wǎng)易開源的python客戶端facebook-wda,facebook-wda這個庫的功能非常強大廓奕,可以取代appium飘痛,不過只支持python,Appium支持多種腳本編寫吗跋。
環(huán)境準備
1、安裝python3
brew install python3
2、安裝Appium-Python-Client
pip3 install Appium-Python-Client
3跌宛、安裝Behave
pip3 install behave
4酗宋、查看Behave支持的語言
behave --lang-list
Languages available:
ar: ??????? / Arabic
bg: български / Bulgarian
ca: català / Catalan
cs: ?esky / Czech
cy-GB: Cymraeg / Welsh
da: dansk / Danish
de: Deutsch / German
en: English / English
en-Scouse: Scouse / Scouse
en-au: Australian / Australian
en-lol: LOLCAT / LOLCAT
en-pirate: Pirate / Pirate
en-tx: Texan / Texan
eo: Esperanto / Esperanto
es: espa?ol / Spanish
et: eesti keel / Estonian
fi: suomi / Finnish
fr: fran?ais / French
gl: galego / Galician
he: ????? / Hebrew
hr: hrvatski / Croatian
hu: magyar / Hungarian
id: Bahasa Indonesia / Indonesian
is: íslenska / Icelandic
it: italiano / Italian
ja: 日本語 / Japanese
ko: ??? / Korean
lt: lietuvi? kalba / Lithuanian
lu: L?tzebuergesch / Luxemburgish
lv: latvie?u / Latvian
nl: Nederlands / Dutch
no: norsk / Norwegian
pl: polski / Polish
pt: português / Portuguese
ro: roman? / Romanian
ru: русский / Russian
sk: Slovensky / Slovak
sr-Cyrl: Српски / Serbian
sr-Latn: Srpski (Latinica) / Serbian (Latin)
sv: Svenska / Swedish
tr: Türk?e / Turkish
uk: Укра?нська / Ukrainian
uz: Узбекча / Uzbek
vi: Ti?ng Vi?t / Vietnamese
zh-CN: 簡體中文 / Chinese simplified
zh-TW: 繁體中文 / Chinese traditional
5、查看對應語言的關鍵字
behave --lang-help zh-CN
Translations for Chinese simplified / 簡體中文
And: 而且<
Background: 背景
But: 但是<
Examples: 例子
Feature: 功能
Given: 假如<
Scenario: 場景
Scenario Outline: 場景大綱
Then: 那么<
When: 當<
創(chuàng)建一個iOS測試工程
image.png
寫測試腳本
1秩冈、創(chuàng)建如下目錄結(jié)構
├── app # 待測app
│ └── TestApp.app
└── features
├── calculate.feature # behave待測功能定義
├── environment.py # 環(huán)境配置
└── steps
└── step.py # 測試steps
2本缠、測試求和功能
創(chuàng)建calculate.feature,輸入如下內(nèi)容
#language: zh-CN
功能: 求和
場景: 計算兩個數(shù)相加
假如 第一個值輸入 10
而且 第二個值輸入 20
當 點擊 求和按鈕
那么 結(jié)果應該為30
3入问、配置環(huán)境
創(chuàng)建environment.py丹锹,輸入如下內(nèi)容
# -*- coding: utf-8 -*
import os
from appium import webdriver
def before_feature(context, feature):
app = '/Users/yangfangming/Desktop/TestDemo/app/TestApp.app'
context.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'ios',
'deviceName': 'iPhone 8',
'platformVersion': '11.1',
'bundleId': 'com.yfm.TestApp'
})
def after_feature(context, feature):
context.driver.quit()
4、創(chuàng)建steps
創(chuàng)建step.py芬失,輸入如下內(nèi)容
# -*- coding: utf-8 -*
from behave import *
@given(u'第一個值輸入 10')
def step_impl(context):
el = context.driver.find_element_by_accessibility_id('textfield1')
el.clear()
el.set_value("10")
@given(u'第二個值輸入 20')
def step_impl(context):
el = context.driver.find_element_by_accessibility_id('textfield2')
el.clear()
el.set_value("20")
@when(u'點擊 求和按鈕')
def step_impl(context):
el = context.driver.find_element_by_accessibility_id('sum')
el.click()
@then(u'結(jié)果應該為30')
def step_impl(context):
# el = context.driver.find_element_by_accessibility_id('result')
el = context.driver.find_element_by_class_name('XCUIElementTypeStaticText')
actual = el.get_attribute('value')
print(actual)
assert actual=='30', 'result is 30'
運行測試
behave
# 或者
behave --lang zh-CN
參考
https://github.com/appium/python-client
https://github.com/serhatbolsu/appium-python-bdd/blob/master/testhive/features/steps/steps.py