appium 調(diào)研學(xué)習(xí)

名詞解釋:

appium:是個(gè)web接口服務(wù),接受http請(qǐng)求,可以開啟多個(gè)端口,默認(rèn)4723

appium desktop:內(nèi)嵌了appium,還提供了屬性配置、腳本錄制等功能

Appium Client:appium為其提供服務(wù)端口,他就是請(qǐng)求的發(fā)送者

appium是什么?

appium是自動(dòng)化測(cè)試工具,可以用來測(cè)試iOS和Android平臺(tái)的應(yīng)用、Web應(yīng)用和Web混合應(yīng)用。在測(cè)試中主要用于流程測(cè)試。

他有一個(gè)比較重要的特點(diǎn),支持跨平臺(tái),下面的工作原理會(huì)提到為什么可以跨平臺(tái)的

工作原理

appium選擇了Client/Server的設(shè)計(jì)模式。

工作原理圖

我們使用自己所熟知的語言編寫腳本,通過相應(yīng)語言環(huán)境的Client,發(fā)送http請(qǐng)求到Server端,Server端得到腳本,通過解析,驅(qū)動(dòng)設(shè)備執(zhí)行appium腳本。

1、Client端針對(duì)不同的語言開發(fā)了相應(yīng)的appium庫,那么我們寫腳本就可以根據(jù)自己所熟悉的語言下載自己熟悉的appium庫。我們所熟知的語言有:Ruby、Python、Java、JavaScript (Node.js)、Objective C、PHP、C# (.NET)、RobotFramework。后面附加相應(yīng)語言的客戶端。

2、Server端支持兩大平臺(tái)MAC和Windows。我們針對(duì)不同的平臺(tái)安裝使用。

3、移動(dòng)設(shè)備支持iOS、Android兩大真機(jī)和分別對(duì)應(yīng)的模擬器。

總結(jié):多語言、跨平臺(tái)。appium的腳本支持多語言開發(fā)、同時(shí)可以在不同的平臺(tái)運(yùn)行。

搭建環(huán)境

作為小白,從搭建環(huán)境到用來做一些簡單的測(cè)試,期間遇到各種奇葩問題,繞了很多彎路。在這稍作總結(jié),希望像我一樣的小白們都能有所收獲。

目前我在Mac系統(tǒng)下,使用Python開發(fā)測(cè)試腳本,測(cè)試iOS項(xiàng)目(Android項(xiàng)目目前還有問題。。。)。

Mac系統(tǒng)搭建環(huán)境

Android項(xiàng)目環(huán)境

Node.js,下載地址:https://nodejs.org/en/download/,為什么要裝node.js呢?appium server 是用node.js寫的,安裝node.js可以直接用npm命令安裝appium,或相關(guān) ? ? ? ? ? ?driver、server。

android SDK、jdk

appium

大體安裝過程是這樣的,brew(mac 套件管理器,包含很多實(shí)用工具)->node->npm->appium

命令行運(yùn)行:appium-doctor,看看缺什么就裝上什么

iOS項(xiàng)目環(huán)境

Mac系統(tǒng):10.12.6

xCode開發(fā)工具(命令行Xcode Command Line Tools)大多開發(fā)iOS的同學(xué)環(huán)境應(yīng)該都是正常的

Appium-Server:目前最新的1.6.2版本

按照上面的工作原理圖 “client->server->移動(dòng)設(shè)備”分步講解安裝過程。

appium client

不太清楚明明支持多種語言,為啥Python和Java測(cè)試腳本成為主流。不過前人栽樹后人乘涼,從網(wǎng)上看了下Python這些例子,最終還是選擇了appium client,其中還有一個(gè)最重要的原因,使用appium desktop 可以生成python腳本,試想,作為小白,把生成的腳本拿來簡單一改就可以使用,這應(yīng)該算是入門最快的捷徑了。

廢話不多說,直接上python client地址:https://github.com/appium/python-client,下載完成進(jìn)入到python-clinet目錄執(zhí)行python setup.py install就可以,操作如下

git clone git@github.com:appium/python-client.git

cd python-client

python setup.py install

擴(kuò)展,附贈(zèng)其他腳本語言的client下載地址,安裝大同小異,自行百度

Ruby???????????????????????????https://github.com/appium/ruby_lib

Python????????????????????????https://github.com/appium/python-client

Java ? ? ? ? ? ? ? ? ? ? ? ? ? ??https://github.com/appium/java-client

JavaScript (Node.js)???https://github.com/admc/wd

Objective C??????????????????https://github.com/appium/selenium-objective-c

PHP???????????????????????????????https://github.com/appium/php-client

C# (.NET)??????????????????????https://github.com/appium/appium-dotnet-driver

RobotFramework??????????https://github.com/jollychang/robotframework-appiumlibrary

appium server

appium desktop下載地址

https://github.com/appium/appium-desktop/releases/tag/v1.6.2

下面是Appium Server,地址和端口號(hào)都是默認(rèn)的

appium server

點(diǎn)擊啟動(dòng)Start Server就會(huì)開啟監(jiān)聽,可以看到4723在監(jiān)聽中,之后運(yùn)行腳本的所有日志都會(huì)在這里展示出來。

點(diǎn)擊放大鏡(Start Inspector Session),設(shè)置相應(yīng)的Capabilities,為接入設(shè)備做準(zhǔn)備。下面是我根據(jù)自己設(shè)備和公司項(xiàng)目的配置的Capabilities

Capabilities配置

關(guān)于Capabilities的配置有很多,你想要的都在這里

http://appium.io/docs/en/writing-running-appium/caps/

iOS程序是如何通過server傳遞指令控制界面的???

WebDriverAgent,是實(shí)現(xiàn)自動(dòng)化測(cè)試工作的實(shí)施者(實(shí)施者,我自己起的名)。他可以在iOS平臺(tái)為服務(wù)器實(shí)現(xiàn)控制遠(yuǎn)端設(shè)備。比如啟動(dòng)、殺死程序和界面點(diǎn)擊、拖拽等。通過連接XCTest.framework和調(diào)用蘋果的API直接在設(shè)備上執(zhí)行命令。

下載地址:https://github.com/facebook/WebDriverAgent

下面是我錄制的腳本

#-*- coding: UTF-8 -*-

# This sample code uses the Appium python client

# pip install Appium-Python-Client

# Then you can paste this? into a file and simply run with Python

import time

from appium import webdriver

caps = {}

caps["platformName"] = "iOS"

caps["deviceName"] = "iPhone"

caps["udid"] = "7e0d8f2dcfbcf64612ae93b087d51bff1f431"

caps["noReset"] = "true"

caps["bundleId"] = "com.yz.spacebridge"

caps["platformVersion"] = "11.2"

caps["xcodeOrgId"] = "<2MUJFSGB46>"

caps["xcodeSigningId"] = "iPhone Developer"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

el1=driver.find_element_by_xpath("http://XCUIElementTypeButton[@name=\"登錄\"]")

el1.click()

time.sleep( 5 )

els4 = driver.find_elements_by_name("千喜鶴美食廣場(chǎng)")

els4[0].click()

time.sleep( 5 )

els5 = driver.find_elements_by_name("4dc370c7")

els5[0].click()

el2 = driver.find_element_by_accessibility_id("back")

el2.click()

el3 = driver.find_element_by_accessibility_id("back")

el3.click()

driver.quit()

值得注意的是,我們經(jīng)常會(huì)遇到編碼問題,如上面添加注釋,注意,此注釋一定要放在最頂端否則不起作用

#-*- coding: UTF-8 -*-

appium desktop,基本使用

要操作或者查看某個(gè)(某組)元素,必須事先知道該元素的id或者xpath,通過appium desktop可以很方便的定位獲取頁面元素id 和xpath。另外還有一些基礎(chǔ)的頁面操作,如下圖:

desktop展示圖

縱橫客云小盒收銀接入

好長一段時(shí)間的調(diào)研和學(xué)習(xí),最終都要落實(shí)到工作和項(xiàng)目中,在云小盒收銀,我們會(huì)有所體現(xiàn)。

簡單講下收銀模塊如下:設(shè)置、交易統(tǒng)計(jì)、收款、訂單

云小盒收銀APP主界面

使用appium進(jìn)行測(cè)試,說白了,就是測(cè)試整個(gè)流程通不通,然后按照“登錄->交易統(tǒng)計(jì)->收銀->核對(duì)統(tǒng)計(jì)結(jié)果->退款”這個(gè)主流程一直循環(huán)跑下去。

據(jù)上模塊所講,按照測(cè)試流程按照模塊在測(cè)試工程中依次劃分下面這四個(gè)模塊loginModel(從設(shè)置進(jìn)入,主測(cè)登錄)、cashierModel(收銀)、refundModel(訂單統(tǒng)計(jì)、退款)、statisticsModel(交易統(tǒng)計(jì))。

客戶端模塊架構(gòu)

主模塊:jinyuanbao,統(tǒng)籌管理

threads = []

t1 = threading.Thread(target=runloop)

threads.append(t1)

if __name__ == '__main__':

? ? for t in threads:

? ? ? ? t.start()

總業(yè)務(wù)流程

def task1():

? ? driver = webdriver.Remote("http://localhost:4723/wd/hub", API.desired_caps)

? ? time.sleep(2)

? ? RefundClass().refundAction(driver)

? ? # # 切換賬號(hào)

? ? account = LoginClass().loginAction(driver)

? ? API.user_account = account

? ? # 查看賬單

? ? total_dic = StatisticsClass().statisticsAction(driver)

? ? if account != API.account2:

? ? ? ? # 微信支付

? ? ? ? dic = CashierClass().cashier_weixin_Action(driver)

? ? ? ? # # 對(duì)比交易賬單

? ? ? ? if dic != None:

? ? ? ? ? ? print dic

? ? ? ? if dic != None and total_dic != None:

? ? ? ? ? ? total_dic = StatisticsClass().payment_data_contrast(driver, total_dic, dic)

? ? ? ? else:

? ? ? ? ? ? print total_dic

? ? ? ? ? ? return

? ? ? ? # 退款

? ? ? ? RefundClass().refundAction(driver)

? ? # 支付寶支付

? ? dic = CashierClass().cashier_alipay_Action(driver)

? ? # 對(duì)比交易賬單

? ? if dic != None:

? ? ? ? print dic

? ? if dic != None and total_dic != None:

? ? ? ? total_dic = StatisticsClass().payment_data_contrast(driver, total_dic, dic)

? ? else:

? ? ? ? print total_dic

? ? ? ? return

? ? # 退款

? ?RefundClass().refundAction(driver)

通用模塊 basemethod

主要實(shí)現(xiàn)如下功能:

1、本地記錄日志

def report_log(self,content):

? ? ? ? date_current = time.strftime("%Y-%m-%d", time.localtime())

? ? ? ? time_current = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

? ? ? ? log_name = 'log/'+date_current+'.txt'

? ? ? ? logfile = open(log_name, 'a+')

? ? ? ? logfile.write('\n')

? ? ? ? logfile.write('時(shí)間:'+time_current+'===')

? ? ? ? logfile.write(content)

? ? ? ? logfile.close()

2、通過id查找元素

def get_id(self,id,driver):

? ? ? ? try:

? ? ? ? ? ? element = WebDriverWait(driver,2).until(lambda driver: driver.find_element_by_id(id))

? ? ? ? ? ? return element

? ? ? ? except:

? ? ? ? ? ? print '未定位到元素:'+id

? ? ? ? ? ? return None

3、通過xpath查找元素

def get_xpath(self,xpath,driver):

? ? ? ? try:

? ? ? ? ? ? element = WebDriverWait(driver,2).until(lambda driver: driver.find_element_by_xpath(xpath))

? ? ? ? ? ? return element

? ? ? ? except:

? ? ? ? ? ? print '未定位到元素:'+xpath

? ? ? ? ? ? return None

4、釘釘webhook服務(wù)上報(bào)

def dingtalk(self,text):

? ? ? ? self.report_log(text)

? ? ? ? webhook = kwebhook

? ? ? ? xiaoding = DingtalkChatbot(webhook)

? ? ? ? content = text

? ? ? ? xiaoding.send_text(msg=content,is_at_all=True)

5、釘釘webhook error上報(bào)

def ding_talk_error(self,text):

? ? ? ? self.report_log(text)

? ? ? ? webhook = kwebhook

? ? ? ? xiaoding = DingtalkChatbot(webhook)

? ? ? ? content = '收銀賬號(hào):' + API.user_account + '\n' + text

? ? ? ? xiaoding.send_text(msg=content, is_at_all=True)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容