使用Python和Nose實(shí)現(xiàn)移動(dòng)應(yīng)用的自動(dòng)化測(cè)試
采用Appium進(jìn)行自動(dòng)化的功能性測(cè)試最酷的一點(diǎn)是,你可以使用具有最適合你的測(cè)試工具的任何一門語言來寫你的測(cè)試代碼。大家選擇最多的一個(gè)測(cè)試編程語言就是Python。 使用Appium和Python為iOS和Android應(yīng)用編寫測(cè)試代碼非常容易。
在這篇博文中我們將詳細(xì)講解使用Appium下的Python編寫的測(cè)試的例子代碼對(duì)一個(gè)iOS的樣例應(yīng)用進(jìn)行測(cè)試所涉及的各個(gè)步驟,而對(duì)Android應(yīng)用進(jìn)行測(cè)試所需的步驟與此非常類似。
開始,先自https://github.com/appium/appiumfork并clone Appium,然后按照安裝指南,在你的機(jī)器上安裝好Appium。
我還需要安裝Appium的所有依賴并對(duì)樣例apps進(jìn)行編譯。在Appium的工作目錄下運(yùn)行下列命令即可完成此任務(wù):
$ ./reset.sh --ios |
編譯完成后,就可以運(yùn)行下面的命令啟動(dòng)Appium了:
$ grunt appium |
現(xiàn)在,Appium已經(jīng)運(yùn)行起來了,然后就切換當(dāng)前目錄到sample-code/examples/python。接著使用pip命令安裝所有依賴庫(如果不是在虛擬環(huán)境virtualenv之下,你就需要使用sudo命令):
$ pip install -r requirements.txt |
接下來運(yùn)行樣例測(cè)試:
$ nosetests simple.py |
既然安裝完所需軟件并運(yùn)行了測(cè)試代碼,大致了解了Appium的工作過程,現(xiàn)在讓我們進(jìn)一步詳細(xì)看看剛才運(yùn)行的樣例測(cè)試代碼。該測(cè)試先是啟動(dòng)了樣例應(yīng)用,然后在幾個(gè)輸入框中填寫了一些內(nèi)容,最后對(duì)運(yùn)行結(jié)果和所期望的結(jié)果進(jìn)行了比對(duì)。首先,我們創(chuàng)建了測(cè)試類及其setUp方法:
classTestSequenceFunctions(unittest.TestCase): defsetUp(self): app=os.path.join(os.path.dirname(__file__), '../../apps/TestApp/build/Release-iphonesimulator', 'TestApp.app') app=os.path.abspath(app) self.driver=webdriver.Remote( command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities={ 'browserName':'iOS', 'platform':'Mac', 'version':'6.0', 'app': app }) self._values=[] |
“desired_capabilities”參數(shù)用來指定運(yùn)行平臺(tái)(iOS 6.0)以及我們想測(cè)試的應(yīng)用。接下來我們還添加了一個(gè)tearDown方法,在每個(gè)測(cè)試完成后發(fā)送了退出命令:
deftearDown(self): self.driver.quit() |
最后,我們定義了用于填寫form的輔助方法和主測(cè)試方法:
def_populate(self): # populate text fields with two random number elems=self.driver.find_elements_by_tag_name('textField') foreleminelems: rndNum=randint(0,10) elem.send_keys(rndNum) self._values.append(rndNum) deftest_ui_computation(self): # populate text fields with values self._populate() # trigger computation by using the button buttons=self.driver.find_elements_by_tag_name("button") buttons[0].click() # is sum equal ? texts=self.driver.find_elements_by_tag_name("staticText") self.assertEqual(int(texts[0].text),self._values[0]+self._values[1]) |
就是這樣啦!Appium的樣例測(cè)試代碼中還有許多Python的例子。如果你對(duì)使用Nose和Python來運(yùn)行Appium測(cè)試有任何問題或看法,煩請(qǐng)告知。
本文轉(zhuǎn)載自:http://www.oschina.net/translate/automated-mobile-app-testing-with-python-and-nose
posted on 2013-06-04 10:13 順其自然EVO 閱讀(386) 評(píng)論(0) 編輯 收藏