How do I find out if a watir object's browser is closed or not? (after typing ctrl+C or otherwise) - watir

When I close the browser watir has been using (or hit ctrl+C and close it out that way), I get these types of errors...
b = Watir::Browser.new
#ctrl+C and closes browser window
b.url
Errno::ECONNREFUSED: Connection refused - connect(2)
...when I run something like #url or #goto on the watir object.
I tried methods like #closed? and #closed on the object, but they aren't recognized. Have also tried this:
b.methods - Object.methods
and perused the available methods but nothing has worked so far.
Short of responding to raised error messages with begin/rescue blocks, what methods can I use to determine if the browser window is closed?
A bonus would be a method that would allow me to reinitialize the browser window, keeping any preferences picked up along the way. That would be the best possible solution.

Use exists? method to check whether browser is closed or not, browser.exists? would true if browser is open, If not, it would return false.

Going to answer my own question here because I'm not sure if a straightforward answer is possible. Monkey-patching time.
class Watir::Browser
def try(meth)
begin
self.send(meth.to_sym)
true
rescue
false
end
end
def open?
if try(:exists?)
exists?
else
false
end
end
end
Works no manner which way you closed your browser window.
I may submit a pull request

The correct way to close the browser is browser.quit or browser.close
Then you can call browser.exists? and it will return false. Sending ctrl+C exits your session entirely, so there is no longer anything listening at the url/port you are making calls to, and you will always get that error.

Related

Close cefpython clientand restart a new one

During the application flow I would like to close cefpython running client and open a new one; I've this function
....
while True:
settings = {...}
settings2= {...}
cef.Initialize(settings=settings)
self.BROWSER = cef.CreateBrowserSync(url=url,
window_title="Tutorial",
browserSettings=settings2)
bindings = cef.JavascriptBindings(bindToFrames=False,
bindToPopups=False)
bindings.SetFunction("backend", func)
self.BROWSER.SetJavascriptBindings(bindings)
cef.MessageLoop()
cef.Shutdown()
and in another function I have this call
self.BROWSER.CloseBrowser(True)
Browser start on first run and is closed but it does not restart. If I comment the line
...
cef.MessageLoop()
#cef.Shutdown()
in the first function the browser does restart but it get stuck and I can't use it.
Thanks in advance.
Functions like cef.Initialize and cef.Shutdown can be called only once during app lifetime. You also shouldn't call cef.MessageLoop multiple times. Your code while True doesn't make much sense, because you do not give browsers time to initialize and load. You should use events like LoadHandler.OnLoadEnd or OnLoadingStateChange or others depending on what you're trying to accomplish.

Prevent MySQL Connector/ODBC Data Source Configuration window from appearing when connection fails

On Excel I have a table that uses MySQL Connector/ODBC to get external data from a database.
If the connection fails when I refresh it the MySQL Connector/ODBC Data Source Configuration window appears.
Instead of that I want to present the user with a custom error message.
Checking if the refresh was successful after refreshing is too late to prevent the window so I ping the server before refreshing and cancel the refresh if the ping fails.
My question is if there's a more reliable/standard way to not show the window when there's no connection. I don't want to write the password on my code for security reasons (the connection is made with a DSN) and I don't know if the ping trick will always work or if it might rarely give unexpected errors or results.
Also I think it's technically or theoretically possible that the connection status changes between the ping check and the refresh even though that wouldn't be the end of the world (if that was even possible between the fast running code).
Code example for anyone asking:
Sub Table()
Sheets(1).ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=connection_test;", Destination:=Range("A1")).QueryTable.CommandText = Array("SELECT table_test_0.`column _test` FROM db_test.table_test table_test_0")
End Sub
Sub Refresh()
Sheets(1).ListObjects(1).QueryTable.Refresh
End Sub

How can I make .goto non-blocking?

I'm writing a rails app which fetches text from an HTML page using Watir and Chrome Headless. All good so far!
The problem starts when I request a page which has a long load time to completely load all elements despite the fact that I don't need them.
Current code I use:
browser = Watir::Browser.new :chrome, headless: true
browser.goto(url)
The .goto function call, however, blocks until ALL elements have loaded. That's not really what I need - what I need is for goto to just start fetching the page, then continue running code since I really just want to wait until the text I need is present, then fetch it.
Any ideas?
Goto will not leave the control until 60 seconds, If page load time exceeds 60 seconds, then it would throw the error. And also Watir.default_timeout has nothing to do with Goto's page loading. You need to set the timings for page_load which you can do by directly calling selenium driver as I have done below because Watir hasn't offered any systax for that
Write the below code, you could achieve what you want
begin
b.driver.manage.timeouts.page_load=5
b=Watir::Browser.new
b.goto(url)
rescue #I have written the rescue block here because goto will the error for you If page is not loaded within a given time
end
AND THEN you can write your rest of the code here, for an example,
puts b.span(text: 'something').text
What happens here is, goto will be block the execution of the code followed by goto for 5 seconds, and then it would fall into the rescue block, so program would continue to execute next line as you expected.
With the new w3c webdriver specification, you can set the page load strategy to 'none.' https://w3c.github.io/webdriver/webdriver-spec.html#navigation
Only Firefox and IE might have this implemented already.

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

Linux x11 XGrabKeyboard() cause keyboard to be frozen

I am writing a program which need to listen the user keyboard stroks.
I use function XGrabKeyboard() and this is my code:
XGrabKeyboard(pDisplay, DefaultRootWindow(pDisplay), True, GrabModeAsync, GrabModeAsync, CurrentTime);
XEvent event;
while (true)
{
XNextEvent(pDisplay, &event);
switch (event.type)
{
...
}
}
But it causes the keyboard and cursor to be frozen.
I looked up the man page, it only says: "The third parameter specifies a Boolean value that indicates whether the keyboard events are to be reported as usual."
I tried both true or false or the 3rd param, both GrabModeAsync and GrabModeSync for the 4th and 5th param, but it doesn't work.
After calling XGrabKeyboard(), the keyboard is frozen and mouse click doesn't response.
Any ideas?
XGrabKeyboard() (if successful - be sure to check the return value), redirects all key events to your client.
So if your "..." inside the while(true) does not properly handle those key events, or does not ever ungrab (XUngrabKeyboard) or release sync events (XAllowEvents, only applies to GrabModeSync), then the keyboard would appear to lock up.
The boolean parameter is owner_events which indicates whether to report key events always to the window provided to XGrabKeyboard, or report them to the window they normally would have gone to without the grab. Typically you want False (report to the grab window).
For typical uses of XGrabKeyboard (I don't know your use-case) the parameters you would want are:
grab window = some window in your app that relates to the reason for the grab
owner_events=False to send all events to that window
pointer_mode=Async to not screw with the pointer
keyboard_mode=Async to just redirect all key events and avoid need for AllowEvents
time=the timestamp from the event triggering the grab, ideally, or one generated by changing a property and grabbing the timestamp off the PropertyNotify
But, it depends. To give any definitive answer you'd probably need to post a compilable program, I think the bug is likely in the "..." part of your code. Try narrowing your app down to a single-file test case that can be run by others perhaps. Or explain more why you are grabbing and what you're trying to accomplish in the big picture.
I cant help with the XGrabKeyboard function - I havent used it before and dont know how it works - but I can suggest another way of getting the keyboard events.
When creating my window using XCreateWindow, the last argument is a XSetWindowAttributes object. This object has a member event_mask, which you can use to choose which events your window will receive.
I set mine like this:
XSetWindowAttributes setWindAttrs
setWindAttrs.event_mask = ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask;
That will mean you receive events for keyboard key presses and mouse button clicks if you pass this object to XCreateWindow on window creation.
Also another note you can use XPending(pDisplay) to check if there are still events waiting to be handled - so it could replace true in your while(true) line.
Edit: Also your freezing issue could be that you dont return false anywhere in your while loop? It may be stuck in an infinite loop, unless you just removed that bit for the post. Try replacing true with xpending as I suggested above and it may fix the issue, or just returning false after handling the event, but this would only handle one event per frame rather than handling all the currently pending events like XPending would do, and I assume that is what you want to do.

Resources