mercredi 18 avril 2018

Selenium C# - Clicking on hidden checkbox and disabled anchor

The HTML I'm testing has a hidden checkbox (with display set to none) with id "ed_passengers_terms". Followed by a span that has 2 icons inside it. Based on whether or not the checkbox is checked, the icon changes and an anchor is enabled.

HTML for checkbox:

<div class="field checkboxes divider-half color-focus">
              <input type="checkbox" id="ed_passengers_terms" name="ed_passengers[terms]" required="required" class="form-control  hide" value="1" />
              <label for="ed_passengers_terms" class="[ short-label  is-adjusted ]  label-stack">
                <span class="fa-stack">
                  <i class="icon-checkbox fa-stack-1x sc-grey-2"></i>
                  <i class="icon-tick text--h6 fa-stack-2x sc-yellow"></i>
                </span>
              </label>
              <span class="regular text--h6 sc-dark-grey">I accept SuperCoucou's <a target="_blank" href="/terms">Terms & Conditions</a></span>
          </div>

HTML for anchor:

<a href="#" onclick="$('.ed-book-form').submit();return false;" class="disabled btn btn-action btn-block text-uppercase text--h7 semibold view-details divider-half">next</a>

JavaScript that enables anchor:

if ($('#ed_passengers_terms').size() && $('#ed_passengers_terms').is(':checked')) {
    $('.btn-action').removeClass('disabled');
}

$('body').on('change', '#ed_passengers_terms', function() {
    if ($(this).is(":checked")) {
        $('.btn-action').removeClass('disabled');
    }
    else {
        $('.btn-action').addClass('disabled');
    }
});

I'm trying to create a test that enters some data on that page, then checks the checkbox to be able to click the hyperlink and proceed.

I've tried using Click() on the span, the icon, and the div. It didn't work. I've tried:

((IJavaScriptExecutor)driver).ExecuteScript("$('#ed_passengers_terms').checked = True;");

As a workaround, I thought I might use JavaScriptExecutor to enable the anchor and it does enable it but I can't seem to be able to click it anyway using:

driver.FindElement(By.CssSelector(".btn.btn-action.btn-block")).Click();

Or using:

driver.FindElement(By.XPath("//a[contains(.,'Next')]")).Click();

I need a way to be able to do the following: 1 - Preferably click the checkbox, but if that's not possible, I can enable the anchor using JavaScript so it's a non-issue. 2 - Click the anchor so the onClick action gets executed and then it redirects to the correct page.

P.S. I do not have access to change the HTML or the JavaScript, it's not my script, I'm only testing it

Aucun commentaire:

Enregistrer un commentaire