How to handle browser javascript confirm navigation warning with ruby ​​watir script

I would like to programmatically handle a javascript validation notification asking if you want to leave the page (see screenshot below).

google chrome alert

The methods I have tried so far have not worked.

The browser doesn't recognize that I'm returning true in order to leave the page.

This is the part where I am trying to handle this warning:

if  @b.div(:class, "infoMessageBox error").present?
        puts "There was an error"
        #@b.execute_script("window.confirm = function() {return true}")
        @b.execute_script("window.onbeforeunload = function() {};")
        @b.goto("#{@env}/TranslateFile.aspx")
        break
    end

      

I also read this article: " Reject & Confirm Navigation" Popup with Watir ", but the method in this article does not help in my case.

I would suggest some kind of help because I am stuck here.

IT WILL HELP in solving my problem.

I have improved the code and it works better now, but some other problems may arise in the future (hopefully not).

@b.refresh
if @b.alert.exists?
    @b.alert.text
    @b.alert.ok 
end
@b.goto("#{@env}/TranslateFile.aspx")

      

This worked for me and now it escapes the warning and continues the workflow. Thanks for the help!:)

Additional information for those who may have problems handling warnings (link is located below):

# Check if alert is shown
browser.alert.exists?

# Get text of alert
browser.alert.text

# Close alert
browser.alert.ok
browser.alert.close
JAVASCRIPT CONFIRMS
# Accept confirm
browser.alert.ok

# Cancel confirm
browser.alert.close
JAVASCRIPT PROMPT
# Enter text to prompt
browser.alert.set "Prompt answer"

# Accept prompt
browser.alert.ok

# Cancel prompt
browser.alert.close

# don't return anything for alert
browser.execute_script("window.alert = function() {}")

# return some string for prompt to simulate user entering it
browser.execute_script("window.prompt = function() {return 'my name'}")

# return null for prompt to simulate clicking Cancel
browser.execute_script("window.prompt = function() {return null}")

# return true for confirm to simulate clicking OK
browser.execute_script("window.confirm = function() {return true}")

# return false for confirm to simulate clicking Cancel
browser.execute_script("window.confirm = function() {return false}")

# don't return anything for leave page popup
browser.execute_script("window.onbeforeunload = null")

      

Documentation from watir

+3


source to share


1 answer


Answer: you can try using an alternative method such as: @b.alert.exists?

if it returns true

then:@b.alert.close



I think this may help you in the future

+2


source







All Articles