0

This is my e2e test code:

import { browser, by, element } from 'protractor';
describe('LoginComponent', () => {
  beforeEach(() => {

  });

  it('should show an error message when the email is incorrect', () => {
    browser.get(browser.baseUrl + 'login');
    const emailValue = 'test@gmail.com';
    const email = element(by.id('email'));
    email.sendKeys(emailValue);
    expect(email.getAttribute('value')).toBe(emailValue);
  });
});

Even though I set the value of the text input to be test@gmail.com the expect is unable to fetch and in the console I get error:

 - Expected null to be 'test@gmail.com'.

What am I doing wrong?

I also tried this:

import { browser, by, element, Key } from 'protractor';
describe('LoginComponent', () => {
  beforeEach(() => {
  });

  it('should show an error message when the email is incorrect', () => {
    browser.get(browser.baseUrl + 'login');
    const emailValue = 'test@gmail.com';
    const email = element(by.id('email'));
    email.sendKeys(emailValue);
    browser.sleep(2000);
    email.sendKeys(Key.TAB);
    expect(email.getAttribute('value')).toBe(emailValue);
  });
});

Html is pretty simple:

<input id="email"/>

Now if I change the html to <input id="email" value=""/>

then the error changes to

  - Expected '' to be 'test@gmail.com'.
Laala
  • 1
  • 1
  • Couple of thoughts; 1) Is it an Angular page? If it is not and the wait for angular function is turned off, you might need to wait to ensure that the email element can receive keys. 2) can you visually confirm that the text has been entered? 3) Nit: look into switching to async / await. – cnishina Jun 04 '21 at 09:31
  • What version of Chrome / ChromeDriver are you using? It looks like there's a separate issue possibly with getAttribute. See https://stackoverflow.com/questions/67813538/element-getattributevalue-returns-null-in-protractor – cnishina Jun 04 '21 at 09:33
  • pretty sure it's the same problem as https://stackoverflow.com/q/67813538/9150146 – Sergey Pleshakov Jun 04 '21 at 13:37
  • Have you tired to change html to `` – yong Jun 05 '21 at 08:26
  • 1
    This is a bug in ChromeDriver 91, check out my answer at https://stackoverflow.com/questions/67844716/protractor-issue-after-update-chromium-version/67888592#67888592 – Jan Molak Jun 08 '21 at 21:46

1 Answers1

0

Looks like after inserting the text into the element you should move the focus to some other element by clicking on it etc.
Then you should use some kind of method waiting until that element appearing with a new value.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Actually I can see browser page start and navigate to the login URL and send keys sends the text to that input but when I use getAttribute value it returns NULL and that is where it fails. – Laala Jun 04 '21 at 07:06
  • Try adding a kind of pause / sleep between sending the keys and getting the element attribute. Also, possibly you should press Enter etc at the end of sending the keys or click on some other element to evaluate that element attribute. There are several possible behaviors – Prophet Jun 04 '21 at 07:09
  • Still doesn't work. Check my updated code. – Laala Jun 04 '21 at 07:14
  • I see. I only can guess here will I need to debug it... browser.sleep(2000); delay can be shorter. Maybe try clicking on some other element, not just tab out? also, try adding the delay between the other element click and getting your element attribute – Prophet Jun 04 '21 at 07:17
  • BTW, are you sure it is element value there, not element text? – Prophet Jun 04 '21 at 07:18
  • I also tried 'expect(email.getText()).toBe(emailValue);' and still get the error: - Expected '' to be 'test@gmail.com'. – Laala Jun 04 '21 at 07:20
  • I see. you didn't answer my previously questions – Prophet Jun 04 '21 at 07:20
  • 1
    when I press TAB so it should have set the value instantly. I set more delay as a test. So far the only code that works is `expect(browser.executeScript('return document.getElementById("email").value')).toBe(emailValue);` I guess I will have to satisfy with this for now. – Laala Jun 04 '21 at 07:24