87692

AttributeError: 'LoginPage' object has no attribute 'driver'

Question:

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'

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.

<h2>base.py</h2> 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() <h2>login.py</h2> 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() <h2>Company_Management.py</h2> 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)

Answer1:

Both your classes extend <a href="https://docs.python.org/2/library/unittest.html#unittest.TestCase" rel="nofollow">[Python]: <em>class</em> unittest.<strong>TestCase</strong>(<em>methodName='runTest'</em>)</a>. <br /> According to <a href="https://docs.python.org/2/library/unittest.html#skipping-tests-and-expected-failures" rel="nofollow">[Python]: Skipping tests and expected failures</a>

<blockquote>

Skipped tests will not have setUp() or tearDown() run around them. Skipped classes will <strong>not</strong> have setUpClass() or tearDownClass() run.

</blockquote>

Also, according to <a href="https://docs.python.org/2/library/unittest.html#setupclass-and-teardownclass" rel="nofollow">[Python]: setUpClass and tearDownClass</a>:

<blockquote>

If you want the setUpClass and tearDownClass on base classes called then <strong>you</strong> must call up to them yourself.

</blockquote>

What happens:

<ul><li><em>login.py</em>: LoginPage is instantiated (and run) automatically by the <em>unittest</em> framework (unittest.main()) and setUpClass method is called - which adds the driver attribute to the LoginPage class - and (automatically, to) all its instances</li> <li>

<em>Company_Management.py</em>: LoginPage is instantiated <em>manually</em> 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. <br />To fix it, manually call the method yourself, either:

<ul><li>

After instantiating the class (on the instance):

em = login.LoginPage() em.setUpClass() </li> <li>

On the class itself (better, before instantiating it)

login.LoginPage.setUpClass() em = login.LoginPage() </li> </ul></li> </ul>

Recommend

  • Dynamically import class by name for static access
  • Django update/create architecture
  • Session key is not modified from Flask tests
  • Instantiate subclass from superclass
  • How do i make the pytest driver instance available in my testcase
  • Haskell exceptions and unit testing
  • ant excluding files
  • How do I interpolate a line number from __LINE__ into the name of a test in Perl?
  • DART - exception in unit testing
  • how to write UI test to test whether image existing when click the first cell in an UITableView?
  • How do I retrieve the text in a table column using Selenium RC?
  • How to initialize global variable at each class invocation?
  • Django how to modify database records by template
  • Failing to get duration of youtube video using xpath
  • Searching an XML file using PHP [closed]
  • Can I put a + sign in a folder with IIS?
  • parsing xml and html page with lxml and requests package in python
  • Mocking a imported function with pytest [duplicate]
  • Open hyperlink on click on an ggplot/plotly chart
  • Symfony2 redirect to https route fails (uses wrong port)
  • Django foreign key drop down
  • Grails calculated field in SQL
  • Keep this build forever option - Jenkins
  • dc-js disable selecting slices on click for pie chart
  • Django rest serializer Breaks when data exists
  • Meteor: Do Something On Email Verification Confirmation
  • How to clear text inside text field when radio button is select
  • Recording logins for password protected directories
  • Is there any way to access browser form field suggestions from JavaScript?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • jquery mobile loadPage not working
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Data Validation Drop Down Box Arrow Disappearing
  • Python: how to group similar lists together in a list of lists?
  • Getting error when using KSoap library to consume .NET web services
  • How do you join a server to an Active Directory (domain)?
  • Busy indicator not showing up in wpf window [duplicate]