title: Mysterious protractor test failure when using custom util
body: <p>I want my tests to work no matter the order they are run in. Therefore I have created a util that logs in the test user if not already logged in. But it seems that when I am using the util that protractor misbehaves. Please read on. The util looks like this:</p>
  
  <pre><code>export class E2EUtils {
    loginAndGoTo(url: string) {
      return browser.get(url).then(() =&gt; {
        return browser.driver.getCurrentUrl().then(
          (currentUrl) =&gt; {
            if (currentUrl.indexOf('/login') !== -1) {
              element(by.css('[name=email]')).sendKeys('user@email.com');
              element(by.css('[name=password]')).sendKeys('secret');
              element(by.css('login form button')).click();
            }
            expect(browser.driver.getCurrentUrl()).toMatch('.*' + url + '$');
          }, (error) =&gt; {
            console.error('error in E2EUtil.loginAndGoTo', error);
          });
      });
    };
  }
  </code></pre>
  
  <p>The test that uses the util</p>
  
  <pre><code>import { E2EUtils } from '../e2e-utils.e2e-spec';
  let utils = new E2EUtils();
  
  describe('Home', () =&gt; {
    it('should show right profile', (done) =&gt; {
      utils.loginAndGoTo('home/user').then(() =&gt; {
        expect(element(by.css('home p')).getText()).toContain('user');
        done();
      }, (e) =&gt; { console.error('error in home.component.e2e-spec.ts', e); });
    });
  });
  </code></pre>
  
  <p>And it does not work. It hangs and eventually gives the error:</p>
  
  <pre><code>Failed: Timed out waiting for Protractor to synchronize with the page after 11 seconds. Please see https://github.com/angular/protractor/blob/master/docs/faq.md
  While waiting for element with locator - Locator: By(css selector, home p)
  </code></pre>
  
  <p>However. This 'identical test works'. Only difference is that I am not using the util.</p>
  
  <pre><code>  it('should show right profile-OLD', () =&gt; {
      browser.get('/home/user');
      element(by.css('[name=email]')).sendKeys('user@email.com');
      element(by.css('[name=password]')).sendKeys('secret');
      element(by.css('login form button')).click();
      expect(browser.getCurrentUrl()).toMatch('.*home/user$');
      expect(element(by.css('home p')).getText()).toContain('user');
    });
  </code></pre>
  
  <p>I have also used the Element Explorer and tried the element locator myself and it only works when not using the util. Even though the correct page is visible. So it would seem that the util sets protractor in a weird state. On a side note, I could not use <code>browser.getCurrentUrl()</code> in the util method and instead had to user <code>browser.driver.getCurrentUrl()</code>. However, <code>browser.getCurrentUrl()</code> works in tests that does not use the util.</p>