[英]AttributeError: 'LoginPage' object has no attribute 'driver'
I have a base class. 我有一个基础课。 Inheriting base class, login.py runs without any problem.
继承基类,login.py可以正常运行。 But when I run Company_Management.py its giving me:
但是当我运行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-workspace/Copell/copell/login.py", line 15, in test_logn
driver =self.driver
AttributeError: 'LoginPage' object has no attribute 'driver'
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. 我想做的是,当我运行Company_Management.py时,它将首先执行
test_logn(self)
方法,然后单击xpath中的2个URL。
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()
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) 错误: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/copell/login.py”,第15行,在test_logn驱动程序= self.driver AttributeError中: 'LoginPage'对象没有属性'driver'---------------------------------------- -----------------------------在7.227秒内失败1次测试(错误= 1)
Both your classes extend [Python 2]: class unittest. 您的两个类都扩展了[Python 2]: 类 unittest。 TestCase ( methodName='runTest' ) .
TestCase ( methodName ='runTest' ) 。
According to [Python 2]: Skipping tests and expected failures 根据[Python 2]:跳过测试和预期的失败
Skipped tests will not have
setUp()
ortearDown()
run around them.跳过的测试不会在它们周围运行
setUp()
或tearDown()
。 Skipped classes will not havesetUpClass()
ortearDownClass()
run.跳过的类将不会运行
setUpClass()
或tearDownClass()
。
Also, according to [Python 2]: setUpClass and tearDownClass : 另外,根据[Python 2]:setUpClass和tearDownClass :
If you want the
setUpClass
andtearDownClass
on base classes called then you must call up to them yourself.如果你想在
setUpClass
和tearDownClass
在基类叫做则必须打电话给他们自己。
What happens: 怎么了:
unittest.main()
) and setUpClass method is called - which adds the driver attribute to the LoginPage class - and (automatically, to) all its instances unittest.main()
和setUpClass方法被调用-这增加了驾驶员属性到LoginPage类-和(自动,于)所有其实例 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. Company_Management.py:LoginPage由您( 手动实例化
em = login.LoginPage()
但setUpClass方法不叫-因此LoginPage(或其任何实例) 没有 司机的属性-因此你的错误。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.