0

In my test I want to verify selected checkbox. Below is my code

if (CheckboxForFirstCitizenship.isSelected()) {
        System.out.println("Checkbox selected");
    } else {
        System.out.println("Checkbox not selected");
    }
    if (CheckboxForSecondCitizenship.isSelected()) {
        System.out.println("Checkbox selected");
    } else {
        System.out.println("Checkbox not selected");
    }

Web elements I have in PageObjects class

@FindBy(xpath = "/html[1]/body[1]/ufe-root[1]/div[1]/div[1]/div[1]/ng-component[1]/div[1]/ufe-klient[1]/app-dashboard[1]/main[1]/div[1]/div[2]/app-individual-client-edit-container[1]/app-process-container[1]/app-local-loader[1]/div[1]/div[3]/app-individual-client-edit-client-data[1]/app-form-container[1]/p-accordion[1]/div[1]/p-accordiontab[1]/div[2]/div[1]/div[2]/div[1]/app-form-data-set-container[1]/div[1]/div[1]/app-individual-client-edit-personal-data-form[1]/app-data-container[1]/div[1]/div[2]/app-container-state[1]/div[1]/form[1]/div[1]/div[8]/div[1]/div[1]/app-individual-client-edit-citizenship-form[1]/div[2]/p-checkbox[1]/div[1]/div[2]")
@CacheLookup
public WebElement CheckboxForFirstCitizenship;

@FindBy(xpath = "/html[1]/body[1]/ufe-root[1]/div[1]/div[1]/div[1]/ng-component[1]/div[1]/ufe-klient[1]/app-dashboard[1]/main[1]/div[1]/div[2]/app-individual-client-edit-container[1]/app-process-container[1]/app-local-loader[1]/div[1]/div[3]/app-individual-client-edit-client-data[1]/app-form-container[1]/p-accordion[1]/div[1]/p-accordiontab[1]/div[2]/div[1]/div[2]/div[1]/app-form-data-set-container[1]/div[1]/div[1]/app-individual-client-edit-personal-data-form[1]/app-data-container[1]/div[1]/div[2]/app-container-state[1]/div[1]/form[1]/div[1]/div[8]/div[1]/div[2]/app-individual-client-edit-citizenship-form[1]/div[2]/p-checkbox[1]/label[1]")
@CacheLookup
public WebElement CheckboxForSecondCitizenship;

enter image description here

In test result I have two printlines 'Checkbox not selected' despite of firstCheckox is selected. What I'm doing wrong? Can someone help?

<p-checkbox _ngcontent-c26="" binary="true" class="control__hint ng-untouched ng-pristine ng-valid ng-star-inserted" label="Główne" id="main-citizenship-checkbox-0" style="" xpath="1">
    <div class="ui-chkbox ui-widget">
        <div class="ui-helper-hidden-accessible">
            <input type="checkbox" name="undefined" value="undefined">
        </div>
        <div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"><span class="ui-chkbox-icon ui-clickable pi pi-check"></span></div>
    </div>
    <!---->
    <label class="ui-chkbox-label ui-label-active ng-star-inserted">Główne</label>
</p-checkbox>
2
  • Share HTML of the checkboxes in text format Commented Oct 30, 2019 at 11:01
  • <p-checkbox _ngcontent-c26="" binary="true" class="control__hint ng-untouched ng-pristine ng-valid ng-star-inserted" label="Główne" id="main-citizenship-checkbox-0" style="" xpath="1"><div class="ui-chkbox ui-widget"><div class="ui-helper-hidden-accessible"><input type="checkbox" name="undefined" value="undefined"></div><div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"><span class="ui-chkbox-icon ui-clickable pi pi-check"></span></div></div><!----><label class="ui-chkbox-label ui-label-active ng-star-inserted">Główne</label></p-checkbox> Commented Oct 30, 2019 at 11:06

1 Answer 1

1

To be able to use isSelected method, you have to address to the input type="checkbox" element.
Use css selector #main-citizenship-checkbox-0 input for the first checkbox.
Probably for the second one, it will be #main-citizenship-checkbox-1 input.

@FindBy(css = "#main-citizenship-checkbox-0 input")
@CacheLookup
public WebElement checkboxForFirstCitizenship;
Sign up to request clarification or add additional context in comments.

1 Comment

I change from xpath to css and it's correct. Thanks for help.

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.