Ruby/Selenium Opening URLs In Same Chrome Tab Instead Of Different Tabs - ruby

I am currently writing a program where I need to open two tabs in Chrome with Selenium/Ruby. Both tabs will hold a different url. So far, I have the following code:
$driver.navigate.to "CHROME EXTENSION URL"
body = $driver.find_element(:tag_name => 'body')
body.send_keys(:control, 't')
sleep 15
$driver.navigate.to "WEB BROWSER URL"
Two tabs are created but when I try to invoke $driver.navigate.to "WEB BROWSER URL" , the browser opens the [WEB BROWSER URL] in the first tab where the Chrome extension is. Does anyone know a way to get the two urls in their own respective tabs? Please let me know if I need to provide additional details.

To open a link in a new tab:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://www.google.com"
# open a new tab and set the context
driver.execute_script "window.open('_blank', 'tab2')"
driver.switch_to.window "tab2"
driver.get "http://stackoverflow.com/"

Related

How to handle download preferences and plug in preferences in watir? I want to know the list of options available for them to use

I have read that I can use the pattern below for passing options to Chrome driver in Watir,
browser = Watir::Browser.new :chrome, opts
I am using the following code at the moment:
Options = Selenium::WebDriver::Remote::Capabilities.new
driver = Selenium::WebDriver.for :chrome options
browser = Watir::Browser.new(driver)
Please let me know what pattern I should use to pass options to Chrome driver. What are the different options available for plugins and download preferences?

How do I resolve a readtimeout when it is the happy path?

Without a modal or alert popup. Pressing a button takes me to a page in such a way that selenium can no longer see the controls.
I have tired do reacquire the page without success.
visit([purchase page])
login
sleep(10)
find(:xpath, './/div[#class="tab-pane active"]/div/div/button[#class="btn btn-primary"]').click
Net::ReadTimeout (Net::ReadTimeout)
What I am fairly certain of is that is not not finding my element because I didn't generate a
ElementNotFound
My question is, what are the tools I want to try to get selenium to see the page I wish to manipulate?
One way I would debug this is by putting save_and_open_page before and after xpath. My guess is that it can't find the xpath to click on and its timing out.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
save_and_open_page
find(:xpath, './/div[#class="blah active"]/div/div/button[#class="btn btn-primary"]').click
save_and_open_page
If this is not the case you can try and set the timeout to 90 seconds to see if it helps.
# https://github.com/teamcapybara/capybara/issues/1305
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 90 # instead of the default 60
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile, http_client: client)
end
Based off your Firefox error
visit https://ftp.mozilla.org/pub/utilities/profilemanager/1.0/.
download profilemanager.mac.dmg
open up profile manager
select default or create a new profile manager
click on start firefox
Error should be fixed now

Watir with Tor browser

I've been playing with Watir and Tor browser and I can't get them to work.
The correct browser opens, however, I'm unable to open a website.
My code:
Selenium::WebDriver::Firefox.path = '\path\Tor Browser\Start Tor Browser.exe'
driver = Selenium::WebDriver.for :firefox
browser = Watir::Browser.new :firefox, :driver => driver
This results in a message box popping up, which says:
Your Firefox profile cannot be loaded. It may be missing or inaccessible.
The correct browser also opens up, however, the website doesn't.
I've also tried:
Selenium::WebDriver::Firefox.path = '\path\Tor Browser\Browser\firefox.exe'
driver = Selenium::WebDriver.for :firefox
browser = Watir::Browser.new :firefox, :driver => driver
With this, the message box doesn't appear, but still, the website is not loaded.
I'm not sure, if the problem is in my code or in the browser.
Thank you very much for any help!
Start Tor then run this, assuming your socks port is 9050
require 'watir-webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.proxy.socks'] = "127.0.0.1"
profile['network.proxy.socks_port'] = 9050
profile['network.proxy.type'] = 1
$browser = Watir::Browser.new :firefox, :profile => profile
$browser.goto "whatsmyip.org"
Firefox and Tor are not the same browser. Watir uses webdriver API to control the browser. You can't use Firefox webdriver API to control another browser.
In your second example, browser don't open the site, but you don't tell him to. You should add :
browser.goto('http://my.example.com')
And don't forget to put a browser.quit at the end of your code to close the browser.

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

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.

Resources