How can you test if the focus is on an input field?
I tried doing it like this: in my .po.ts
getEditInputField(fieldName: string) {
return $(`input[data-e2e=${fieldName}]`)
}
In my .spec.ts
it("should focus the Abschlag input field", async () => {
expect(await budgetBilling.getEditInputField("Abschlag").isSelected()).toBe(true)
})
But if i test the test like .toBe(false)
, protractor lets the test pass in both cases. But it should fail in the case of toBe(false)
.
Then I found this solution: protractor: test for focus of field
and used it like this:
it("should focus the Abschlag input field", async () => {
// await browser.waitForAngular()
expect(await budgetBilling.getEditInputField("Abschlag").getAttribute('id')).toEqual(await browser.driver.switchTo().activeElement().getAttribute('id'))
})
And the test passes as expected. But if I change it to getEditInputField("something")
and "something" is also an input field on the same form, but not selected/focused, the test also passes. But it should fail in this case.
And the waitForAngular()
included or not does not seem to matter.
Am I doing something wrong or has someone a good idea how to test in protractor if a field is selected/focused?