Cant capture alert using "alert" method of Page Object

18 views
Skip to first unread message

NaviHan

unread,
Apr 10, 2019, 10:23:16 PM4/10/19
to Watir General
Im trying to capture the alert text and dismiss the alert using the "alert" method

  def test
   
@browser.div(:class => 'address-details', :index =>0).click
    alert_text
= alert do
     
@browser.div(:class => 'select-checkbox', :index =>1).click
   
end
    sleep
10
    puts alert_text


 
end


I have passed @browser.div(:class => 'select-checkbox', :index =>1).click  into the alert method which actually triggers the alert

As per the implementation this should capture the alert text and click OK

However I'm getting this error

elenium::WebDriver::Error::UnhandledAlertError: unexpected alert open: {Alert text : This address cannot be used.}
  (Session info: chrome=73.0.3683.86)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)


Titus Fortner

unread,
Apr 11, 2019, 12:23:03 AM4/11/19
to watir-general
I don't like the way Page Object uses blocks... In Watir it is simply:

alert_text = @browser.alert.text
@browser.alert.dismiss
> --
> --
> Before posting, please read https://github.com/watir/watir_meta/wiki/Guidelines-for-Posting-to-Watir-General-Google-Group.
> In short: search before you ask, be nice.
>
> watir-...@googlegroups.com
> http://groups.google.com/group/watir-general
> watir-genera...@googlegroups.com
> ---
> You received this message because you are subscribed to the Google Groups "Watir General" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to watir-genera...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

NaviHan

unread,
Apr 11, 2019, 12:41:55 AM4/11/19
to Watir General

NaviHan

unread,
Apr 11, 2019, 12:46:32 AM4/11/19
to Watir General
Hi Titus

    @browser.div(:class => 'address-details', :index =>0).click
    # alert_text = alert do
    #   @browser.div(:class => 'select-checkbox', :index =>1).click
    # end
    # sleep 10
    # puts alert_text
    @browser.div(:class => 'select-checkbox', :index =>1).click
    puts @browser.alert.text
    @browser.alert.ok


This worked.

But I would like to know why the PageObject method alert is not working as expected.

May be wait for Justin :-)

On Thursday, 11 April 2019 12:23:16 UTC+10, NaviHan wrote:

Justin Ko

unread,
Apr 11, 2019, 11:00:43 AM4/11/19
to Watir General
Hi Navi,

At this point, I would say there is a design flaw in Page-Object's #alert method, which does:

if @browser.alert.exists?
  value
= @browser.alert.text
 
@browser.alert.ok
end

The conditional check is likely the cause of your problem. I don't know the history of how we got here, but my guess is that we wanted to handle controls that randomly created alerts.

Unfortunately, this can be easily thrown off. I assume your div does not immediately trigger the alert. There is probably some sort of processing and/or asynchronous call that happens before deciding to show the alert. This can be enough to cause problems - ie:
# Div is clicked
# Some JavaScript starts to determine if the alert should appear
# #alert checks if an alert exists or not, finds nothing so just moves on (ie no alert to dismiss)
# The JavaScript finishes processing, displaying the alert
# The next Watir command fails since the alert is still there

You can see how little is required to create the timing issue. The following page has a div, that when clicked waits 10ms before displaying the alert.

<!doctype html>
<html>
<head>
 
<script>
   
function sleep(ms) {
     
return new Promise(resolve => setTimeout(resolve, ms));
   
}

    async
function run() {
      await sleep
(10); // (in milliseconds) slight delay before alert is triggered
      alert
("test");
   
}
 
</script>
</head>
<body>
 
<div class="select-checkbox" onclick="run();">asdf</div>
</body>
</html>

The following page-object clicks this div but does not dismiss the alert. The exception then occurs on the next command:

class MyPage
  include
PageObject

 
def do_stuff
    alert
do
      div
.click
   
end
    sleep
(1) # ensure alert has appeared
    p div
.text
    #=> Selenium::WebDriver::Error::UnhandledAlertError
 
end
end

page
= MyPage.new(browser)
page
.do_stuff

I would be curious if you added a #sleep to your code if that would address the problem:

@browser.div(:class => 'address-details', :index =>0).click
alert_text
= alert do
 
@browser.div(:class => 'select-checkbox', :index =>1).
click
  sleep
(10) # add this sleep to ensure that the alert appears before proceeding
end
sleep
(10)
puts alert_text

Though if that solves your problem, I do not think the #alert method is adding any value. You know the alert is going to appear, so you may as well call Watir's alert code (ie Titus' example), which would include automatic waiting.

Justin

NaviHan

unread,
Apr 12, 2019, 3:03:27 AM4/12/19
to Watir General
Thanks a lot Justin in taking time to write a detailed explanation about the issue. Novice automation engineers like me are very blessed to have people like you and Titus showing patience to explain basic things to us.

You are right about the issue. Though Im not able to confirm with the devs regarding the JS that runs between the click and the alert as you suggested the #alert function was able to capture the alert with a sleep after the click.

I will user the Watir alert method as its has automatic waiting.

As far as I know there is only a limited no of times alert is even shown our our application.

Cheers
Navi

On Thursday, 11 April 2019 12:23:16 UTC+10, NaviHan wrote:
Reply all
Reply to author
Forward
0 new messages