In Firewatir, how can I interact with the Firefox print dialog? - ruby

I'm trying to test a page that has the print dialog appear immediately upon accessing it. All I need to do is close the dialog or click Cancel on it so I can interact with the page behind it. I've tried to figure out how to do this, but everything I've found indicates that it isn't possible in Firefox.

If you're on MacOS or Linux, not sure what to tell you...I bet it's possible, but I need to learn how myself. :)
If you're on Windows, I do have some suggestions. I suggest checking out the RAutomation gem, which is good for tasks like this (locating and clicking Cancel in the Firefox Print dialog):
https://github.com/jarmo/RAutomation
Or you can try using AutoIt. Installing Watir also installs a copy of AutoItX3.dll, which you can use for simple GUI automation like this.
To see if AutoIt is ready to use on your system, try this out in irb - this code will wait 10 seconds for the Firefox Print dialog to appear, then click the Cancel button:
irb(main):001:0> require 'win32ole'
=> true
irb(main):002:0> autoit = WIN32OLE.new('AutoItX3.Control')
=> #<WIN32OLE:0x3c61ce0>
irb(main):003:0> result = autoit.WinWaitActive('Print', '', 10)
=> 1
irb(main):004:0> result = autoit.ControlClick('Print', '', 'Cancel')
=> 1
If the "WIN32OLE.new('AutoItX3.Control')" line raises an exception, you may need to use regsvr32.exe to register the DLL. For example, here's how to do that on Win7:
Start an elevated cmd.exe
regsvr32 C:\Ruby187\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\AutoItX3.dll
(Note that you might need to change the path above if your Ruby installation is not in C:\Ruby187 or you have a different Watir version than 1.6.5.)
AutoIt documentation is here:
http://www.autoitscript.com/autoit3/docs/
One last thing to watch out for:
If the statement in your code that brings up the Print dialog blocks until the dialog is closed, things get a little more complex. You'll need to use threads or an external process to handle the dialog (since I don't think FireWatir has a click_no_wait method yet).

Related

Selenium WebDriver in Ruby: Preventing the test from closing the browser window at end

I appear to be having the exact opposite problem many other people have - in that my Selenium tests in Ruby will close the browser window at test end, no matter what the end result is. Pass or fail, it will always close the browser. I would like to stop this.
Context:
Previously I coded tests in Java using IntelliJ IDEA. Browser windows for Selenium tests in this case would NOT close at all period unless you used driver.quit(). This is actually quite useful as it means that the browser window would stay open if the test failed - which meant I could look at where it stopped in the browser and help figure out why it failed. This was also useful for test writing as it meant that I could essentially pick up where I left off to write the next block instead of having to keep a parallel tab going in another browser by hand to get the next set of selectors in the given screen.
I've found in Ruby using RubyMine that the browser will close when the test ends in any capacity. This is a bit of a problem. While technically I could take a screencap on failure, it'd mean that I'd have a harder time retracing why it failed (back button on browser, typing in fields to work out if a quirk in our UI caused it, etc). And of course, screencaps take up hard drive space. ;)
I've tried the detach=true (and True) command switch for Chrome and that has not worked.
Setup:
IDE: RubyMine
Gems: selenium-webdriver
Browser: Chrome, using ChromeDriver. (In Ruby this just involves using WebDriver)
OS: OSX
Not headless, using no other frameworks/testing environments. It is quite literally a few lines of setup and then hitting the run button in RubyMine in a bog-standard .rb.
Summary:
I haven't been able to find any existing questions here or in other places online for Ruby specifically for keeping a Selenium test in RubyMine from closing the window on test end. "Test end" in this case refers to success (reaching the end of the .rb) or failure (Tracebacks, in this case). I would prefer that the window would stay open until it'd hit a driver.quit line. Is there any way I can set this up?
Thank you very much. I hope this isn't redundant. I also hope this will help other testers in the future :)
You can use the :desired_capabilities to set this flag:
caps = Selenium::WebDriver::Remote::Capabilities.chrome("goog:chromeOptions" => {detach: true})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
Note that old examples will be using "chromeOptions", but for newer Chrome versions, it will need to be "goog:chromeOptions".
#JustinKo has posted an answer here
Chrome detach option is no longer working
You can pass Chrome Options using the following format:
browser = Watir::Browser.new(
:chrome,
'goog:chromeOptions' => {detach: true}
)
The answer from #Justin Ko worked for me. However at the time of posting I am getting a warning telling me to use
capabilities: caps
instead of
desired_capabilities: caps
as it is now currently deprecated at the time of posting.
I do not have enough reputation to be able to comment so I am posting this as an answer.

Close window through Capybara

I am trying to launch the browser for the capybara automation. The browser is auto populating a "Error window" (non-browser window).
I need to "close/click ok" on the window to open the browser. I tried to handle this popup with the "AutoIT" executable file. When i run the AutoIT file manually, the executable file handles it successfully.
I am not able to execute through the capybara script. The code is as below
ses = Capybara::Session.new(:selenium)
IO.popen('c:\ruby\handler.exe') #- Tried this step to execute AutoIT .exe file before visit url step.
No luck here. The popup appears only when referencing with the object.
ses.execute_script "window.close()" #- Tried this step to close the window with the ses object. no luck in this too.
ses.visit "https://google.com"
Is there a way to close the window programmatically?
It seems that you should be able to register a custom selenium driver using the Chrome browser and specify the command-line option to disable all extensions, with the following driver registry code:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome,
:switches => %w[--disable-extensions])
end
ses = Capybara::Session.new(:selenium)
...
I put this together using information here and the list of available command line options here (I did not test this myself though).
There are a few approaches to a popup error like that:
1) The easiest one is to do your approach - execute an autoit script before calling capybara to visit the site that results in the error popping up. You should ensure that the autoit script you are executing waits for the window to appear before trying to close it (see: https://www.autoitscript.com/autoit3/docs/functions/WinWait.htm for reference).
2) You could execute another ruby script/thread (keep in mind threading in ruby is a bit complicated) that would execute the autoit script in the background (in a loop) and wait for a succesfull response.
3) You could try to disable whatever is causing the error to pop up in your browser.
I will be able to provide some code following approaches 1 and 2 in a few hours if you will still have problems solving the issue.
The error Failed to load Extension. Loading of unpacked extensions are disabled by administrator. indicates that your system was set-up to disable extensions with Chrome. So even if you manage to close it, you probably won't be able to automate Chrome with Selenium since it needs to start Chrome with the driver as an extension.
Your best chance to make it work is probably to disable the restriction or to add the extension to the white list.
Here is a link about this issue:
https://bugs.chromium.org/p/chromedriver/issues/detail?id=639

Can you close all open windows in capybara?

So when I run my tests individually, they work great. However, when I run my feature (multiple tests) my code is failing.
This is because when capybara exists, not all of my windows are closing and therefore my selenium drivers don't know which window to use.
Basically, what is happening is that my test is opening multiple windows. This is happening because my tests are clicking links which open new windows and checking the content in the new windows. Even though I am saying page.quit at the end of each test, this function isn't closing all open windows (closes active window but not the original window). When the 2nd test runs, it uses the originally create window, but when it goes to open the new pop up, it uses the wrong window.
How can I ensure that a new browser is being used during each test and all windows from the previous test are closed.
I am currently using page.reset! and page.quit.
Not sure, but can I say session.quit to close all open browser windows?
This code works:
page.execute_script "window.close();"
I just execute this while in the window I want to close.
I use this code piece to access popups and close them after. It should not be much difference
within_window(page.driver.browser.window_handles.last) do
click buttons and stuff...
...
page.driver.browser.close #closes popup
end
Hope it helps
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.execute_script "window.close()"
end
Did the charm for me. Combination of #Jason and #Justin's answers.
Thanks!
Try the following:
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.quit
end
(I think it will work, but I have not had a chance to test it.)
Try this:
tab_id = page.driver.find_window("http://google.com.au")
page.driver.browser.switch_to.window(tab_id)
page.driver.quit
This worked for me.
In order to have all assertions in spec file, I passed control to new page (window), after executing assertions I closed new page and passed control to original page as bellow:
after(:each) do
expect(#static_page.has_logo?).to eq(true) # assertion in new page
page.execute_script('window.close()') # close new page
switch_to_window(windows.first) # return control to first page
end
Adding this here because this page comes up within the first few Google results for "capybara close all windows".
We had some mysterious feature spec failures that we tracked down to a few that opened app links in second tabs -- since the tabs were just left open, every so often they'd fire a failing AJAX request and the current test would bomb out with a completely unrelated error. The way we fixed that was to add a hook that made sure to close all but the current window after every feature spec:
Rspec.configure do |config|
config.after(type: :feature) do
# Make sure all browser windows except one are closed
windows.reject(&:current?).each(&:close)
end
end
This worked like a charm for us, using Capybara's headless_chrome driver.
Add this to your feature tests, it'll reset the session cookies and start the test on a blank page:
# Window washing - use a clean window before every example
before(:example) do
page.reset!
end

webbrowser disable script debugging in Visual Basic 6

I want to disable script errors from popping up in a VB6 application. (I have VB6 installed on this machine).
Currently, if I navigate to a particular page, it pops up saying "INternet Explorer Script Error: An error has ocurred in the script on this page" ... "Do you want to continue running scripts on this page?"
Setting the webbrowser1.silent to 'true' does not work. Instead all that happens, is instead of displaying an error message, it starts up the actual 'script' debugger, and then exits the program. On a machine without the (visual studio) debugger, it still pops up a message asking to use the debugger, i.e., on Vista, (when silent is set to true).
Manually changing the 'disable script debugging (other)' (and regular one), doesn't seem to working in MSIE (also testing version 6.0 for xp users).
How do I disable script errors?
Thanks in advance!
WebBrowser.ScriptErrorsSuppressed = True
(The above code will Not work in VB6)
(Try this instead)
WebBrowser1.Silent = True
The webbrowser.silent can be reset to true when the application is run if you've set it in the design window. You may have to reset it to true when starting the application (ie: at runtime) and see if that works.
I Think It's That You Need
Write This Code In Your Form_Load Or Everywhere You Want
WebBrowser.ScriptErrorsSuppressed = True

Problem with Pop-up Windows using Selenium

I'm new to the testing world, so my question might seem a lil' bit too naive and stupid. At risk of looking/sounding stupid, my question is this:
I've been trying to test the contents in a pop-up window on my company's web app. I've figured out how to detect the pop-up window for now, but i can't get selenium to 'click' on the link inside of that pop-up window. there are multiple pop-ups in this web app so it's really difficult for a newbie like to create a test case.
I tried the click, clickAndWait, mouseDown and mouseKey as an option but it is still not working. can somebody guide me through this?
TIA,
Angela
When the popup appears you will need to move the context of the script over to the window.
You can do this by using the selectWindow | window_ID_from_the_link and then do the clicking.
If that doesn't work you may need to use the openWindow command to create the popup and then start testing against that.
Use getConfirmation/getassert/getprompt according to the type of the pop up you use .....By default they will be clicked with ok option by the server and you have to consume the message from the pop up for the other selenium commands to work correctly.............
The above suggestion is given from my experience in working with selenium RC used with perl..........
Perhaps you can try the FireFox Plugin. You can click through your application and record your steps. After recording the steps you can easily save it as some sort of file or unittest.
I'm not sure about the command you should use for the popups, maybe the firefox plugin will help in this manner (it will create your commands).
If you created the popup with a div tag, U can use following code to stop the selenium server until the popup opens.
int second = 0;
while(!selenium.IsElementPresent(mylink))
{
if(second >= 5)
break;
Thread.Sleep(1000);
second++;
}
After a popup opens, Now you can click on any link inside the popup.You have to use the below code.
selenium.click("id=popup_link"); (popup_link is the id of the link present on the popup)
Good Luck.
Not sure if this is what you are looking for, but if you want to click on something specific that Selenium is not able to handle - like browser pop-ups or other pop-ups, you can use Sikuli Script. Sikuli does an image comparison and clicks on the same - this is very powerful.
Here is the link: http://www.sikuli.org/

Resources