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


On Thursday, April 11, 2019 at 12:46:32 AM UTC-4, NaviHan wrote:
>
> 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:
>>
>> 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)
>>
>>
>> A video of the issue here @ 
>> https://drive.google.com/file/d/1Di9idgfqjE7TLSn5QG7pgUFxpln3kben/view?usp=sharing
>>
>

-- 
-- 
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-general@googlegroups.com
http://groups.google.com/group/watir-general
watir-general+unsubscr...@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-general+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to