nhrcpt
nhrcpt

Reputation: 872

Testing whether element is disabled in Protractor

We have some checkboxes that should be disabled on the app. Below is the html snippet:

<label _ngcontent-c17="">
<input _ngcontent-c17="" checked="" disabled="" type="checkbox" value="">
    <span _ngcontent-c17="" class="ml-2"> Speech</span>
</label>

On the app, I see that the checkbox is both Checked and Disabled.

Below is my code snippet to test whether the "Speech" checkbox is checked and disabled:

 it(' Should validate the checked list on "Not Started" Page ', function () {
  expect(EC.elementToBeSelected(G.Speech_Checkbox_on_Services)).toBeTruthy();
  expect(G.Speech_Checkbox_on_Services.isEnabled()).toBeFalsy();
  expect(G.Speech_Checkbox_on_Services.getAttribute('disabled')).toBeTruthy();
  });

Last two lines of the code should check if the checkbox is disabled. However, when I run it, both the lines fail. How can I fix this?

Expected true to be falsy.
Expected null to be truthy.

Logs:

Error: Failed expectation
at UserContext.<anonymous> (C:\ESY_testing-and-deployment\Non_Test_Speks\TestBed.js:57:57)
at C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:112:25
at new ManagedPromise (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1077:7)
at ControlFlow.promise (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2505:12)
at schedulerExecute (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:95:18)
at TaskQueue.execute_ (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2974:25)
at C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:668:7

Error: Failed expectation
at UserContext.<anonymous> (C:\ESY_testing-and-deployment\Non_Test_Speks\TestBed.js:58:70)
at C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:112:25
at new ManagedPromise (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1077:7)
at ControlFlow.promise (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2505:12)
at schedulerExecute (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:95:18)
at TaskQueue.execute_ (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2974:25)
at C:\Users\Nasim Patwary\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:668:7

Upvotes: 0

Views: 1311

Answers (1)

Shrirang
Shrirang

Reputation: 198

If you want to check whether the checkbox is checked and disabled

expect(G.Speech_Checkbox_on_Services.attr('checked')).toBeTruthy();
expect(G.Speech_Checkbox_on_Services.getAttribute('disabled')).toBe(true);

https://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.isEnabled

hope this helps.

Upvotes: 2

Related Questions