Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confirm alert window in phantom.js

i have this function which find button and click it, but after that alert appears and i need to confirm it using phantom.js

function() {
  page.evaluate(function() {
    $('.item-list clicked').first().find($('.comment-delete')).find('a').click();
  })
}

may be i can emulate function which call alert whithout click immediately ? or use function waitFor for waiting this alert?(unlikely this, waitFor waiting only for DOM objects i think so)

like image 200
Alice Polansky Avatar asked Nov 11 '13 09:11

Alice Polansky


People also ask

How to use confirm alert in JavaScript?

The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. message is the optional string to be displayed in the dialog.

How does Phantom JS work?

PhantomJS is a discontinued headless browser used for automating web page interaction. PhantomJS provides a JavaScript API enabling automated navigation, screenshots, user behavior and assertions making it a common tool used to run browser-based unit tests in a headless system like a continuous integration environment.


2 Answers

As far as I know, Phantom.JS is headless. The only thing you can do is get the context of the alert with

window.onAlert = function(alertText){
    ...
   }

but no more than this I think. You cannot either close (in general) or render (in Phantom.JS) the alert programmatically.

Some sources:
* Render JavaScript alerts with Phantom.JS
* Close alert programmatically

like image 40
MarcoL Avatar answered Oct 05 '22 01:10

MarcoL


I can't find the other stackoverflow answer that helped me answer this as well but basically you can inject a javascript function that will confirm the alert popup:

Here's my python webdriver implementation:

def example_js_confirmation( self ):
    js_confirm = 'window.confirm = function(){return true;}'    # .js function to confirm a popup
    self.execute_javascript( js_confirm )
    self.find_by_id( 'submit' ).click()
    self.execute_javascript( 'return window.confirm' )  # trigger the injected js that returns true (virtually click OK)

It's on somebody's todo list =) : Selenium Desired Capabilities - set handlesAlerts for PhantomJS driver

like image 166
foupfeiffer Avatar answered Oct 05 '22 02:10

foupfeiffer