TestProject Forum

Python Not command failing test

Hi,

I wondered if you could help with an issue I am getting with one of my assertions:

Currently, I have a test that logs a user type onto the system and then checks to see if the buttons displayed are all there. I do this by using my own command isElementPresent which then links to the selenium driver.find_element .

def verifySysAdminButtonsAreDisplayed(self):
result = self.isElementPresent(self._timesheets_nav, locatorType=“link”)
result = self.isElementPresent(self._awaiting_pay_bill_nav, locatorType=“link”) and result
result = self.isElementPresent(self._payments_nav, locatorType=“link”) and result
return result

If the elements are on the page then true is passed back to the test and this is then asserted against with assert result == True. So far so good…

The issue I am having is that for the reverse of this check - i.e. checking that buttons are NOT displayed. For this I am using the same check of isElementPresent but adding a “not” in front of the check so it still gives me a True response if the element is not displayed. This way I can still assert that against the same result.i.e. == True

def verifySysAdminButtonsAreNotDisplayed(self):
result = not self.isElementPresent(self._my_payments_button, locatorType=“link”)
result = (not self.isElementPresent(self._my_holiday_button, locatorType=“link”)) and result
result = (not self.isElementPresent(self._my_compliances_button, locatorType=“link”)) and result
result = (not self.isElementPresent(self._my_invoices_button, locatorType=“link”)) and result
return result

If I run this test locally then my tests all pass but if I check the Test Project Reports then these items are failing. I assume that this is because it is failing to find the elements and so reporting a failure even though the Assert is passing.

Hello @marklane2001

Can you please share the report of the test?
As well as a simple scenario that reproduces the issue, If possible.

Hi

I have attached my screenshot of the failing elements below. Basically, these x4 failures should not find the elements as they should not exist for this user type. I have an assert method on this page object which reads:

def verifySysAdminButtonsAreNotDisplayed(self):
result = not self.isElementPresent(self._my_payments_button, locatorType=“link”)
result = (not self.isElementPresent(self._my_holiday_button, locatorType=“link”)) and result
result = (not self.isElementPresent(self._my_compliances_button, locatorType=“link”)) and result
result = (not self.isElementPresent(self._my_invoices_button, locatorType=“link”)) and result
return result

And I just pass back True these elements are not found

This assert method uses a Selenium Class for the IsElementPresent which in effect just uses the self.driver.find_element(byType, locator) to locate the element. This will return false as the element does not exist which in turn will return true for the Assert as Not has been found.

Hello @marklane2001
Please try adding @report_asserttion_errors annotation above the test methods.

Hi,

Thanks for your reply.

I do already have them on the test if that’s what you mean?

@report_assertion_errors
@pytest.mark.order(4)
def test_validAdminLogin(self):
self.lp.clearUserAndPasswordFields()
self.lp.login(“user”, “password”)
result1 = self.lp.verifyLoginTitle()
self.ts.mark(result1, “Title Verified”)
result2 = self.hp.verifySysAdminButtonsAreDisplayed()
assert result2 == True
self.ts.mark(result2, “Sys Admin Buttons are displayed correctly”)
result3 = self.hp.verifySysAdminButtonsAreNotDisplayed()
assert result3 == True

Thanks, @marklane2001

So basically, what you are seeing here, is not that the inverted result of the assert is failing the test, but the find element command it self is failing the test since the element is not found.

Before it even reaches the part of the assertion and the not condition, a fail message is propagated from the failing driver command to the command reporter of the SDK to the agent.

Unless you disable the automatic command reports, all driver commands will be reported.

What I suggest is the following:

Disable the automatic command reports before that block of code using the report object.
Then in each of those driver actions, encapsulate them inside of a manual step report.
That means each driver action will be inside a report().step() method where the condition, if the step passed or failed, will be the result of the assertion.

Afterwards, enable the automatic command reports.

Great. What line do I use to disable and re enable the reports please?

I do not currently have any reporting against the page objects where I store this test method so should I add this into the page object or disable it on the test before I use the page object assert method?

It should be in the test themselves.

You can do the following:

    reporter = self._driver.report()
    reporter.disable_command_reports(True)
    # Have your lines here with manual step reporting
    reporter.disable_command_reports(False)