-1

When I run my script it gives me this error,

CONSOLE ERROR :

"C:\Users\LENOVO L540\PycharmProjects\easyNeasypytest\venv\Scripts\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.2\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path "C:/Users/LENOVO L540/PycharmProjects/easyNeasypytest"
Testing started at 3:39 PM ...
Launching pytest with arguments C:/Users/LENOVO L540/PycharmProjects/easyNeasypytest in C:\Users\LENOVO L540\PycharmProjects\easyNeasypytest

============================= test session starts =============================
platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- C:\Users\LENOVO L540\PycharmProjects\easyNeasypytest\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\LENOVO L540\PycharmProjects\easyNeasypytest
collecting ... collected 1 item

tests/test_e2e.py::TestOne::test_e2e FAILED                              [100%]
tests\test_e2e.py:13 (TestOne.test_e2e)
self = <tests.test_e2e.TestOne object at 0x000001BB1A4A5C40>

    def test_e2e(self):
>       action = ActionChains(self.driver)
E       AttributeError: 'TestOne' object has no attribute 'driver'

tests\test_e2e.py:15: AttributeError


Assertion failed


Assertion failed


================================== FAILURES ===================================
______________________________ TestOne.test_e2e _______________________________

self = <tests.test_e2e.TestOne object at 0x000001BB1A4A5C40>

    def test_e2e(self):
>       action = ActionChains(self.driver)
E       AttributeError: 'TestOne' object has no attribute 'driver'

tests\test_e2e.py:15: AttributeError
============================== warnings summary ===============================
utilities\BaseClass.py:4
  C:\Users\LENOVO L540\PycharmProjects\easyNeasypytest\utilities\BaseClass.py:4: PytestUnknownMarkWarning: Unknown pytest.mark.usefixture - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.usefixture("setup")

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED tests/test_e2e.py::TestOne::test_e2e - AttributeError: 'TestOne' objec...
======================== 1 failed, 1 warning in 0.15s =========================

Process finished with exit code 1


Assertion failed

Assertion failed

test_e2e.py

import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

from utilities.BaseClass import BaseClass


class TestOne(BaseClass):

    def test_e2e(self):
        action = ActionChains(self.driver)
        menu = self.driver.find_element_by_css_selector("li[class='list-inline-item cart-btn drop mr-0'] a[class='btn btn-link border-none']")
        time.sleep(2)
        action.move_to_element(menu).perform()
        childmenu = self.driver.find_element_by_xpath("//a[contains(text(),'Login')]")
        action.move_to_element(childmenu).click().perform()
        self.driver.find_element_by_id("email").send_keys("saniaaslam987@gmail.com")
        self.driver.find_element_by_name("password").send_keys("123456")
        self.driver.find_element_by_tag_name("button").click()
        print(self.driver.find_element_by_xpath("//div[contains(text(),'Logged In Successfully.')]").text)
        time.sleep(3)
        print("***********************Scrolled down starts***************")
        for i in range(10):
            time.sleep(0.5)
            self.driver.execute_script(" window.scrollBy(0, 350);")
            print("scrolling time: " + str(i))
        print("*************************scrolling down end**********************")
        print("**************************************Scrolled up starts****************")
        for i in range(10):
            time.sleep(0.5)
            self.driver.execute_script(" window.scrollBy(0,-350);")
            print("scrolling time: - " + str(i))
        print("**************************************Scrolled up End ****************")
        time.sleep(2)
        self.driver.find_element_by_xpath("//input[@name='keyword']").send_keys("product")
        self.driver.find_element_by_css_selector("[class*='search-form__submit form-btn form-btn--2']").click()
        time.sleep(2)
        self.driver.find_element_by_xpath("//h5[contains(text(),'Product Dummy 7')]").click()
        self.driver.execute_script("window.scrollTo(0, 200)")
        self.driver.find_element_by_class_name("checkmark").click()
        self.driver.find_element_by_css_selector("button[class='btn btn-secondary btn-lg font-ubuntu']").click()
        self.driver.find_element_by_xpath("//a[@href='https://aujtravels.com/easyneasy/cart']//img").click()
        time.sleep(1)
        self.driver.find_element_by_xpath(
            "//label[contains(@class,'checkbox-design m-auto p-0 d-inline-block m-checkbox')]//span[contains(@class,'checkmark')]").click()
        time.sleep(1)
        self.driver.find_element_by_css_selector("#plusButtonId5").click()
        time.sleep(1)
        print(self.driver.find_element_by_xpath("//div[contains(text(),'Cart updated successfully')]").text)
        time.sleep(3)
        self.driver.find_element_by_css_selector("#proceed_btn").click()
        time.sleep(1)
        self.driver.find_element_by_xpath("//a[contains(text(),'+ Add New Address')]").click()
        time.sleep(2)
        self.driver.find_element_by_xpath("//a[contains(text(),'Add New Address')]").click()
        time.sleep(2)
        self.driver.find_element_by_css_selector("#name").send_keys("Arsal")
        time.sleep(2)
        self.driver.find_element_by_id("phone").send_keys("12345678901")
        time.sleep(2)
        self. driver.find_element_by_id("address").send_keys("8")
        self.driver.find_element_by_xpath("//input[@id='street']").send_keys("ghang Road ")
        cities = Select(self.driver.find_element_by_css_selector("#city_id"))
        cities.select_by_visible_text('Karachi')
        area = Select(self.driver.find_element_by_xpath("//select[@id='district_id']"))
        time.sleep(2)
        area.select_by_index(1)
        self.driver.find_element_by_css_selector("#zip_code").send_keys("39350")
        self.driver.find_element_by_xpath("//label[@for='shipping_address']").click()
        self.driver.find_element_by_css_selector("#add_address_btn").click()
        # print(driver.find_element_by_xpath("//div[contains(text(),'Address added successfully')]").text)
        self.driver.get_screenshot_as_file("screen.png")
        next = self.driver.find_element_by_link_text("Payment Methods")
        self.driver.execute_script("arguments[0].click()", next)
        time.sleep(1)
        self.driver.find_element_by_xpath("//a[contains(text(),'Delete')]").click()
        alert = self.driver.switch_to.alert
        print(alert.text)
        alert.accept()
        time.sleep(2)
        self.driver.find_element_by_link_text("Add Payment Method").click()
        time.sleep(2)
        dropdown = Select(self.driver.find_element_by_xpath("//select[@id='payment_method']"))
        dropdown.select_by_visible_text('Debit')
        self.driver.find_element_by_xpath("//input[@id='name']").send_keys("Arsal")
        self.driver.find_element_by_id("card_digit").send_keys("1234")
        self.driver.find_element_by_name("expiry").send_keys("07/25")
        self.driver.find_element_by_xpath("//input[@id='add_payment_btn']").click()
        time.sleep(1)
        self.driver.refresh()
        self.driver.find_element_by_xpath("//a[contains(text(),'Account Settings')]").click()
        time.sleep(1)

        self.driver.find_element_by_xpath("//a[contains(text(),'Edit')]").click()
        time.sleep(1)
        # driver.switch_to.frame("automation_1")
        self.driver.find_element_by_name("tax_number").send_keys("123456789")
        time.sleep(2)
        self.driver.find_element_by_id("profile_image").send_keys("C:\\Users/LENOVO L540/Downloads/bird.jpg")
        self.driver.find_element_by_xpath("//input[@value='Update']").click()
        print("*************************************Profile Updated*******************")

Conftest.py

import pytest
from selenium import webdriver


@pytest.fixture(scope="class")
def setup(request):
    driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
    driver.implicitly_wait(10)
    driver.get("https://aujtravels.com/easyneasy/")
    driver.maximize_window()
    request.cls.driver = driver
    yield
    driver.close()

BaseClass.py

import pytest


@pytest.mark.usefixture("setup")
class BaseClass:****
    pass

I'm not able to understand what this error is and how to fix it. Please help me with it.

1
self.driver

means driver of this object and your code cannot find any variable called driver for that object instance.

There is no instance variable driver:

def test_e2e(self):
    action = ActionChains(self.driver)

do something like (if baseclass has a driver class variable)

from utilities.BaseClass import BaseClass 

     driver = BaseClass.driver

This is a really good article to understand python inheritance basics:

https://www.tutorialspoint.com/python/python_classes_objects

you should call init

2
  • The link seems to be broken . – Mate Mrše Feb 22 at 12:53
  • 1
    @MateMrše Thanks updated it – PDHide Feb 22 at 12:59
0

One way to bypass this issue is the use of request in test parameter.ie:

def test_e2e(self, request):
self.driver = request.cls.driver
...

This way you can execute the tests. Note: the import should be better if provided from the code root location i.e

from utilities.BaseClass import BaseClass >>> from src.utilities.BaseClass import BaseClass
-1

Try to run your code using py. test "filename" not with the "run button"

else you will get this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.