Suppress auto-closing window in Watir - ruby

I am using Watir with Chromedriver to automate form submission on some website. I have to login and submit multiple forms. The problem is, when I click the submit button the page the page automatically closes, so when I goto('next_url') I get this error:
/Users/jackz/.rvm/gems/ruby-1.9.3-p327/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok': 'auto_id' does not refer to an open tab (Selenium::WebDriver::Error::UnknownError)
The Watir instance is still there, but the window is closed. I could create a new instance every time, but then I would have to login again every time and this would take longer.
So how can I either:
Open a new window in the same Watir instance
or
Suppress the window from closing after I submit
require 'watir-webdriver'
#b = Watir::Browser.new :chrome
#b.goto(URL)
#b.buttons.first.click
#this is when the window closes
#b.goto(NEW_URL)
#then I get an error
Thanks

I figured out an answer to my own question. I can open a new window in Watir using javascript:
b = Watir::Browser.new
b.execute_script("window.open()")
b.windows.last.use
This opens a window where I can fill out the form, then when the window automatically closes I still have the original window to work with. Probably not the best solution, but it works for now.

Add this to your existing code if your using chrome. else, modify accordingly as per your browser.
This will keep the window open, and the current watir session active.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => false})
browser = Watir::Browser.new :chrome

What Mrityunjeyan suggested above was in a right direction, but you need to change a few things to make it work.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => true })
b = Watir::Browser.new('chrome', desired_capabilities: caps)
Check the documentation here.
https://sites.google.com/a/chromium.org/chromedriver/capabilities

Related

Closing then opening browser with Watir

I want to close the current browser, open a new one and let it continue it's work.
It looks something like this:
browser = Watir::Browser.new :chrome
.....
browser = browser.close
browser = browser.new
..
But after closing the browser it returns
uninitialized constant Browser (NameError)
Thanks :)
when you call browser.close, the browser object gets removed after the actual browser is closed. To make a new one you need to use the same syntax you did the first time (as indicated by Justin in the comments to your question)
browser = Watir::Browser.new :chrome

changing chrome prefs in `ruby` after starting the browser

I have created a browser using
`#prefs = {download: { prompt_for_download: false, default_directory: "#{Dir.home}/new2" } }
Watir::Browser.new :chrome, prefs: #prefs`
and when my script runs, it initiates another window while clicking Print option on Chrome
I want to change the prefs of the opened window to #prefs once it opens
Is there any way of writing it in ruby, watir, page-object
I have no idea how to change the prefs of newly opened window
Those things can only be set at the time of browser instantiation. They are the parameters used by the browser driver when creating the session and can not be changed on an active season.

Open a new window with Ruby

I want to open a new window using the openWindow() method that I can see in the rdoc, but whenever I attempt to run my code, I am told that the method does not exist.
require 'rubygems'
require 'selenium-webdriver'
$browser = Selenium::WebDriver.for :firefox #I've tried chrome too to the same effect
$browser.navigate.to("http://google.com")
$browser.openWindow("http://cnet.com","ASDF") #This doesn't work.
$browser.open_window("http://cnet.com","ASDF") #This doesn't work either.
It would be greatly appreciated if someone could set the record straight on how to use this.
As detailed in this article, the correct way to use the API is:
#driver.get 'http://the-internet.herokuapp.com/windows'
main_window = #driver.window_handle
#driver.find_element(css: '.example a').click
windows = #driver.window_handles
windows.each do |window|
if main_window != window
#new_window = window
end
end
#driver.switch_to.window(main_window)
#driver.title.should_not =~ /New Window/
#driver.switch_to.window(#new_window)
#driver.title.should =~ /New Window/
Which will have the following behavior:
Load the page
Get the window handle for the current window
Take an action that opens a new window
Get the window handle for the new window
Switch between the windows as needed
I am not sure whether you could use
openWindow method but
To open a new window you will have to open a new instance of your firefox browser again
so ,try doing something like
$browser = Selenium::WebDriver.for :firefox
$browser.navigate.to("http://google.com")
$browser_new = Selenium::WebDriver.for :firefox
$browser_new.goto("http://cnet.com")
I don't know Selenium, but according to your own question the name of the method is open_window not openWindow.

Watir-webdriver: Script working on Firefox but failing on Chrome or Opera. why?

I'm testing this on ark.com..
the following code works perfectly for firefox with watir-webdriver but gives me an error for google chrome and opera browsers respectively.
heres the code:
require "rubygems"
require "watir-webdriver"
b = Watir::Browser.new :ff
b.goto "http://www.ark.com"
# Signing in
7.times{b.link(:text, "Sign in").flash}
sleep 3
b.link(:text, "Sign in").click
sleep 3
# Popup
b.text_field(:name, "login_email").set "email#gmail.com"
b.send_keys :tab
b.text_field(:name, "login_password").set "password"
b.button(:value, "Sign in").click
puts b.title
changing the code as follows gives me errors:
b = Watir::Browser.new :chrome
or
b = Watir::Browser.new :opera
I get the following error message:
You may only interact with visible elements (Selenium::WebDriver::Error::ElementNotVisibleError)
I've tried the following stack overflow suggestion to no avail How do I use Watir::Waiter::wait_until to force Chrome to wait?
Also, my code works perfectly in firefox but not in other browsers, why might this be the case?
Any help would be appreciated. Thanks.
As mentioned in some other posts, you can full screen the browser with the following:
screen_width = b.execute_script("return screen.width;")
screen_height = b.execute_script("return screen.height;")
b.driver.manage.window.resize_to(screen_width,screen_height)
b.driver.manage.window.move_to(0,0)
Then like anonygoose said, the sign in link should be there (assuming your screen isn't too small).
Looking at the website in both Chrome and Firefox I'd say there's some sort of bug or unwanted feature going on with it.
If the web browser you're using is too narrow in width the sign-up button disappears completely.
I'd say that's why you're getting strange errors.
Test this by maximizing the Chrome browser as soon as it launches. From my testing this allows the signup button to be found.
I am not sure of a way to maximize either browser by default, but perhaps someone else on here will know or be able to suggest a way to go about it.

How do I prevent Watir from auto closing firefox?

I am automating test cases using Ruby and Watir. One of my methods opens the web browser, but as soon as my script leaves the "open browser" method and goes to the next method (filling out forms within the browser), the browser auto closes. When I automate using the IE browser it will not close until it hits the IE.close statement, but with firefox it closes automatically. Is there any way to avoid this?
Code:
require 'rubygems'
require 'watir-webdriver'
require 'rexml/document'
def openbrowser
$user = "user"
$pass = "password"
ff = Watir::Browser.new :firefox
ff.goto "http://<some website>"
ff.text_field(:name, "username").set($user)
ff.text_field(:name, "password").set($pass)
ff.button(:value,"Sign In").click
ff.link(:xpath => "html/body/div[1]/div[2]/a[1]").click
ff.text_field(:name,"userID").set($ID)
ff.button(:value,"View User").click
ff.link(:xpath => "html/body/div[1]/ul[1]/li[2]/a").click
sleep 20
end
# Run Program
openbrowser
I was attempting to run this code in NetBeans, so this behavior may just be specific to that editor.
There were two causes I have found for it shutting down, first is when there is an error in the code, the browser will shut down as soon as an exception is thrown. Second, the browser shuts down at the end of the program if there is no sleep established.
I use the Test Unit class, I open the browser in the setup method and generally close it down in the teardown method, this works for me in IE & Firefox.
More information here, http://wiki.openqa.org/display/WTR/Test+Unit

Resources