Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';登录页面';对象没有属性';驱动程序&x27;_Python_Python 2.7_Unit Testing_Selenium Webdriver - Fatal编程技术网

Python 属性错误:';登录页面';对象没有属性';驱动程序&x27;

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

我有一个基础班。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\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框架(
    unittest.main()
    )自动实例化(并运行),并调用setUpClass方法- 它将驱动程序属性添加到LoginPage类,并(自动)添加到其所有实例
  • Company_Management.py:LoginPage由您手动实例化(
    em=login.LoginPage()
    ),但没有调用setUpClass方法,因此LoginPage(或其任何实例)没有驱动程序属性,因此您会出错
    要修复此问题,请自己手动调用该方法,或者:

    • 实例化类(在实例上)后:

      em=login.LoginPage()
      em.setUpClass()
      
    • 在类本身上(更好,在实例化之前)

      login.LoginPage.setUpClass()
      em=login.LoginPage()
      

哪一行抛出错误?请张贴stacktrace。不要在注释中放置代码。请编辑这个问题。
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()