title: Protractor: Get text of an alert?
body: <p>I'm testing my Angular app with Protractor. I've looked through <a href="https://github.com/angular/protractor/blob/master/docs/api.md" rel="nofollow">the docs</a> and can't find any way to get the text of an alert. It's not an element in the DOM per se (at least, not that I can figure out; when there's an alert up, Chrome's inspector won't allow you to inspect it). How would I test that an alert has the correct message? Or even, that one is present?</p>
  
  <h3>Edit</h3>
  
  <p>Here is my code. HTML:</p>
  
  <pre><code>  &lt;button id='alertButton' data-ng-click='ngAlert()'&gt;Button&lt;/button&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>$scope.ngAlert = function(){
    window.alert('Hello');
  };
  </code></pre>
  
  <p>Protractor spec:</p>
  
  <pre><code>  describe('alert', function(){
      var ptor = protractor.getInstance();
      beforeEach(function(){
        button = $('#alertButton');
        button.click();
      });
      it('tells the alert message', function(){
        expect(button.getText()).toEqual('Button');
      });
    });
  </code></pre>
  
  <p>When I make an assertion on the button text like this:</p>
  
  <pre><code>it('tells the alert message', function(){
    expect(button.getText()).toEqual('Button');
  });
  </code></pre>
  
  <p>It passes. But if I try to read an alert like this:</p>
  
  <pre><code>it('tells the alert message', function(){
    var alertDialog = ptor.switchTo().alert();
    expect(alertDialog.getText()).toEqual('Hello');
  });
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>$ protractor spec/e2e/conf.js Using the selenium server at
    <a href="http://localhost:4444/wd/hub" rel="nofollow">http://localhost:4444/wd/hub</a> .F</p>
    
    <p>Failures:</p>
    
    <p>1) alert tells the alert message    Message:
         NoSuchAlertError: no alert open   (Session info: chrome=30.0.1599.101)   (Driver info: chromedriver=2.2,platform=Mac OS
    X 10.9.0 x86_64) (WARNING: The server did not provide any stacktrace
    information) Command duration or timeout: 3 milliseconds Build info:
    version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
    System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version:
    '10.9', java.version: '1.6.0_65' Session ID:
    edbaa752eb14ad45f7e961903b69a466 Driver info:
    org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=MAC,
    acceptSslCerts=true, javascriptEnabled=true, browserName=chrome,
    chrome={chromedriverVersion=2.2}, rotatable=false,
    locationContextEnabled=true, version=30.0.1599.101,
    cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true,
    browserConnectionEnabled=false, nativeEvents=true,
    webStorageEnabled=true, applicationCacheEnabled=false,
    takesScreenshot=true}]</p>
    
    <p>Finished in 2.125 seconds 2 tests, 2 assertions, 1 failure</p>
    
    <p>/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1542
          throw error;
                ^ NoSuchAlertError: no alert open</p>
  </blockquote>
  
  <p>But I've tested it on the page and it works, and the test can clearly find the button in the DOM. So either the <code>click()</code> function isn't working, or something else is going on?</p>