Finding element and entering in Ruby Selenium - ruby

I'm new to the Selenium and Ruby and Cucumber/Gherkins world and am trying a simple script to navigate to the Google page, find the search bar and enter a word and press enter or find the "Google Search" element and click.
This is in a Ruby file but formatted in Gherkins as I'm working with it.
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
Given(/^I am on the Google website$/) do
driver.navigate.to "http://www.google.com"
end
When(/^search is entered$/) do
search = driver.find_element(xpath: "//div[#class = 'jhp big']//input[#class = 'gLFyf gsfi']")
search.send_keys "this"
end
Then(/^confirm$/) do
puts "Confirmed"
driver.close
end
So here I'm navigating to the google website using a Selenium WebDriver initialized as driver. Then finding the element using the xpath and sending in the word 'this'.
When I run this, I get this error:
Selenium::WebDriver::Error::NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class = 'jhp big']//input[#class = 'gLFyf gsfi']"}
Also, to click 'enter' I can either do send_keys :enter or find the Search button and use 'click' correct?
Thank you in advance

you have to use implicit wait for driver to wait until element found
Write the following code and then fit into your cucumber model
require 'selenium-webdriver'
driver=Selenium::WebDriver.for :chrome
driver.navigate.to("https://www.google.com/")
driver.manage.timeouts.implicit_wait=10
driver.find_element(name: 'q').send_keys 'raja'
driver.find_element(name: 'btnK').click
But remember, If you WATIR which is a wrapper around selenium binding these kind of waiting is automatic.

Related

Unable to click on link using Link text/Partial Link text using ruby

I am new here and even to Ruby and Selenium. I am trying to click a link on web page which has following code:
<a> target="mainFrame" href="dynamic_Utility_Index.htm">Dynamic Utilities</a>
So basically I want to click on this dynamic utilities. The script which I have written so far is:
require 'selenium-webdriver'
require 'win32ole'
driver = Selenium::WebDriver.for:firefox
driver.manage().window().maximize();
driver.navigate.to 'xyz.com'
wait = Selenium::WebDriver::Wait.new(:timeout =>10) # seconds
#Click on Dynamic Utilities
wait.until{driver.find_element(:link_text,'dynamic_Utility_Index.htm').click}
puts "Clicked"
I have even used link, partial_link_text in place of link_text but keep getting the following error
(Unable to locate element: {"method":"link
text","selector":"dynamic"})
(Selenium::WebDriver::Error::TimeOutError)
I am using Ruby and Selenium Web driver.
Did you try with:
wait.until{driver.find_element(:link_text,'DYNAMIC UTLITIES').click}
?? Sometimes I had to use the text link by capitalized.
Another option could be:
driver.find_element(:xpath, "//a[contains(#target, 'mainFrame')]").click
Hope works for you :D
The item you consider to be a link text (dynamic_Utility_Index.htm) actually just a value of href attribute. Actual link text is "Dynamic Utilities". Also you can use xpath locator //a[#href="dynamic_Utility_Index.htm"] instead of search by link text:
wait.until{driver.find_element(:xpath,'//a[#href="dynamic_Utility_Index.htm"]').click}

Selenium switch to iframe not working in ruby

I'm unable to find an element after switching to an iframe. I believe driver.switch_to.frame("homeFrame") is failing. I can locate other elements on the page that are not in the iframe, but I can't get anything inside the iframe.
Here is my mock ruby:
require 'selenium-webdriver'
begin
driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10
driver.navigate.to "file:///Users/$USER_NAME/Desktop/iframe/index.html"
driver.switch_to.frame("homeFrame")
link = driver.find_elements(:xpath, "/html/body/div[2]/div[3]/div/div/ul/li[5]/a")
link.click
end
I created a gist with all the example code and the HTML I am working with.
a-main.rb - mock example ruby script
b-index.html - HTML that contains the iframe element
c-homeFrame-iframe.html - HTML inside the iframe
Here is the link to the gist https://gist.github.com/spencerdcarlson/f29b9833608f6f2f2bbf

how do I loop a click action(refresh) until a specific string is found?

I am writing a watir script where I need to loop a click action untill a text is found. Please let me know how to do this.
thanks
You could do something like this:
require 'watir'
b = Watir::Browser.new
b.goto('http://www.example.com')
until b.text.include? "example"
b.refresh
end
But watir and watir-webdriver have methods to handle dynamic page elements. This link has examples on waiting with watir-webdriver.

Clicking group of links in watir

I am new to ruby, and I am trying to work with watir. I think I got the basics, but I am having trouble clicking all links whose id matches a regex. I tried this;
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "http://mysite.com"
browser.links(:id, /asd[0-7]/).each do |adv|
adv.click
sleep 1
end
But it doesn't seem to be clicking the links. I am doing something wrong here? Links are opening in new windows, so looping through them is no problem. But I couldn't make the loop work.
This kind of investigation is better in IRB. Anyway, you should validate that you have links to click.
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "https://rvm.io/"
links = browser.links(:href => /gemsets/)
links.count
I changed mine up to use a site I can access and has links.

How do you get window titles, ids, and names in selenium-webdriver?

Im trying to implement the following methods from selenium-webdriver (ruby)
get_all_window_ids
get_all_window_titles
get_all_window_names
I ran Selenium IDE and exported my script to Ruby Test::Unit. Saved it as .rb
Opened my script for editing using Aptana Studio 3
Initial code snippet as follows:
require "rubygems"
require "selenium-webdriver"
require "test/unit"
class SwitchToPopup3 < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = (URL of my test website)
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_switch_to_popup3
.
.
puts #driver.get_all_window_ids()
puts #driver.get_all_window_titles()
puts #driver.get_all_window_names()
.
.
end
The error I keep getting is
NoMethodError: undefined method `get_all_window_ids' for # <Selenium::WebDriver::Driver:0x101e4b040 browser=:chrome>
/Users/rsucgang/Documents/Aptana Studio 3 Workspace/Testing/SwitchToPopup2.rb:37:in `test_switch_to_popup3'
I've studied the documentation for the ruby bindings for selenium-webdriver
http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/Client/GeneratedDriver.html#get_all_window_titles-instance_method
Ultimately my goal here is to run my automation script:
Click on a link that opens a new window with target=_blank and no windowID available (does not implement JS)
Identify the names of all opened windows in the browser
switch to a new pop-up window using the switchToWindow(name) method
continue running my script on that pop-up window
I've googled and researched this on the internet and I have not gotten any much information.
Thanks and please let me know if you need more information.
OSL Mac OSX 10.7.3
Ruby: ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
Browser: Firefox 9.0.1 (Mac)
Chrome: Chrome 17.0.963.79 (Mac)
Selenium-Server: Ruby gem 2.20.0
Justin, you have a good approach. But there is a gotcha in assuming that the window handles will return in the correct order. This isn't always the case across all browsers. I outline a slightly different approach in my free weekly Selenium tip newsletter (elemental-selenium.com).
It goes like this:
#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/
You can see the full tip here.
The problem is that the get_all_window_ids is for Selenium::Client rather than Selenium::Webdriver. I believe that Selenium::Client is the old version Selenium and the API is not the same as Selenium::Webdriver (see here). Since you are using Selenium::Webdriver, this explains why you get an 'undefined method' error.
For Selenium::Webdriver, the only way I know how to switch between windows is using:
#driver.switch_to.window("<window_handle>")
You can get all the known window_handles by:
#driver.window_handles
#=> Returns all window handles as an array of strings
If you want to switch to the popup you just opened you can do the following. Note that this assumes .window_handles are in the order that the windows were opened, which I believe is true:
#driver.switch_to.window #driver.window_handles.last
To summarize, assuming you only care about accessing the popup (and not about accessing by name) you can do:
#Click control that opens popup
#driver.find_element(:id, 'button that opens popup').click
#Switch to popup
#driver.switch_to.window #driver.window_handles.last
#Do actions in new popup
#driver.find_element(:id, 'id of element in popup').click
Note that if after working with the popup, you will want to return to the original window, then I suggest you do the following. By passing a block to the switch_to.window, the block will be executed in the popup and when the block ends #driver will automatically point back to the original window.
#Click control that opens popup
#driver.find_element(:id, 'button that opens popup').click
#Switch to popup
#driver.switch_to.window( #driver.window_handles.last ){
#Do actions in new popup
#driver.find_element(:id, 'id of element in popup').click
}
#Continue with original window
#driver.find_element(:id, 'button in original window').click

Resources