1

content of test_homepage.py

    def test_insurance_pages_open_successfully_using_fixtures(page_object, load_home_page, insurance_data):
            page_object.open_insurance(insurance_data)
            assert page_object.ui.contains_text('Buying two-wheeler insurance from Coverfox is simple')

open_insurance function in page object home_page.py

    def open_insurance(self, insurance):
        self._ui.move_to(locators.drp_dwn_insurance)
        self._ui.click(format_locator(locators.lnk_insurance, insurance))

move_to function in another file.py

    def move_to(self, locator):
        to_element = self.find_element(locator)
        print("element value", to_element)
        self.action.move_to_element(to_element).perform()

What I am trying to over here is test_insurance_pages_open_successfully_using_fixtures takes 3 fixtures as arguments 1.

page_object that provides a page object at a session-level 2.

load_home_page to load the home page again at session-level 3.

insurance_data fixture in conftest.py which suppliers list of link texts read from some CSV file

So, in essence, it will load the page and open all links one by one for website - https://www.coverfox.com/

First test case passes for link Two-wheelers insurance but for 2nd test data run it fails giving an exception of stale element reference on the point where it is trying to move to(move_to function) insurance link again.

I am not storing elements anywhere and function is written in a way that it will find the element again.

What is causing this? Or Pytest does some sort of element caching in the background

Max Voitko
  • 1,542
  • 1
  • 17
  • 32

1 Answers1

0

It seems that you should use function-level fixture for load_home_page or refresh the page after you have done some actions.

In the current approach (at least how you described it) you are using the same page and page state for different tests.

Could you please share the fixtures code as well?

Max Voitko
  • 1,542
  • 1
  • 17
  • 32
  • I have tried both ways but it did not work. In one I made load_home_page as a function leve fixture. In another attempt I did it module level only. Even the way function has been written it is doing a fresh find element with in the function body, ideally exception should not appear – sourabh dhingra Nov 12 '19 at 09:52
  • Yes, please find the exception E selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document E (Session info: chrome=78.0.3904.97) E (Driver info: chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Windows NT 10.0.18362 x86_64) ..\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py:192: StaleElementReferenceException =========================================================== 1 failed, 1 passed, 3 skipped in 29.13 s – sourabh dhingra Nov 12 '19 at 09:54
  • fixtures code - @pytest.fixture(params=load_test_data("insurance_categories")) def insurance_data(request): data = request.param return data – sourabh dhingra Nov 13 '19 at 08:54