mardi 11 février 2020

Check the label of a checkbox using Selenium and behave in Python

I want to write a test that checks if the correct text is shown on a website. It mostly works, but I am stuck with checking the label of a checkbox.

HTML:

<div class="checkbox">
  <label>
    <input id="remember_me" name="remember_me" tabindex="130" type="checkbox" value="y">
      " Remain logged in
      "
  </label>
</div>

Behave:

Scenario: See the formular for log in
    Given I am on the "/login" page
    Then I should see header "Log In"
    ...
    And I should see the checkbox for "remember_me"
    And I should see label "Remain logged in"

The header is found without issue:

@then(u'I should see header "{header}"')
def step_impl(context, header):
  assert context.browser.find_element_by_xpath("//h1[contains(text(), '{}')]".format(header))

as well as the checkbox itself:

@then(u'I should see the checkbox for "{name}"')
def step_impl(context, name):
  assert len(context.browser.find_elements_by_xpath("//input[@type='checkbox'][@name='{}']".format(name))) > 0

I don't know how to specifically check for the label "Remain logged in" however. Anything I tried, including checking for the text itself with find_text() or in such a way:

def step_impl(context, label):
  assert context.browser.find_element_by_xpath("//input[@type='checkbox' and contains(text(), '{}')]".format(name))) > 0

has not work.

Does Selenium allow to check the text of the label and if yes, how would I go about it?




Aucun commentaire:

Enregistrer un commentaire