selenium RC ruby, wait for hidden element to appear? - ruby

How can I wait for a hidden element to appear on the page?
I tried seleum.is_element_present but it dosent seem to be working.

What you want is the is_visible method. Below will wait for 30 seconds to become visible
!30.times{ break if (#selenium.is_visible("locator") rescue false); sleep 1 }

wait_for_element(locator, options={})
Wait for an element to be present (the wait in happenning browser side).

Came across the same problem using Watir, present? only works if element is visible. To wait for an invisible element to appear you can do this in Watir
Watir::Wait.until { browser.div(:class => "loaded").exists? }
exists? returns true if element is in the DOM, doesn't care about visibility

Related

Capybara wait until button is enabled?

Surprised I actually haven't come across this, but I have a simple button that is disabled until a dropdown is selected. Sometimes the page isn't fast enough to "enable" the button to be clicked on after the previous dropdown is selected causing it to fail.
I could throw in a "sleep" of a second or two and fix this, but that seems like a lazy/poor way to do this.
Is there a way in capybara (or purely selenium) that I can make it wait until the button is actually enabled? I'd like to throw this is the page model method for this button (as im trying to avoid API specific methods/selenium/etc... in the actual test specs (Although I can if I need to).
FWIW this is specifically for Ruby's capybara framework but pure selenium is fine as well.
Assuming the button you're referring to is actually a button (<button> element, or <input> element with type submit, reset, image, or button) then Capybaras :button selector will (by default) wait for it to be non-disabled.
click_button('Something')
or
find_button('button_id').click
or
find(:button, 'button_value').click
If any of the finder or action methods aren't waiting long enough for a specific element you can always increase the maximum wait time for a specific finder/action by passing a :wait option
find(:button, 'Something', wait: 10).click
If you're not using selector types (if not, why not) and instead are just using raw CSS to locate the element then you can use the :enabled pseudo class along with your existing CSS and something like
find('#my_button:enabled', wait: 10).click
If the element you're calling a button isn't actually a button but some other type of element (<a> etc) styled to look like a button, then you'll need to explain exactly how you're disabling the "button".
In Python you can do something like this:
def wait_until_clickable(driver, xpath, timeout = 1):
while timeout > 0:
try:
element = driver.find_element_by_xpath(xpath)
element.click()
return element
except:
time.sleep(0.1)
timeout = timeout - 0.1
return False

How to iterate with more than one element on the page

I have several buttons to click on the same page. How do I iterate and click on each of them?
def btnConectar()
elements = all("button[data-control-name='srp_profile_actions']").count
puts elements
first("button[data-control-name='srp_profile_actions']").click
find("section[class=modal]")
find("button[class='button-primary-large ml1']").click
end
all returns an Array like Capybara::Result object. You can iterate through that using the standard ruby enumerable methods.
all("button[data-control-name='srp_profile_actions']").each do |el|
el.click
find("section[class=modal]") # Not sure what this is for - if it's an expectation/assertion it should be written as such
click_button(class: %w(button-primary-large ml1)
end
That will work as long as clicking on the button doesn't cause the browser to move to another page.
If clicking does cause the browser to move to another page then all the rest of the elements in the Capybara::Result object will become stale (resulting in a stale element reference error on the next iteration) and you won't be able to iterate any more. If that is your case then details on what exactly you're doing will be necessary. Questions like does the original button still exist on the page after clicking the button-primary-large button, or can you iterate by just clicking the first matching button over and over? If it does still exist is it changed in any way to indicate it's already been clicked, or is the number/order of buttons on the page guaranteed to be stable? It would probably help to understand if you posted a fragment of the HTML for the first and second iteration.
def btnConectar()
page.all("button[data-control-name='srp_profile_actions']").each do |el|
while page.has_css?("button[data-control-name='srp_profile_actions']")
el.click #Click the button
find("section[class=modal]") #Modal mapping
click_button(class: %w(button-primary-large ml1)) #Click the button
sleep 3
end
end
end

Watir 'Wait' doesn't wait

gem "watir", "6.9"
Trying to execute this code
2.times { next_button.when_present.click }
or like this
2.times { next_button.wait_until(&:present?).click }
And getting this error
Watir::Exception::UnknownObjectException:
unable to locate element: #<Watir::Button: located: false; {:class=>"course-player__content-next-btn", :tag_name=>"button"}>
But I'm telling Watir to wait for an element before clicking on that!
What's going on? I'm not a fan of sleeps and don't want to resort to using them!
Thank you!
P.S. next_button has been found and identified.
The code works with sleeps and doesn't wait for an element by default.
You can try for example -
if element is visible in gui and exist in html = continue to click
browser.element(class: button_ok).wait_until_present
browser.element(class: button_ok).click
or - if element is exist in html and it is visible in gui = continue to click
browser.element(class: button_ok).present?
browser.element(class: button_ok).click
or - if element is exist in html = continue to click
browser.element(class: button_ok).exists?
browser.element(class: button_ok).click
or - wait until element is not visible(disapear)
browser.element(class: button_ok).wait_while_present
browser.element(class: button_ok).click
Hope it will help you.
of course button_ok is variable
button_ok = "path"
As of 6.15 #wait_while_present and #wait_until_present are deprecated. For versions 6.2 through 6.14 these methods had subtly different behaviour or from .wait_until(&:present?) & .wait_while(&:present?). This is no longer the case and these methods should no longer be used.

how to reload page until a button appears using capybara and ruby

I want to click a button after an action and button does not appear until and unless I reload the page. And some times it takes some time to appear the button and I have to reload page for more than once. I don't want to put static delays. So is there a way to achieve following using capybara and ruby:
do
page.evaluate_script("window.location.reload()")
until a button appears
While Mesut's code should work fine, I would re-write it as:
Timeout.timeout(Capybara.default_max_wait_time) do
loop do
page.evaluate_script("window.location.reload()")
break if page.has_selector?(...)
end
end
This will make sure to fail if it will have to wait more than timeout defined in Capybara settings. It can be useful when for example specs are running on the CI server.
Be aware that it can still lead to unexpected behaviors in some drivers, because it can interrupt while some scripts are evaluating.
reoload until page.has_selector? returns true, check this:
while true
page.evaluate_script 'window.location.reload()'
if page.has_selector?("css_selector")
break
end
end

Selenium Webdriver - How do I skip the wait for page load after a click and continue

I have an rspec test using webdriver that clicks on a button... after clicking the button, the page never fully loads (which is expected and correct behaviour). After clicking the button, I want to wait 2 seconds and then navigate to a different URL... despite the fact that the page has not loaded. I don't want to throw an error because the page hasn't loaded, I want to just ignore that, and continue on as if everything is good. The page should never load, that is the expected and correct behaviour.
How can I avoid waiting until the timeout, and secondly, how can I have that not throw an error which casuses the test to fail.
Thank you!
WebDriver has a blocking API and it will always wait for page to be loaded. What you can do instead, is to press the button via JavaScript, i.e. trigger its onclick event. I am not familiar with Ruby, but in Java it would be:
WebDriver driver = ....; // Init WebDriver
WebElement button = ....; // Find your element for clicking
String script = "if (document.createEventObject){"+
"return arguments[0].fireEvent('onclick');"+
"}else{"+
"var evt = arguments[0].ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
((JavascriptExecutor)driver).executeScript(script, button);
After this you can wait for 2 seconds and continue
I had this similar problem, I tried this solution which #Sergii mentioned but was getting below error:
org.openqa.selenium.JavascriptException: javascript error: element is not defined (Session info: chrome=84.0.4147.89)
As a workaround had to initialize element within java script code.
String script = "var element = document.querySelector('button[type=submit]');" +
"if (document.createEventObject){"+
"return element.fireEvent('onclick');"+
"}else{"+
"var evt = element.ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
Thank you #Sergii for your answer, I was not able to add this as a comment due to limited characters, so added it as a separate answer.
why don't you try a simple trick of using "waiting()" after waitForPageToLoad() which makes it to neglect the previous command in Selenium and never fails that step
What if you used RSpec's expect method:
expect {
Thread.new() {
sleep(2)
raise RuntimeError
}
theButton.click()
}.to raise_error
Send the 'Enter' key to the link or button in question instead of a click, webdriver won't wait for the page to load and will return instantly (this is in C#, just translate to Ruby):
element.SendKeys(Key,Return);

Resources