简体   繁体   English

AttributeError:'LoginPage'对象没有属性'driver'

[英]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。

base.py base.py

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 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 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()

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' ) . TestCasemethodName ='runTest'
According to [Python 2]: Skipping tests and expected failures 根据[Python 2]:跳过测试和预期的失败

Skipped tests will not have setUp() or tearDown() run around them. 跳过的测试不会在它们周围运行setUp()tearDown() Skipped classes will not have setUpClass() or tearDownClass() run. 跳过的类将不会运行setUpClass()tearDownClass()

Also, according to [Python 2]: setUpClass and tearDownClass : 另外,根据[Python 2]:setUpClass和tearDownClass

If you want the setUpClass and tearDownClass on base classes called then you must call up to them yourself. 如果你想在setUpClasstearDownClass在基类叫做必须打电话给他们自己。

What happens: 怎么了:

  • 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 login.py:LoginPage单元测试框架自动实例化(并运行)( 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM