title: how to handle unexpected 'alert' in selenium webdriver
body: <p><strong>PS:</strong> <em>driver.switchTo().alert().accept(); is not working</em> </p>
  
  <p>Well , I want to automate testing of webpage which shows  <strong>random alerts*</strong>  </p>
  
  <pre><code>*random alerts :JavaScript Alert which are not controlled and can appear any time 
  </code></pre>
  
  <p>so I dont know where to put <code>driver.switchTo().alert().accept()</code>because the occurrence of alert is not known in advance.</p>
  
  <p>So kindly help me.. Here is simple mocha selenium test... I have commented out the alert.accept() part..</p>
  
  <p>I want to know how to handle  </p>
  
  <blockquote>
    <p>UnexpectedAlertOpenError: unexpected alert open: {Alert text :}</p>
  </blockquote>
  
  <p>once in for all....  </p>
  
  <p>I read the selenium docs but couldn't find any...</p>
  
  <p>here is my code</p>
  
  <pre><code>describe( 'handling alerts' , function(done){      
          after(function(done){
                 return driver.quit();
              done(); 
          });
  
          it( 'should check title ', function(done){
                  driver.get("https://www.google.in");
                  driver.findElement(By.id("q")); 
  
                  driver.get("https://jsfiddle.net/cwthh0y7/");
                 // driver.switchTo().alert().accept();   
  
                  driver.getTitle().then(function(title){  
  
                      assert.equal(title, "Edit fiddle - JSFiddle");
                      done();   
                  });
  
  
  
          });
  
      });
  </code></pre>