-1

I don`t why this protractor code work for popups. My protractor version : 4.0.3 chrome version: chromedriver_2.22.exe

below is the Error message that i am getting

Failed: no alert open (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64) Stack: NoSuchAlertError: no alert open (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64)

HTML Script for the popup

<div class="modal-content" modal-transclude=""><div class="popupAlert popupAlert-full marginNone ng-scope">
    <div class="popup-header">
        <h2 class="modal-title ng-binding" id="myModalLabel">You are leaving this page </h2>
    </div>
    <div class="popup-body">
        <div class="row Margin0">
            <p class="text ng-binding">By continuing all the SLI data will be lost.</p>
        </div>
    </div>
    <div class="popup-footer">
        <div class="row Margin0">
            <a data-ng-click="cancel()" class="pull-right buttonLink2">Cancel
            <button type="button" class="pull-right swc_button" focus-element="autofocus" data-dismiss="modal" ng-click="ok()">Ok</button>
        </a></div><a data-ng-click="cancel()" class="pull-right buttonLink2">
    </a></div><a data-ng-click="cancel()" class="pull-right buttonLink2">
</a></div></div>

Protractor code

var popupAlert = browser.switchTo().alert();

    var alertText = popupAlert.getText();
    console.log(" Alert text :" +alertText);
   popupAlert.accept();

//expect(popupAlert.accept).toBeDefined();

Balaji Singh .Y
  • 683
  • 2
  • 9
  • 22

3 Answers3

1

browser.switchTo().alert() will work only if your pop up is a Javascript alert box. In your scenario,the pop up is generated using HTML code and it can be captured as a normal webelement. use below code to accept the pop up.

element(by.css(".popupAlert")).isDisplayed().then(function(isDisplayed){    
  if(isDisplayed){
      element(by.css(".popup-footer a")).click() //to close the alert
      //or
     element(by.css(".popup-footer button")).click() //to accept the alert
   }
})
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22
  • Even if it is HTML popup but still we need to Switch then only i will able to click on the cancel link or click on submit button – Balaji Singh .Y Aug 22 '16 at 14:13
  • HTML popup is nothing but a div element that is overlapped over your webpage. Those popups are called as Modal windows. have a look at sample bootstrap modal windows for better understanding on how modal windows works. [Bootstrap modal windows] (http://getbootstrap.com/javascript/#modals) – Sudharsan Selvaraj Aug 22 '16 at 14:24
1

I agree with @Sudarshan's answer you don't have a javascript alert there, it is a simple html popup.

however since the popup takes sometime to be displayed, in my experience with use of ExpectedConditions it could be captured efficiently.

var EC = protractor.ExpectedConditions;
var popup = $('popupAlert');
var okButton = element(by.buttonText('Ok')); //pls use appropriate locators incase this doesn't work!
popup.click();
browser.wait(EC.visibilityOf(okButton),5000);
okButton.click();
Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
0

Check whether you have written code to open up the alert box.

Do this is by using browser.pause(). This acts like a breakpoint in protractor, use it after doing a action to open the alert box and use key c to continue the protractor test step by step( dont wait too long else protractor will throw a timeout exception).

radio_head
  • 1,306
  • 4
  • 13
  • 28