我有一个基类。继承基类,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-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个网址。
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()
错误:test_company(copell.Company_Management.CompanyManagement)------------------------------------- --------------------------------- Traceback(最近一次调用最后一次):File" / 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 driver = self.driver中AttributeError:' LoginPage'对象没有属性' driver' -------------------------------------------------- -------------------在7.227s中进行1次测试失败(错误= 1)
答案 0 :(得分:1)
你的课程都延伸[Python 2]: class unittest.TestCase(methodName='runTest')。
根据{{3}}
跳过的测试不会让
setUp()
或tearDown()
绕过它们。跳过的课程将不让setUpClass()
或tearDownClass()
运行。
另外,根据[Python 2]: Skipping tests and expected failures:
如果你希望
setUpClass
和tearDownClass
在基类上调用你必须自己调用它们。
会发生什么:
unittest.main()
)和 setUpClass 方法被调用 -
它将驱动程序属性添加到 LoginPage 类 - 以及(自动地)到其所有实例 Company_Management.py : LoginPage 由您(em = login.LoginPage()
)实例化手动,但 setUpClass 方法未被调用 - 因此 LoginPage (或其任何实例)没有 驱动程序属性 - 因此你的错误。
要解决此问题,请自行手动调用方法:
实例化类(在实例上)后:
em = login.LoginPage()
em.setUpClass()
关于类本身(更好,在实例化之前)
login.LoginPage.setUpClass()
em = login.LoginPage()