Automating testing of Toggle button

Hi, I am trying to automate the testing of this toggle button but it doesn’t seem to work. Any ideas how i can go ahead with it
on

of

Depends on the underlying html code for this particular button or style of buttons. Could you share the html code for it (removing any code to maintain security/privacy)?

                <label class="col-md-2 control-label ng-binding ng-scope" for="temporaryPassword">Temporary <i class="fa fa-question-circle text-muted ng-scope" tooltip="If enabled, the user must change the password on next login" tooltip-placement="right" tooltip-trigger="mouseover mouseout"></i></label>
                <div class="col-md-6">
                    <span ng-model="temporaryPassword" onoffswitch="" on-text="ON" off-text="OFF" class="ng-isolate-scope ng-not-empty ng-valid"><div class="onoffswitch" tabindex="0"><input type="checkbox" ng-model="ngModel" ng-disabled="ngDisabled" class="onoffswitch-checkbox ng-untouched ng-valid ng-dirty ng-valid-parse ng-not-empty" name="temporaryPassword" id="temporaryPassword"><label for="temporaryPassword" class="onoffswitch-label"><span class="onoffswitch-inner"><span class="onoffswitch-active ng-binding">ONn</span><span class="onoffswitch-inactive ng-binding">OFF</span></span><span class="onoffswitch-switch"></span></label></div></span>
                </div>
                <kc-tooltip class="hidden"></kc-tooltip>
            </div>

I am assuming the html you shared is when the toggle is set to “ON” based on this bit:

<span class="onoffswitch-active ng-binding">

With that said it may be that your active button is:

//span[contains(@class, 'onoffswitch-active')]

You would then need to figure out what your class for the “OFF” version of the switch is.

If you wanted to have a locator that would find the element regardless of its toggle state it could go like so (Assuming the “OFF” state has a class of “onoffswitch-inactive” for this example):

//span[contains(@class, 'onoffswitch-active') or contains(@class, 'onoffswitch-inactive')]

Cropath is fairly useful for help to find these locators and test them out live: ChroPath - Chrome Web Store

As the toggle switch has an html element of type checkbox, the libraries checkbox keywords might work to either check the state of the element or change the state of it. And as it has an id (temporaryPassword) I would use that as the locator. For example

Select Checkbox id:temporaryPassword
Checkbox Should Be Selected id:temporaryPassword

should select it and the verify it is selected.

image