0

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?

micro
  • 27
  • 6

1 Answers1

0

Have you added id attributes to the input elements or are they blank? In your case I guess you do not have to compare id and can instead compare the data attribute:

expect(browser.driver.switchTo().activeElement().getAttribute('data-e2e')).toEqual('Abschlag')
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • 1
    You are absolutely right. I dont have the attribute 'id' on my element. To use 'data-e2e' works perfectly. Thank you :) – micro Jan 24 '20 at 12:19