How to close facebook's chatbox in Watir - watir

I'm currently struggling atm on closing facebook's chatbox whenever my program goes to a page's page (intended kek). It pops up after a few secs upon page load completion. I cant seem to access elements inside it even if its already loaded specifically the "x button" at the top right as long as the chatbox has focus (header is blue).
#b.a(href: "https://www.facebook.com/"+handle+"/?ref=br_rs").click()
#b.a(aria_label: "Close tab").wait_until_present.click()
Running the 2nd line returns a timeout error even though Im staring straight at it for 30secs. Magically (at least for me) the 2nd line runs if the chatbox loses focus (turns gray, mostly when i tab out). I've tried setting focus on some of the main page's element but to no avail. I can also close it if i do a send_keys but It requires me to add a sleep for like 3 seconds or so till the chatbox automatically opens upon page load but I heard adding hard coded sleep is not recommended. Any help? Thanks in advance.

Tried this and it worked on Windows 10, watir 6.12, chromedriver 2.41, Chrome 68.0.3
b.a(:'aria-label' => 'Close tab').click
For some reason it did not on my other machine, Ubuntu 16.04, Watir 6.11, chromedriver 2.40, Chrome 67.0.3
It magically worked after this
require 'watir'
p = Selenium::WebDriver::Chrome::Profile.new
p['enable_aria_label'] = true
b = Watir::Browser.new :chrome, :profile => p
sleep 40 #logged in manually and opened chat box
b.a(:'aria-label' => 'Close tab').click
sleep 20
It closes first chat box it finds. You can close specific one via
b.as(:'aria-label' => 'Close tab')[0].click
Maybe it will work for you too.

Related

Detect that automated Chrome page has been closed

I have written code to automate entries into a survey using Excel VBA with selenium controlling chrome.
If the user closes the driven chrome window then currently I get a vba error when the next element can't be found.
I want to write some code to detect the chrome window is no longer present and then close down the vba code smoothly.
My idea was to try to find the tab title and execute the closure code if it did not exist - meaning I got the chrome not available error. The code below seems to work but there is a 13 second wait whilst it looks for the page title. Shortening any of the 4 Timeouts available in selenium basic does not seem to change this wait.
My questions are:
Is there a better way to check if the driven chrome window has closed?
Is there a way to shorten the period of time before the error is thrown?
On Error Resume Next
tabTitle = ch.Title
If Not tabTitle Then
Debug.Print "Closed"
End If
On Error GoTo 0
Many thanks
The only way I know of is to catch UnreachableBrowserException. I am not sure what Selenium version you are using, but in Java, you would surround your code in a try/catch
try {
// do your Excel thing here
} catch (UnreachableBrowserException e) {
// execute your alternate plan
}
The problem is that this could happen at any time, anywhere. You can simply put this everywhere on the code. If this is part of a fully-automated solution, why not simply run your script in headless mode. This will prevent the root cause of the problem which is a user unexpectedly closing the browser.

How do I resize an open window in WebdriverIO?

I am using WebdriverIO for automation and am required to resize an open window to check the display of various UI elements compared to a maximized display.
I've tried each of the following separately with no success:
browser.setViewportSize({width:1200,height:662})
browser.windowHandleSize(1200, 662);
I also have checks to make sure the window has been properly resized:
expect(browser.getViewportSize('width')).to.be.lessThan(1300);
expect(browser.getViewportSize('width')).to.be.greaterThan(1000);
Unfortunately, nothing happens visually and the expects fail, as expected. I'm not sure if I'm not understanding how to use setViewportSize or windowHandleSize properly (maybe they're only intended to be used to resize new windows rather than existing ones?), or if this functionality exists in WebdriverIO at all.
I've used the following command in my WebdriverIO Config in setup hooks, but also just tested it in the middle of a test that's already running to both resize it smaller and then back to larger. Hope it helps!
browser.windowHandleSize({width:700, height:500});
browser.windowHandleSize({width:1280, height:870});
http://webdriver.io/api/protocol/windowHandleSize.html
Just to note as there have been several updates, please make sure your browser drivers, WebdriverIO, and Selenium Server are the latest versions!
In case someone else comes here and is confused, the API call is setWindowSize, with the arguments (width, height):
browser.setWindowSize( 700, 500 );
See: https://webdriver.io/docs/api/browser/setWindowSize.html
Note that the above command resizes the window but not the content. The setWindowRect command from the default WebDriver interface sets an absolute content size:
browser.setWindowRect( 0, 0, 700, 500 );
See: https://webdriver.io/docs/api/webdriver.html#setwindowrect

MAC OS X difference between "open APP.app" and "APP.app/content/MacOs/APP" shell scripts

Somehow I get a problem, if I call an application twice or several times but only one instance should be running (which is desired).
First some (probably necessary) background information:
working on MAC OS X El Capitan (10.11.6)
I've got an Application made by node.js, electron and build by "npm build -m" (let's call it APP.app)
I installed the application App.app into program-folder by opening the built dmg-file and move it into the program-folder
Then I start the application App.app by click on the App.app-icon in program-folder
The application starts with a visible Window and also has an hidden background process running
If I close the visible Window, the App.app-Icon remains in the dock (which is okay, since the background-process is still running)
Now (and this is the difference to the windows build and running in windows), if I click again on the App.app-Icon in the program-folder, I only get a focus on the already running application INSTEAD of opening the window and close the old application (I can see te focus switch/activation by the switching menu name beside the apple-logo on top of the screen; it becomes "App")
the closing of the previous running instance is defined in electron code in the init-method as follows:
var shouldQuit = app.makeSingleInstance(function (commandLine, workingDirectory) {
console.info('starting new instance');
initInternal(commandLine);
// Someone tried to run a second instance, we should focus our window.
var windows = options.closeWindowsOnRestart ? Browser.getAllWindows() : appWindows.slice();
windows.forEach(function (val, index) {
val.close();
});
readyCallback();
});
So, I did some homework and could figure out following:
my App.app-icon-click should look something like open /Applications/App.app
with this, I get the same problem as descriped above if I call it again (it only focusses the already opened window)
Now comes the funny part. If I open the application by calling directly /Applications/App.app/content/MacOS/App.app the old application-instance is closed and new App.app-application is started
I read through open-manual and could figure out, that if I use the -n-flag, the application starts succesful a new instance, too. (open /Applications/App.app -n)
I was wondering WHY? Do you have any clues? What is the difference between open and directly call of the application?
I suggest that the info.pklist in App.app-package is making the open-call different to the direct call of App.app.
BTW:
Unfortunately I already tried to add try-catch-blocks to debug the problem, but open does not give an output to terminal, it just opens the call whereas the direct call does not throw an error and everything works fine.
Anyway, I believe it's more a MAC OS X problem than an App.app-problem.
Hopefully there's somebody with same problems and a solution for me
Don't hesitate to ask for more details, if needed.
Short Version:
open starts the application with the LaunchService (and possible
added launch-parameters in info.plist)
whereas direct call of the
application just starts the application iself without any other
launch-options
*Long Version *
I read through the open manual (man open; Source: https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/open.1.html) and could figure out, that "the default application as determined via LaunchServices is used to open the specified files" if you click on an item.
So I read through the LaunchService-documentation and could figure out following:
"Opening Items by File-System Reference [...] default way:
If the designated item is an application: [...]
If the application is already running, it is activated (brought to the front of the screen) and sent an 'rapp' (“reopen
application”) Apple event."
(Source: https://developer.apple.com/library/content/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCTasks/LSCTasks.html#//apple_ref/doc/uid/TP30000999-CH203-TP9)
This reflects exactly my watching that if I click on the icon the second time, the app is just focussed/activated.
Thus I need the possibility to tell the application to open a new instance (oapp-event) instead of activating the already opened application (rapp-event)
Further reading lead me to following informations:
"Launch Options When opening an application (whether by itself or to
open one or more documents or URLs), you can specify various launch
options to control the manner in which it is launched or activated.
These can include: [...] Whether to launch a new instance of the
application, even if another instance is already running"
(Source: https://developer.apple.com/library/content/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCConcepts/LSCConcepts.html#//apple_ref/doc/uid/TP30000999-CH202-BABBJHID)
Thus I only need to add the "launch option" to define, that a new instance should be created instead of activating the existing one. But there's not written what the launch-option for it is and how they will be applied to the application (I suggest it belongs into info.plist-file).
So at least this is the answer to my original question, so I posted it here.
open - starts the application with the Launch-Service (and the defined option-parameters in info.plist)
whereas direct call of the application just starts the application iself without any other launch-options

Watir-webdriver keeps opening multiple browsers

Hello StackOverflow members,
I've been searching the site (and the rest of the web) for an answer to this question, but all my search queries returned the awesome features of Watir... I seem to be one of the few people running into this particular problem. I hope someone out there has an easy answer for me :)
I'm working on website test automation. The current test set is written with Cucumber/Ruby/Selenium-Webdriver/Capybara. I'm personally interested in switching to Watir-Webdriver in combination with Cucumber and Ruby, but I'm struggling with the following:
Every time I run my cucumber test, Watir opens not one, but TWO browser screens. It seems to want to initiate a blank screen (which just goes to the site I configurated as default), plus another browser screen in which the actual test steps are executed.
Keep in mind, I'm rather new to this and I'm having this trouble when simply following a beginners tutorial. Nothing fancy as of yet.
In my 'Support/env.rb' file, I have the following:
require "cucumber"
require 'watir-webdriver'
app_host = ENV['apphost']
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
at_exit do
#browser.close
end
The first bit in my steps file (GoogleSearch.rb -- yes it's that basic):
require_relative "../support/env"
Given(/^that I have gone to the Google page$/) do
#browser.goto 'http://www.google.com'
end
Now, when I run this test, I expect just ONE browser to be initiated. But instead, the automation initiates TWO browser screens. One just stays in the background doing nothing, the other contains the test steps.
Again, I've searched for a while now (which I'm usually pretty good at), but I haven't found the answer to my problem anywhere. The only way I got it to work, is to start with a step in my steps file, initiating a browser (instead of doing this in the env.rb file). But I don't want to start each test with opening a browser..
Any help would be very much appreciated. If any more information from me is required, I'll update as soon as I can.
Thanks in advance!
The problem is that env.rb is being loaded twice:
It is automatically included when running the cucumber command
It is being included a second time in GoogleSearch.rb when calling the line require_relative "../support/env".
As a result, each of the hooks is registered twice. In other words, Cucumber is seeing the hooks to run before each scenario as:
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
As you can see, Watir::Browser.start is called twice resulting in the two browsers. The first one is not used since the second call uses the same variable.
To solve the problem, simply remove the require_relative "../support/env" line.
Note that this will only address the issue with opening two browsers for each scenario. You will notice that you will still get a new browser for each scenario and that only the last browser gets closed. If you only want one browser for all scenarios, you should look at the global hooks.

"object invoked has disconnected from its clients" error message is displayed while working with popup windows

I am trying to work with popup windows. I have gone through the site "http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups". I have modified the "ie-class.rb" by adding the code mentioned in the solution #7 of the site.
Also I have tried the example mentioned below :
require 'watir/ie'
require 'win32ole'
iewin = Watir::IE.new
iewin.goto("http://www.w3schools.com/js/tryit_view.asp?filename=tryjs_confirm")
iewin=Watir::IE.attach(:url, /w3schools/)
iewin.bring_to_front
iewin.maximize
iewin.button(:value, "Show a confirm box").click_no_wait
txt = iewin.clickprompt("OK") # waits for popup and click ok
puts txt #prints the popup text
txt1=iewin.clickprompt("OK")
puts txt1
iewin.close
On executing the following code, popups are handled but produces the following error :
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:319:in `method_missing': unknown property or method: `name' (NoMethodError)
HRESULT error code:0x80010108
The object invoked has disconnected from its clients.
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:319:in `exists?'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:406:in `close'
from popup.rb:50:in `<main>'
Press a button!
You pressed OK!
But when I remove the command "iewin.close", error message is not displayed.
"iewin.close" command closes the browser and then there is nothing for watir to perform then why is the error message displayed.
Please suggest.
It appears that you are using ruby 1.9.2 with Watir. The watir installation page recommends using Ruby 1.8.7. That should fix the problem.
Good luck,
Dave
Notice that the output you have is not in order. You are seeing the error from closing the browser, BEFORE the output from your Puts.
Not sure if this is a treading issue or what, but in any event it looks from the output like the window is attempting to close before stuff that is dependent on it has finished. Perhaps try inserting a brief sleep for a second or five right before you try to close the window and see if you are still having issues.
Dug into this a bit. It appears that Ruby 1.9 is throwing a "NoMethodError" instead of a "WIN32OLERuntimeError". The Watir IE code reads as such
def exists?
begin
!!(#ie.name =~ /Internet Explorer/)
rescue WIN32OLERuntimeError
false
end
end
To fix this a second rescue can be added to the call
def exists?
begin
!!(#ie.name =~ /Internet Explorer/)
rescue WIN32OLERuntimeError
false
rescue NoMethodError
false
end
end
I don't know the side effects of adding such a call, but it does suppress the close warning.

Resources