Open a new window with Ruby - 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.

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

how to open new tab in chrome using ruby script in selenium web driver [duplicate]

This question already has answers here:
How to open a new tab using Selenium WebDriver in Java?
(36 answers)
Closed 8 years ago.
I am new to Ruby language. i want to work with chrome browser using selenium web driver. i am trying to open new tab in chrome browser. but i am unable to get.could you please check below code once.suggest me if anything wrong
require 'selenium-webdriver'
$driver = Selenium::WebDriver.for :chrome
$driver.navigate.to "http://www.google.com/"
$driver.manage.timeouts.implicit_wait = 30
body = $driver.find_element(:tag_name,'body')
body .send_keys(:control,'t')
$driver.navigate.to "http://www.ask.com/"
output: New tab in chrome is not opened,second url also opened in the same page which is already opened with first url.
You should use get method to open URL and also I have corrected your find_element method , Please try below updated code :
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.get('http://google.com/')
driver.manage.timeouts.implicit_wait = 30
body = driver.find_element(:tag_name => 'body')
body.send_keys(:control, 't')
driver.get('http://www.ask.com/')
Hope above will solve your issue.

Unable to click on a popup with id's using selenium webdriver and ruby

Following is the code i am using
def self.yes_publish
sleep 5
driver.find_element(:id, 'dialogConfirmChanges-publishButton').displayed?
WAIT.until { driver.find_element(:id, 'dialogConfirmChanges-publishButton') }.click
puts driver.find_element(:id, 'embed-left-center-part').displayed?
end
But i am unable to click on it. This id works fine in irb.
I get an error modal dialog present, as webdriver is unable to locate element it closes to window after a particular timeout.
This popup is to publish changes made on a page.
xpath = .//*[#id='dialogConfirmChanges-publishButton']
You have to use the switch_to method to deal with the pop-up. Look at the documentation of JavaScript dialogs :
You can use webdriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.
Note: At this time alert handling is only available in Firefox and IE (or in those browsers through the remote server), and only alerts that are generated post onload can be captured.
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"
driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
a.dismiss
else
a.accept
end
EDIT
As per the comment box HTML given by you..I think below should work :
driver.find_element(:xpath,"//div[#class='ui-dialog-buttonset']/button[#id='dia‌​logConfirmChanges-publishButton']").click

How to use same browser window for automated test using selenium-webdriver (ruby)?

I am automating test cases for a website using selenium-webdriver and cucumber in ruby. I need each feature to run in a particular order and using the same browser window. Atm each feature creates a new window to run test in. Though in some test cases this behavior is desired- in many cases it is not. From my research so far it seems there are mixed answers about whether or not it is possible to drive the same browser window with selenium throughout test cases. Most answers I have run into were for other languages and were work arounds specific to a browser (I am developing my test while testing IE but will be expected to run these test in other browsers). I am working in Ruby and from what I have read it seems as though I'd have to make a class for the page? I'm confused as to why I would have to do this or how that helps.
my env.rb file:
require 'selenium-webdriver'
require 'rubygems'
require 'nokogiri'
require 'rspec/expectations'
Before do
#driver ||= Selenium::WebDriver.for :ie
#accept_next_alert = true
#driver.manage.timeouts.implicit_wait = 30
#driver.manage.timeouts.script_timeout = 30
#verification_errors = []
end
After do
##driver.quit
##verification_errors.should == []
end
Some information I've gathered so far of people with similar problems:
https://code.google.com/p/selenium/issues/detail?id=18
Is there any way to attach an already running browser to selenium webdriver in java?
Please ask me questions if anything about my question is not clear. I have many more test to create but I do not want to move on creating test if my foundation is sloppy and missing requested capabilities. (If you notice any other issues within my code please point them out in a comment)
The Before hook is run before each scenario. This is why a new browser is opened each time.
Do the following instead (in the env.rb):
require "selenium-webdriver"
driver = Selenium::WebDriver.for :ie
accept_next_alert = true
driver.manage.timeouts.implicit_wait = 30
driver.manage.timeouts.script_timeout = 30
verification_errors = []
Before do
#driver = driver
end
at_exit do
driver.close
end
In this case, a browser will be opened at the start (before any tests). Then each test will grab that browser and continue using it.
Note: While it is usually okay to re-use the browser across tests. You should be careful about tests that need to be run in a specific order (ie become dependent). Dependent tests can be hard to debug and maintain.
I had a similar problem in creating a spec_helper file. I did the following (simplified for locally-run firefox) for my purposes and it works very, very reliably. RSpec will use the same browser window for all it blocks in your _spec.rb file.
Rspec.configure do |config|
config.before(:all) do
#driver = Selenium::WebDriver.for :firefox
end
config.after(:all) do
#driver.quit
end
end
If you switch to :each instead of :all, you can use a separate browser instance for each assertion block... again, with :each RSpec will give a new browser instance for each it. Both are useful depending on the circumstance.
As the answers solve the problem but do not answer the question "How to connect to an existing session".
I managed to do this with the following code since it is not officially supported.
# monkey-patch 2 methods
module Selenium
module WebDriver
class Driver
# Be able to set the driver
def set_bridge_to(b)
#bridge = b
end
# bridge is a private method, simply create a public version
def public_bridge
#bridge
end
end
end
end
caps = Selenium::WebDriver::Remote::Capabilities.send("chrome")
driver = Selenium::WebDriver.for(
:remote,
url: "http://chrome:4444/wd/hub",
desired_capabilities: caps
)
used_bridge = driver.bridge
driver.get('https://www.google.com')
# opens a new unused chrome window
driver2 = Selenium::WebDriver.for(
:remote,
url: "http://chrome:4444/wd/hub",
desired_capabilities: caps
)
driver2.close() # close unused chrome window
driver2.set_bridge_to(used_bridge)
driver2.title # => "Google"
Sadly this did not test work between 2 rescue jobs, will update this in the future when I made it work for my own use case.

Suppress auto-closing window in Watir

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

Resources