No matter what I try to do, the browser tries to run the test too fast before it has a chance to find the element that I am looking for. If I put in a simple "sleep 2", it has a chance for the drop down menu to drop and load and successfully find the element. But I am wanting to learn to use the Selenium Wait command. I have tried numerous combinations of the below and looked all over the web for documentation or perhaps examples. I have found plenty of firefox and people say that some of the things below worked perfectly in firefox, but for me and my project team mates, we can not get any of the waits, implicit or explicit, to pause long enough for it to detect the element. The element does not exist until the drop down menu is fully dropped, then it can detect it. It doesn't take 2 seconds but it seems that none of my wait commands will actually make it wait. Like I said, I have tried numerous different things and almost all of them are below. If anyone can help guide me, i would appreciate it. Here is some of the code I have tried:
def setup
#driver = Selenium::WebDriver.for :chrome
#driver.get "https://website.herokuapp.com/"
#wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
# #driver = Selenium::WebDriver.for:chrome
# #driver.manage.timeouts.implicit_wait = 30
# #wait = Selenium::WebDriver::Wait.new(:timeout => 15)
# #wait = Selenium::WebDriver::Wait.new(:timeout => 10)
# #driver.manage.timeouts.implicit_wait = 10
# #wait = Selenium::WebDriver::Wait.new(timeout: 10)
#driver.manage.window.maximize()
# #driver.navigate.to("https://website.herokuapp.com/")
end
def test_user_name_is_present
login()
#driver.find_element(:class, "navbar-toggle").click()
# user = #driver.find_element(:class, "dropdown-toggle").text
# #wait.until{#driver.find_element(:class, "dropdown-toggle")}
#driver.find_element(:class, "dropdown-toggle")
#wait.until { #driver.find_element(:class => "dropdown-toggle") }
user = #driver.find_element(:class, "dropdown-toggle").text
assert_equal(true, user.include?('HEATHER'), "no user")
end
I'm more familiar with JavaScript or Java bindings.
But what about:
def test_user_name_is_present
login()
#driver.find_element(:class, "navbar-toggle").click()
#wait.until { #driver.find_element(:class => "dropdown-toggle").displayed? }
user = #driver.find_element(:class, "dropdown-toggle").text
assert_equal(true, user.include?('HEATHER'), "no user")
end
Our team uses this, just add to your test_helper.rb if using rails
def wait_for_ajax(sleep_sec = 4)
assert page.has_no_content?(:css, 'body.loading')
sleep sleep_sec if sleep_sec
end
Related
Hi I got Net::ReadTimeout every time I use find_elements when there are no elements like this. A short example my Feature-file: (cucamber)
Feature: Retest Faild Testcases
Scenario: Simpel test find_elements
Given open website humblebundle.com
And search for civilization
my ruby step-file:
Given /^open website (.*)$/ do |url|
$driver.navigate.to "http://www."+url
$wait = Selenium::WebDriver::Wait.new(:timeout => 180)
$wait.until {
$driver.find_element(:css => "div[class='navbar-content']").displayed?
}
end
And /^search for (.*)$/ do |name|
element=$driver.find_element(:css => "input[placeholder='Search']")
element.send_keys name
sleep(1)
element.send_keys :enter
sleep(10)
#line199 items=$driver.find_elements(:id => "Games")
puts items.count
end
and the error I get:
Net::ReadTimeout: Net::ReadTimeout
./features/step_definitions/basic_steps.rb:199:in /^search for (.*)$/'
./features/a_test.feature:4:inAnd search for civilization'
I would be very thankful for any help with this.
By default, the timeout for a request is set to 60 seconds. If your implicit wait setting is set to greater than this value, you will get a Net::ReadTimeout if no elements are located. You must keep your implicit wait setting less than your request timeout.
Thanks, that was it.
I change the implicit wait 2 secends lesser than my ReadTimeout.
I Trying to open multiple instance of Firefox browser from my ruby code.
I use selenium 2.53.4 and firefox 47.0.2
The problem is after the thread has been created, the driver not initiate imidiately. it's took so long time until it's opened. And the second driver will opened after the first one is almost finished, this make multithread useless.
Here is my code
require "selenium-webdriver"
th = Array.new
i = 0
limit = 3
while i < 10
if(Thread.list.count <= 3)
th[i] = Thread.new(i){ |index|
start = Time.new
puts "#{index} - Start Initiate at #{start}"
driver = Selenium::WebDriver.for :firefox
finish = Time.new
puts "#{index} - Finish Initiate at #{finish}"
driver.get "http://google.com"
sleep(10)
driver.quit
puts "#{index} - Finished"
}
i = i + 1
puts "Thread - #{i} Created"
end # end if
end # end while
th.each{|t|
if(!t.nil?)
t.join
end
}
Did I code it properly? or it's firefox limitation? or selenium?
Note :
When I remove the thread it's take lesser time (about 6 s until navigate to intended URL)
it's work great using chrome driver
I was trying to automate rediff.com . I went from one page to other but when i came back i got staleException . I tried a lot but couldn't fix it.
I am attaching the code snippet too. Any help would be appreciated.
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
#driver.manage.window.maximize
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
element = wait.until { #driver.find_element(:xpath,".//*[#id='popular_cat']") }
ensure
box=#driver.find_element(:xpath,".//*[#id='popular_cat']")
end
links=box.find_elements(:tag_name,"a")
puts "Total links are:#{links.size}"
links.each do |i|
puts "--------------------"
puts "Value of all links is:#{i.text}"
i.click
puts "Title of page is :#{#driver.title}"
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
box=#driver.find_element(:xpath,".//*[#id='popular_cat']")
links=box.find_elements(:tag_name,"a")
end
Every time you reload the page (because you are going to other page and then going back or because you simple reloaded the page) your references to the links 'links=box.find_elements(:tag_name,"a")' are lost.
I would suggest a few changes in order to workaround this (might not be the best solution)
links = box.find_elements(:tag_name,"a").size
links_counter = 0
while links_counter < links
box = #driver.find_element(:xpath,".//*[#id='popular_cat']")
current_link = box.find_elements(:tag_name,"a")[links_counter]
links_counter += 1
puts "--------------------"
puts "Value of all links is:#{current_link.text}"
current_link.click
puts "Title of page is :#{#driver.title}"
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
end
I hope this helps you!
Best,
Fernando
I have done a lot of research before asking this questions but unable to find an answer.
I am trying to automate a website using Ruby Selenium + Webdriver. I am having an issue with the script hanging once new window opened. I am unable to focus on new window or do anything else.
Here is the code that opens up new window:
<a id="theField" class="RightTextHeading" onclick="javascript:openTheWindow('someInfo.aspx?type=','300','300');if(window.document.RetValue == '5'){window.parent.LoadinIframe('someStuff.aspx?info=N&Stuff=','Some Data here > Full View','false');}; return false;" href="../MoreStuff/#" style="text-decoration: none;">ClickMe <span class="context"> opens an overlay</span></a>
Here is my ruby code:
require 'rubygems'
require 'watir-webdriver'
require 'selenium-webdriver'
prefs = {
:download => {
:prompt_for_download => false,
:default_directory => '.'
}
}
client = Selenium::WebDriver::Remote::Http::Default.new
#client.timeout = 600 # seconds - default is 60
client.timeout = 30 # seconds - default is 60
d = Selenium::WebDriver.for :chrome, :prefs => prefs, :http_client => client
b = Watir::Browser.new d
saved_cookies = b.cookies.to_a
b.goto'https://somewebsite.com'
begin
b.iframe(:id => 'iframeBody').a(:id => "somelink").click
rescue
puts 'Rescue'
end
sleep 2
puts "before"
#if b.window(:title=>"The new window").exists?
#puts " Test passed. New window opened!"
#else
#puts " Test Failed! No window found"
#end
#b.window(:title => /The new window/).use do
#b.window.last.use do
# puts "here"
#end
#puts b.window(:title => "The new window").exists?
puts b.windows.size
puts "after"
#b.close
I have commented out different things I attempted to make this work.
It seems that Ruby just sits and waits after the new window is opened and I do not know how to get it out of the waiting state.
If I do not begin/rescue, the code stops execution until it times out.
When I am trying to rescue I can get control back but still unable to focus on the new window.
Any ideas are appreciated?
Please let me know if you have additional questions.
Thanks,
-Andrey
Im not entirely sure what your trying to do with the test, but the answer on how to do something with the new window is already in your code to some extent
#b.window.last.use do
should be
#b.windows.last.use
or
#browser.windows.last.use
Will have your test start using the new window once it appears.
Depending on what your trying to do, may change where in your code that line should go. But that will let you access and interact with the new window.
I'm pretty new to ruby and am trying to implement a Tk application that will display a window prompting for input at a certain interval. In between the interval I want the window to not display in any taskbars, etc. and so I've implemented the following code that seems to work perfectly the first time through, but after the window displays the second time and I enter text in the TkEntry and click the TkButton the window is dismissed and never returns. I've tried putting in some "puts" calls at key locations to see what is happening and it seems that it never even makes it past the call to "displayUi".
*EDIT:
I'm running ruby 1.9.3p385 (2013-02-06) [i386-mingw32] on a Windows 7 system (in case that makes any difference)
Any help (even if it's providing a different mechanism to accomplish the same goal) would be appreciated, but please keep in mind that I'm a ruby noobie. Thanks!
require "tk"
class Sample
attr_accessor :root, :active
#active = false
def initialize
# init
end
def entry (task)
# do some work here
#active = false
end
def displayUi ()
#active = true
if (#root.nil?)
#root = TkRoot.new { title "Sample App" }
else
# already running just restart
Tk.restart
end
TkLabel.new(#root) {
text 'Sample Text'
pack { padx 15; pady 15; side 'left' }
}
statusInput = TkEntry.new(#root) {
pack('side'=>'left', 'padx'=>10, 'pady'=>10)
}
statusInput.focus
response = TkVariable.new
statusInput.textvariable = response
TkButton.new(#root, :text => "Ok", :command => proc { entry(response.value); #root.destroy }) {
pack('side'=>'left', 'padx'=>10, 'pady'=>10)
}
Tk.mainloop
end
end
i=0
st = Sample.new
while (true)
if (!st.active)
st.displayUi()
end
sleep(1)
end