0

I've been trying to implement tests to check for field validation in forms. A check for specific field error messages was straightforward, but I've also tried a generic check to identify the parent element of a field for an error class. This however isn't working.

A field with an error has the following HTML;

<div class="field clearfix error ">
    <div class="error">
        <p>Please enter a value</p>
    </div>
    <label for="id_fromDate">
    <input id="id_fromDate" type="text" value="" name="fromDate">
</div>

So to check for an error I've got the following function;

def assertValidationFail(self, field_id):
    # Checks for a div.error sibling element
    el = self.find(field_id)
    try:
        error_el = el.find_element_by_xpath('../div[@class="error"]')
    except NoSuchElementException:
        error_el = None
    self.assertIsNotNone(error_el)

So el is the input field, but then the xpath always fails. I believed that ../ went up a level in the same way that command line navigation does - is this not the case?

2 Answers 2

1

Misunderstood your question earlier. You may try the following logic: find the parent div, then check if it contains class error, rather than find parent div.error and check NoSuchElementException.

Because .. is the way to go upper level, ../div means parent's children div.

// non-working code, only the logic
parent_div = el.find_element_by_xpath("..") # the parent div
self.assertTrue("error" in parent_div.get_attribute("class"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I like this way of doing it. But it seems grabbing the parent_div for the error only works on input fields. On multiple select boxes or radio button's the parent seems to be the option label & starting to do ./../../ type things is something I'd like to avoid.
1

When you're using a relative xpath (based on an existing element), it needs to start with ./ like this:

el.find_element_by_xpath('./../div[@class="error"]')

Only after the ./ can you start specifying xpath nodes etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.