Watir: "unexpected alert open" error thrown when trying to refresh page after manually closing alert - watir

I've created an instance of Watir::Browser with chrome set as the browser.
b = Watir::Browser.new :chrome
If I manually close an alert and then try to refresh the page using
b.refresh
the following error is thrown:
Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open
(Session info: chrome=28.0.1500.95)
(Driver info: chromedriver=2.2,platform=Windows NT 6.1 x86_64)
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/response.rb:51:in assert_ok'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/response.rb:15:in initialize'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:59:in new'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:59:in create_response'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/default.rb:66:in request'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:40:in call'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:634:in raw_execute'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:612:in execute'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:210:in refresh'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/common/navigation.rb:38:in refresh'
from C:/ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/browser.rb:201:in refresh'
When I call b.alert.exists? it returns true.
I tried looking at using b.alert.initialize to reset the state of the alert but it throwns an exception.
Is there a way to completely reset the state of the browser object?

I was facing this problem too:
But I am successfully get rid of it by putting it in a begin-rescue block.
Here is how:
begin
# put line of code which triggers alert on page
rescue Selenium::WebDriver::Error::UnhandledAlertError
#browser.alert.ok
end

Related

Timeout Error while using selenium firefox webdriver in VBA

I am trying to use selenium firefox webdriver to navigate to a page and initiate a click event. This event should paste a csv data onto the clipboard. Upon executing this code the firefox window pops up without any url in the address bar and then I get the following error message:
Error: TimeoutError
Firefox failed to open the listening port 127.0.0.1:12825 with 15s
Here is my code
Set objFireFox = CreateObject("Selenium.FirefoxDriver")
objFireFox.Get "https://chartink.com/screener/practical-swing-trading-stock-scanner", 20000, False
objFireFox.FindElementsByClass("btn btn-default buttons-copy buttons-html5 btn-primary").Click
Initially I was using the following statement
objFireFox.Get "https://chartink.com/screener/practical-swing-trading-stock-scanner"
then I modified it to
objFireFox.Get "https://chartink.com/screener/practical-swing-trading-stock-scanner", 20000, False
but still the error message was same in both cases. I have downloaded the latest version of mozilla/geckodriver from https://github.com/mozilla/geckodriver/releases/tag/v0.30.0
Thanks

How to Know an Alert Pop-Up Has Occurred

I am automating a website. I am using SeleniumBasic in Excel VBA.
I have a simple Pop-Up, or Alert, with an OK button.
I cannot access the Pop_Up by using Inspect so I have no HTML to show.
When the Pop-Up occurs, I respond with Driver.SwitchToAlert.Accept. It goes away, as desired.
I don't know how to check programmatically if the Pop-Up is present.
If I assume that the Pop-Up may have occurred and use this code on a just-in-case basis, i.e. use the code without the Pop-Up existing, the program stops without any error message.
Is there a way to check if the Pop-Up window exists before I respond?
Well, I found the answer:
I got this code from someone who got it from someone else.
Private Function AlertIsPresent() As Boolean
'Returns true if an alert is present, false otherwise
Dim T As String
On Error Resume Next
T = Driver.Title 'This raises error 26 if an alert(pop-up) is displayed
AlertIsPresent = (26 = Err.Number)
End Function
If function returns 'true', use the Driver.SwitchToAlert.Accept to close it.

elementIsNotVisible throwing noSuchElementError in Selenium

I wrote a test to ensure a slideout panel closes in my selenium tests using node js. Whenever I close the panel I use the condition elementIsNotVisible. However an error is thrown, noSuchElementError. This error is technically correct because the element should no longer be on the page, but shouldn't elementIsNotVisible be correct as well?
Test.js file:
await driver.wait(until.elementIsVisible(testPage.addAllAnnotationsButton(driver)), 20000);
await testPage.exitGeneAnnotationsButton(driver).click();
await driver.wait(until.elementIsNotVisible(testPage.addAllAnnotationsButton(driver)), 20000);
Page.js file:
const testPage = {
addAllAnnotationsButton: driver => driver.findElement(By.css(testPage.addAllAnnotationsButtonSELECTOR))
}
Error message(Note- Error message is thrown on the line where elementIsNotVisible is called):
NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"div.slideout.opened:nth-child(6) button.ui.button.medium:nth-child(2)"}
Edit 1: Please note, I have tried using stalenessOf and I still receive the same error.
As you have mentioned that when you close the panel, you use the condition elementIsNotVisible :
Here once you have closed the panel , the respective web element is not present in DOM. That is the reason you are getting NoSuchElement exception.
Suggestion : Use invisiblityofElement instead of elementIsNotVisible.
Though there are many reasons behind NoSuchElement Exception, one of the most common is try to interact with element/elements present in frame/iframe without switching the focus of web driver.
Hope this will help.

Make Watir-webdriver to load a page for limited time and to be able to retrieve information later

I know that there are several questions related to implementation of waiting and timeouts in Watir, however I have not found an answer to my problem (which must be common). I use Watir-webdriver for testing of a page that due to AJAX implementation loads portion-by-portion for very long time (more than 5 min). I need to be able just to sample this page for a limited time (20-40 sec) and to be able to analyze the information that is loaded during this short time. However, as I know, there is no straightforward direct mechanism to tell Watir::Browser to stop. I can use the Timeout, but although my script gets the control after rescue, it is impossible to interrogate the browser and verify the information that it is able to received during the timeout window. All I can do at this point is to kill the process and restart the browser as discussed here: Make headless browser stop loading page and elsewhere.
The code below illustrates my situation. In this example I have a global timeout (30 sec) and a local timeout (15 sec) used for reading the page. It never gets to b.text call; the script just outputs the first exception after 15 sec and then it keeps waiting for the browser to be released and after the global timeout of 30 sec prints the second exception message.
Time out. Got into exception branch
Dropped to bottom rescue.
The end.
I also tried to send an 'escape' key to the browser, but any communication with it while it is in the goto method is impossible. Any tips and suggestions will be appreciated!
require 'watir-webdriver'
require 'timeout'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 30 # Set the global timeout
b = Watir::Browser.new :chrome, :http_client => client
my_url = '...here is my address...'
begin
begin
Timeout::timeout(15) { b.goto my_url } # Access the page with local timeout
b.close # if all is unbelievably good and the page is loaded
rescue Exception => e
puts 'Time out. Got into exception branch'
if b.text.include? 'my_text' # NEVER GETS HERE
puts 'Yes, I see the text!'
else
puts 'I do not see the text.'
end
end
rescue Exception => e
puts 'Dropped to bottom rescue.'
end
puts 'The end.'
Watir relies on Selenium WebDriver to handle calls to the browser. At this time all browsers require that the document.readyState of the current frame return "complete" before returning control to your code.
A recent update to the webdriver specification appears to allow for the possibility of a browser driver implementing a page loading strategy that is not blocking, but it is not a requirement and is not supported at this time.
https://w3c.github.io/webdriver/webdriver-spec.html#the-page-load-strategy

what can watir's new "alert API" actually do?

I just read that new Watir version (3.1.0) has "support for Alert API". What can I actually do with this API?
Is it meant for dealing with javascript popup windows? If so, what command can I use to click "OK" in popup window? Or how can I read title & text of popup window?
I tried "browser.alert.ok" after updating Watir to 3.1.0 but just got some error message...
(There is not yet any clear documentation about this, and trying to interpret the source code is not my strongest side...)
Alert API is for dealing with JavaScript popups.
browser.alert.ok should work. Could you provide the error message?
I am not sure about getting popup title, but you can get it's text with browser.alert.text
More information: http://watir.github.io/docs/javascript-dialogs/
browser.alert.ok should work. Could you provide the error message?
Sure. First, here is Watir code that I used:
require 'watir'
b = Watir::Browser.start "www.w3schools.com/js/tryit.asp?filename=tryjs_alert"
b.maximize
b.frame(:name, "view").button(:text, "Show alert box").click_no_wait
sleep 2
b.alert.ok
And here is the error message:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/functions.rb:319: [BUG] Segmentation fault
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
-- control frame ----------
c:0012 p:---- s:0052 b:0052 l:000051 d:000051 CFUNC :enum_child_windows
c:0011 p:0065 s:0046 b:0046 l:002478 d:002478 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/win_32/functions.rb:319
c:0010 p:0015 s:0038 b:0038 l:0018e4 d:0018e4 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/win_32/functions.rb:200
c:0009 p:0025 s:0033 b:0033 l:000032 d:000032 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/win_32/functions.rb:147
c:0008 p:0035 s:0025 b:0024 l:000023 d:000023 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/win_32/window.rb:247
c:0007 p:0017 s:0020 b:0020 l:000019 d:000019 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/rautomation-0.7.2/lib/rautomation/window.rb:220
c:0006 p:---- s:0015 b:0015 l:000014 d:000014 FINISH
c:0005 p:0079 s:0013 b:0013 l:000012 d:000012 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/watir-classic-3.1.0/lib/watir-classic/dialogs/alert.rb:38
c:0004 p:0011 s:0010 b:0010 l:000009 d:000009 METHOD C:/Ruby192/lib/ruby/gems/1.
9.1/gems/watir-classic-3.1.0/lib/watir-classic/dialogs/alert.rb:27
c:0003 p:0107 s:0007 b:0007 l:00043c d:0018c0 EVAL C:/watir_testit/pop.rb:6
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:00043c d:00043c TOP
-- Ruby level backtrace information ----------------------------------------
C:/watir_testit/pop.rb:6:in <main>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.1.0/lib/watir-classic/dialog
s/alert.rb:27:inok'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.1.0/lib/watir-classic/dialog
s/alert.rb:38:in dialog'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/window.rb:
220:inmethod_missing'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/window.rb:247:in child'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/functions.rb:147:inchild_window_locators'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/functions.rb:200:in control_hwnd'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/functions.rb:319:infind_hwnd'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rautomation-0.7.2/lib/rautomation/adapter/wi
n_32/functions.rb:319:in `enum_child_windows'
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Resources