Python 属性错误:';登录页面';对象没有属性';驱动程序&x27;
我有一个基础班。login.py继承基类,运行时没有任何问题。但当我运行Company_Management.py时,它给了我:Python 属性错误:';登录页面';对象没有属性';驱动程序&x27;,python,python-2.7,unit-testing,selenium-webdriver,Python,Python 2.7,Unit Testing,Selenium Webdriver,我有一个基础班。login.py继承基类,运行时没有任何问题。但当我运行Company_Management.py时,它给了我: Traceback (most recent call last): File "/home/sohel/eclipse-workspace/Copell/copell/Company_Management.py", line 22, in test_company em.test_logn() File "/home/sohel/eclipse-w
Traceback (most recent call last):
File "/home/sohel/eclipse-workspace/Copell/copell/Company_Management.py", line 22, in test_company
em.test_logn()
File "/home/sohel/eclipse-workspace/Copell/copell/login.py", line 15, in test_logn
driver =self.driver
AttributeError: 'LoginPage' object has no attribute 'driver'
我想做的是,当我运行Company_Management.py时,它将首先执行test\u logn(self)
方法,然后单击xpath中的2个URL
base.py
login.py
公司管理
错误:test_company(copell.company_Management.CompanyManagement)-------------------------------------------------------------回溯(最近一次调用):文件“/home/sohel/eclipse workspace/copell/copell/company_Management.py”,第22行,在test_company em.test_logn()文件“/home/sohel/eclipse workspace/copell/login.py”中,第15行,在test_logn driver=self.driver AttributeError:“LoginPage”对象没有属性“driver”---------------------------------------------------------------在7.227s中运行了1次测试失败(错误=1)您的两个类都扩展了据 跳过的测试将不会有
setUp()
或tearDown()
围绕它们运行。跳过的类将不会运行setUpClass()
或tearDownClass()
此外,根据:
如果希望调用基类上的setUpClass
和tearDownClass
,则必须自己调用它们
发生了什么:
- login.py:LoginPage由unittest框架(
)自动实例化(并运行),并调用setUpClass方法- 它将驱动程序属性添加到LoginPage类,并(自动)添加到其所有实例unittest.main()
- Company_Management.py:LoginPage由您手动实例化(
),但没有调用setUpClass方法,因此LoginPage(或其任何实例)没有驱动程序属性,因此您会出错em=login.LoginPage()
要修复此问题,请自己手动调用该方法,或者:- 实例化类(在实例上)后:
em=login.LoginPage() em.setUpClass()
- 在类本身上(更好,在实例化之前)
login.LoginPage.setUpClass() em=login.LoginPage()
- 实例化类(在实例上)后:
import unittest
import time
from selenium import webdriver
class Login(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome('/home/sohel/eclipse-workspace/chromedriver')
cls.driver.maximize_window()
cls.driver.get("https:www.car.com/login?back_url=%2F")
time.sleep(3)
@classmethod
def tearDownClass(cls):
cls.driver.close()
if __name__ == '__main__':
unittest.main()
import base
import unittest
import time
class LoginPage(base.Login):
def test_logn(self):
driver =self.driver
driver.find_element_by_id("email").clear()
driver.find_element_by_id("email").send_keys("keya@gmail.com")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("Abcd1234")
driver.find_element_by_xpath("//button[@type='submit']").click()
def test_logout(self):
self.driver.find_element_by_xpath("//li[9]/a/span").click()
if __name__ == '__main__':
unittest.main()
import base
import unittest
import login
import logging
import time
class CompanyManagement(base.Login):
def test_company(self):
driver = self.driver
em = login.LoginPage()
em.test_logn()
driver.find_element_by_xpath("//ec-ui-side-bar/div/div/ul/li[3]/a/span").click()
driver.find_element_by_xpath("//ec-ui-side-bar/div/div/ul/li[3]/ul/li/a/span").click()
time.sleep(3)
if __name__ == '__main__':
unittest.main()