1、先上問題,知道是什麼問題先
1 EE 2 ====================================================================== 3 ERROR: testCalcu (__main__.MyTest) 4 ---------------------------------------------------------------------- 5 Traceback (most recent call last): 6 File "D:WorkSpace3EXAMPLEAppiu_ex est.py", line 28, in testCalcu 7 self.driver.find_element_by_id("com.txt.calculator:id/tv_skip").click() 8 AttributeError: `MyTest` object has no attribute `driver` 9 10 ====================================================================== 11 ERROR: testCalcu (__main__.MyTest) 12 ---------------------------------------------------------------------- 13 Traceback (most recent call last): 14 File "D:WorkSpace3EXAMPLEAppiu_ex est.py", line 25, in tearDown 15 self.driver.quit() 16 AttributeError: `MyTest` object has no attribute `driver` 17 18 ---------------------------------------------------------------------- 19 Ran 1 test in 0.001s 20 21 FAILED (errors=2)
2、再上原始碼,如果你也在5分鐘內未找到該問題原因,呵呵,那就在後續寫程式碼中一定要仔細再仔細
1 # -*- coding:utf-8 -*- 2 from appium import webdriver 3 import unittest 4 5 class MyTest(unittest.TestCase): 6 def setup(self): 7 desired_caps = {} 8 desired_caps[`platformName`] =`Android` 9 desired_caps[`platformVersion`] =`6.0` 10 desired_caps[`deviceName`] =`DIYTHYTCCQBIV47D` 11 desired_caps[`appPackage`] =`com.txt.calculator` 12 desired_caps[`appActivity`] =`.Calculator` 13 desired_caps["unicodeKeyboard"] ="True" 14 desired_caps["resetKeyboard"] ="True" 15 self.driver = webdriver.Remote(`http://localhost:4723/wd/hub`, desired_caps) 16 17 def tearDown(self): 18 self.driver.quit() 19 20 def testCalcu(self): 21 self.driver.find_element_by_id("com.txt.calculator:id/tv_skip").click() 22 self.driver.find_element_by_id("com.txt.calculator:id/digit_8").click() 23 self.driver.find_element_by_id("com.txt.calculator:id/op_add").click() 24 self.driver.find_element_by_id("com.txt.calculator:id/digit_5").click() 25 self.driver.find_element_by_id("com.txt.calculator:id/eq").click() 26 try: 27 result = self.driver.find_element_by_class_name("android.widget.EditText").text 28 self.assertEqual(`13`, result, "The result isn`t right") 29 except Exception as e: 30 print (e) 31 self.fail("The element is wrong or non-exists") 32 33 if __name__ == `__main__`: 34 unittest.main()
3、duang duang duang duang開始計時
~~~~~5分鐘之後,給出出問題的原始碼
class MyTest(unittest.TestCase): def setup(self):
注意setup 此處 up首字要大寫,如未按照unittest框架的要求進行編寫,它自然就不會先進行初使化了,自然也就出現後面的報錯 MyTest例項中沒有driver的屬性
修改正確如下:
class MyTest(unittest.TestCase): def setUp(self):