以前在 testerhome 上面看到一篇文章:
2015 年 3 月 新秀群作业-- Appium+Python+PO 设计模式 入门笔记(https://testerhome.com/topics/2226PageObject 的模式来写 app 的自动化用例,使用的是 appium+python。)自己也照着这种
自己把 driver 封装在了 AppiumTest 类里面,同时把登录模块也进行封装 LoginAction,运行 test_case 时,提示报错 'WebDriver' object has no attribute 'driver'

代码:
test_login.py

class case_login(unittest.TestCase):

    def setUp(self):
        self.driver = AppiumTest().get_driver()
        print("开始执行登录用例")


    def tearDown(self):
        self.driver.quit()

    def test_clear_login(self):
        LoginAction.Login(self.driver,"","")
        self.assertIsNone(obj=LoginPage.submitForget_loc,msg="用户名为空,登录失败")

if __name__ == "__main__":
    unittest.main(case_login)

LoginAction.py

class LoginAction(object):

    def __init__(self):
        print("login...")

    @staticmethod
    def Login(driver,username,password):
        LoginDriver = LoginPage.Login(driver)
        LoginDriver.userNameObj().send_keys(username)
        LoginDriver.passWordObj().send_keys(password)
        LoginDriver.LoginButtonObj().click()

LoginPage.py

#用户名
username_loc = (By.ID,(ParseConfigurationFile.ParseCofigFile().getOptionValue('login','username')))
# 密码
password_loc = (By.ID,(ParseConfigurationFile.ParseCofigFile().getOptionValue('login','password')))
# 登录按钮
submitLogin_loc = (By.NAME,(ParseConfigurationFile.ParseCofigFile().getOptionValue('login','submitLogin')))
# 忘记密码按钮
submitForget_loc = (By.NAME,(ParseConfigurationFile.ParseCofigFile().getOptionValue('login','submitForget')))

class Login(object):


    def __init__(self,driver):
        self.driver = AppiumTest.get_driver(driver)

    def userNameObj(self):
        elementObj =Base.find_element(self.driver,username_loc)
        return elementObj

    def passWordObj(self):
        elementObj = Base.find_element(self.driver,password_loc)
        return elementObj

    def LoginButtonObj(self):
        elementObj = Base.find_element(self.driver,submitLogin_loc)
        return elementObj

    def ForgetButtonObj(self):
        elementObj = Base.find_element(self.driver,submitForget_loc)
        return  elementObj

AppiumTest.py

from appium import webdriver
class AppiumTest:
    def __init__(self):

        capabilities = {'platformName': 'Android',
                        'platformVersion': '4.4.2',
                        'deviceName': 'd779f807',  # 通过CMD adb devices获得
                        'app': 'F://AppiumTest//DianJianbao_2017-10-18_1.2.4_official_.apk',# aapt dump badging 电脑中apk的绝对路径
                        'appPackage': 'com.gzkit.dianjianbao',# 导入的APP包名
                        'appActivity': 'com.gzkit.dianjianbao.module.login.LoginActivity',
                        'unicodeKeyboard': True,  # 支持中文输入,会自动安装Unicode 输入法。默认值为 false
                        'resetKeyboard': True  # 在设定了 unicodeKeyboard 关键字的 Unicode 测试结束后,重置输入法到原有状态
                        }
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', capabilities)
        self.driver.implicitly_wait(30)

    def get_driver(self):
        return self.driver

困扰了一段时间 仍未解决,自己基础不足,恳请大神们指点一下非常感谢。

最后贴上报错信息 orz

Error
Traceback (most recent call last):
  File "C:\Users\zhenggor\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Users\zhenggor\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run
    testMethod()
  File "C:\Users\zhenggor\Desktop\JianlibaoUnittest1\TestScripts\test_login.py", line 19, in test_01_clear_login
    LoginAction.Login(self.driver,"","")
  File "C:\Users\zhenggor\Desktop\JianlibaoUnittest1\appModules\LoginAction.py", line 12, in Login
    LoginDriver = LoginPage.Login(driver)
  File "C:\Users\zhenggor\Desktop\JianlibaoUnittest1\PageObjects\LoginPage.py", line 20, in __init__
    self.driver = AppiumTest.get_driver(driver)
  File "C:\Users\zhenggor\Desktop\JianlibaoUnittest1\util\AppiumTest.py", line 22, in get_driver
    return self.driver
AttributeError: 'WebDriver' object has no attribute 'driver'


↙↙↙阅读原文可查看相关链接,并与作者交流