AttributeError:'LoginPage'对象没有属性'driver' [英] AttributeError: 'LoginPage' object has no attribute 'driver'
问题描述
我有一个基础课.继承基类,login.py可以正常运行.但是当我运行Company_Management.py时,它给了我:
I have a base class. Inheriting base class, login.py runs without any problem. But when I run Company_Management.py its giving me:
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_logn(self)
方法,然后单击xpath中的2个URL.
What I am trying to do is that, when I will run Company_Management.py it will excute test_logn(self)
method first then will click on 2 urls from xpath.
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()
login.py
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()
Company_Management.py
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()
错误:test_company(copell.Company_Management.CompanyManagement)------------------------------------- ---------------------------------追溯(最近一次通话最后一次):文件"/home/sohel/eclipse- test_company em.test_logn()中第22行的工作空间/Copell/copell/Company_Management.py"文件,在test_logn驱动程序= self中,文件"/home/sohel/eclipse-workspace/Copell/copell/login.py",第15行.驱动程序AttributeError:'LoginPage'对象没有属性'驱动程序'------------------------------------- --------------------------------在7.227秒内失败1次测试(错误= 1)
ERROR: test_company (copell.Company_Management.CompanyManagement) ---------------------------------------------------------------------- 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' --------------------------------------------------------------------- Ran 1 test in 7.227s FAILED (errors=1)
推荐答案
两个类都扩展了 [Python 2]: class 单元测试. TestCase ( methodName ='runTest').
根据 [Python 2]:跳过测试和预期的失败
Both your classes extend [Python 2]: class unittest.TestCase(methodName='runTest').
According to [Python 2]: Skipping tests and expected failures
跳过的测试不会在它们周围运行
setUp()
或tearDown()
.跳过的课程将不运行setUpClass()
或tearDownClass()
.
Skipped tests will not have
setUp()
ortearDown()
run around them. Skipped classes will not havesetUpClass()
ortearDownClass()
run.
此外,根据 [Python 2]:setUpClass和tearDownClass :
如果要在基类上调用
setUpClass
和tearDownClass
,则您必须自己调用它们.
If you want the
setUpClass
andtearDownClass
on base classes called then you must call up to them yourself.
会发生什么:
- login.py : LoginPage 由 unittest 框架(
unittest.main()
)和 setUpClass自动实例化(并运行) 方法称为- 将 driver 属性添加到 LoginPage 类-并(自动)添加到其所有实例 -
Company_Management.py :手动登录 LoginPage ,但 setUpClass 方法未调用-因此 LoginPage (或其任何实例)没有 driver 属性-因此,您的错误.
要解决此问题,请自行手动调用该方法,或者:
- login.py: LoginPage is instantiated (and run) automatically by the unittest framework (
unittest.main()
) and setUpClass method is called - which adds the driver attribute to the LoginPage class - and (automatically, to) all its instances Company_Management.py: LoginPage is instantiated manually by you (
em = login.LoginPage()
), but the setUpClass method isn't called - and thus LoginPage (or any of its instances) doesn't have the driver attribute - hence your error.
To fix it, manually call the method yourself, either:
-
实例化类后(在实例上):
After instantiating the class (on the instance):
em = login.LoginPage()
em.setUpClass()
关于类本身(最好在实例化之前)
On the class itself (better, before instantiating it)
login.LoginPage.setUpClass()
em = login.LoginPage()
这篇关于AttributeError:'LoginPage'对象没有属性'driver'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!