Skip to content

Improvements to the Deferred Assert system #1065

@mdmintz

Description

@mdmintz

SeleniumBase deferred asserts allow you to make multiple assertions on the same page without failing the test after the first failed assert. Instead, you can choose when to process those assertions by calling: self.process_deferred_asserts().

A few improvements are needed.

  • The spacing needs to be updated. (The current results INCORRECTLY group the "CHECK" with the previous assertion in the console output):
Exception: 
E       *** DEFERRED ASSERTION FAILURES FROM: examples.test_deferred_asserts.DeferredAssertTests.test_deferred_asserts
E       CHECK #2: (https://xkcd.com/993/)
E                
E        Element {img[alt="Rocket Ship"]} was not present after 1 second!
E       CHECK #4: (https://xkcd.com/993/)
E                
E        Expected text {Fake Item} for {#middleContainer} was not visible after 1 second!
E       CHECK #6: (https://xkcd.com/993/)
E                
E        Element {a[name="Super Fake !!!"]} was not present after 1 second!

That should look more like this, where the grouping is done CORRECTLY:

Exception: 
E       ***** DEFERRED ASSERTION FAILURES:
E       TEST: examples.test_deferred_asserts.DeferredAssertTests.test_deferred_asserts
E               
E       CHECK #2: (https://xkcd.com/993/) 
E        Element {img[alt="Rocket Ship"]} was not present after 1 second!
E               
E       CHECK #4: (https://xkcd.com/993/) 
E        Expected text {Fake Item} for {#middleContainer} was not visible after 1 second!
E               
E       CHECK #6: (https://xkcd.com/993/) 
E        Element {a[name="Super Fake !!!"]} was not present after 1 second!
  • Additionally, there should be a deferred_assert_exact_text() method, especially since the Recorder is making use of assert_exact_text(). That way it would be easy to swap a regular assert with a deferred assert.

After the work for this is completed, the new example test for this should look like:

import pytest
from seleniumbase import BaseCase

class DeferredAssertTests(BaseCase):
    @pytest.mark.expected_failure
    def test_deferred_asserts(self):
        self.open("https://xkcd.com/993/")
        self.wait_for_element("#comic")
        print("\n(This test should fail)")
        self.deferred_assert_element('img[alt="Brand Identity"]')
        self.deferred_assert_element('img[alt="Rocket Ship"]')  # Will Fail
        self.deferred_assert_element("#comicmap")
        self.deferred_assert_text("Fake Item", "#middleContainer")  # Will Fail
        self.deferred_assert_text("Random", "#middleContainer")
        self.deferred_assert_element('a[name="Super Fake !!!"]')  # Will Fail
        self.deferred_assert_exact_text("Brand Identity", "#ctitle")
        self.deferred_assert_exact_text("Fake Food", "#comic")  # Will Fail
        self.process_deferred_asserts()

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions