Watir: How I can click button in IE popup windows - ruby

I have a customized popup IE window where there are buttons included into iframe. I can't click on any button in it. I know how to work with iframes, but I can't switch to this window. Window has title and URL.
I wrote this method:
def confirm_ok
self.in_iframe(:id => 'frmMain') do |frame|
self.button_element(:id => 'btnOK', :frame => frame).click
end
end
But I get this error:
Watir::Exception::UnknownFrameException: unable to locate iframe using {:id=>"frmMain", :tag_name=>"iframe"}
I use Watir, PageObject. And run scenarios under IE.
Watir's method doesn't work:
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end
I get error
NoMethodError: undefined method `window' for #<PA_Main:0x33f6780>

As best as I can tell you have two separate issues.
Firstly, I have no idea how you set your browser variable to a page object instance. The Page Object Module definitely sets browser as a readable attribute.
So if the code is within a class that has import PageObject, you should be able to do browser.window(...) just fine.
If you are using the code outside of such a class, you need to make sure that you are in a scope that has access to the Watir::Browser instance. If you have a page object defined, you can use it like: my_page_object.browser.window(...)
Secondly - based on what you are describing, the iframe usage has to be combined with the window usage:
browser.window(title: "annoying popup") do
browser.iframe(id: 'frmMain').button(id: "close").click
end

Related

Click on buttons on the page that has got the same class using capybara with siteprism

There are 20 different buttons to expect and needs to be clicked through to expect and verify the urls inside the code. I have tried different ways to implement my tests but they are failing.
I'm trying something like:
page.all(:class => 'action red').each do |button|
c = button.find(:class => 'action view red')
c.click
page.driver.browser.switch_to.window(#new_window)
expect('some element on those 20 different browsers sessions before closing them')
page.driver.browser.close
end
end
I'm getting this error:
ArgumentError: invalid keys :class, should be one of :count, :minimum,
:maximum, :between, :text, :visible, :exact, :match, :wait
Any can help me in the code how to perform get the elements of all the 20 buttons, store them and click them to expect the url each of them before closing it
Your "buttons" aren't buttons - since they are <a> elements they are actually links, styled to look like buttons.
Assuming that clicking each of these links actually opens a new window (since you're attempting to switch to a new window) then the code would be something like
page.all(:link, class: ['action', 'red']).each do |link|
win = page.window_opened_by { link.click }
page.within_window(win) do
expect(page).to ... # whatever you need to expect
end
win.close()
end
Note this doesn't use any driver specific (.driver.browser...) methods - you should stay away from them whenever possible since they are generally a sign you're doing something wrong. Additionally, the :class option wasn't universally available on all of Capybaras builtin selector types until v2.10, so you will need to be using a newer version of Capybara for that.

Appium-Capybara-ruby running automation on Physical device and some of the capybara function not working

I have setup Appium - Capybara and was able to run automation suites on physical device, but facing issue with few methods
I was able to successfully run Capybara methods like
fill_in field, :with => text
click_button button_text
expect(page).to have_content(text)
But facing issues with below method ( it works on regular chrome on laptop but not on mobile )
page.first(:link, link_text).click
can you please help me to understand if appium capybara supports all the capybara methods or only few of them.
Below is the error message
undefined method `click' for nil:NilClass (NoMethodError)
In the above code, first the link with link_text is being searched on the page. If no link is found then nil is returned.
Thus, to make this code work, we need to wait for the link with the link text to appear on the page and then click it.
So you can use any one of the below mentioned code before clicking the fink
page.should have_content(link_text)
page.find_link(link_text)
If the above code doesn't works then you can also try increasing the default wait time as shown below:
Capybara.default_wait_time = 30
page.should have_content(link_text)
page.first(:link, link_text).click
Capybara.default_wait_time = DEFAULT_WAIT_TIME
DEFAULT_WAIT_TIME for Capybara tests is set in your environment file.
Hope this helps :)
undefined method 'click' for nil:NilClass (NoMethodError) is telling you that #first isn't returning an element (#first returns immediately with a matching element, or nil if there is none). Is there a reason you are using first rather than find?? If there is only one matching link on the page you should really prefer find over first (in this case find_link), or try to scope to a part of the page where the would only be one link, since it will throw an error explaining why it didn't find the link (rather than returning nil) and also has waiting behavior for elements to show up (first can have the same behavior but it doesn't by default)

instantiating a page object in Ruby with Selenium and Cucumber

I'm trying to automatize a browser to navigate a website and click on different pages. To do this I have two classes AllPages and SearchPage which inherits from AllPages.
The first thing I do is instantiate AllPages, go to the website and click a link which leads me to a Searchpage, at which point I try to instantiate SearchPage using these lines of code
AllPages:
def return_search_page browser
#browser = browser
end
Steps:
#newpage = AllPages.new #browser
#searchpage = #newpage.return_search_page #newpage
#searchpage.find_searchbox
the error i get trying to run this is:
"undefined method `find_searchbox' for #< AllPages:0x00000002632cf0> (NoMethodError)"
My method find_searchbox which is located in SearchPage is this
def find_searchbox
#browser.find_element(:class, "searchbox")
end
I'm guessing i'm freaking up the instantiation of the object #searchpage, since that's in a scenario that comes after opening the browser and navigating to the website.
Any help will be appreciated
def return_search_page browser
SearchPage.new browser
end
#newpage = AllPages.new #browser
#searchpage = #newpage.return_search_page #browser
#searchpage.find_searchbox
And if you are setting #browser in your initialize, you wouldn't need to pass it in as a parameter to #return_seearch_page

How to manage frame in ruby selenium webdriver

I am working with RUBY selenium webdriver (not java, not c#, etc). Please, the solutions must be in ruby. So, I have the frame with Id "_wicket_window_3". To locate this element, I apply:
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
My frama has the buttons "Adjuntar", "Exportar" and "Terminar".
Now, how can i manage the frame ? I want to click the button "Adjuntar","Terminar" or "Exportar".
If I try there:
frame = driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
frame.click_button("Adjuntar")
The response is: Undefined method.
Please, help me !!!
Regards, Agustín.
You can't manage the iframe you switched to as you planning.
When you do
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
Your context start to be inside this iframe, all operations you will now do on driver, will be inside this iframe.
So, after the switch do:
driver.click_button("Adjuntar")
It should click on the button.
I dont think click_button is the right command, that's why the error you get is undefined method
you have to switch to the frame then look for the element of that button as you did before to look for the frame, then click it
something like
driver.switch_to.frame driver.find_element(:id, "_wicket_window_3")
driver.find_element(:id, "id_of_the_button_you_want_to_click").click
Note used :id there but you can use whatever the locator you want, :css, :xpath, :class whatever

Duplicate Checkbox in Page-Object is not defined?

I am trying to click a checkbox that enables a purchase button to appear. When I try to use it, I get a "NoMethodError: undefined method 'eula' for Cart:0x101f54810" error. I think it may be because there are two identical checkboxes, but I am just not certain.
HTML:
<p id="eula-box" class="annoy cc"><input type="checkbox" name="terms_of_service" value="terms_of_service" tabindex=20 />I have read & agree to the End-User License Agreement.</p>
<p id="eula-box" class="annoy pp"><input type="checkbox" name="terms_of_service" value="terms_of_service" tabindex=20 />I have read & agree to the End-User License Agreement.</p>
My class:
require 'rubygems'
require 'page-object'
require 'page-object/page_factory'
require 'watir-webdriver'
CART_URL = 'http://www.anonymizer.com/cart/checkout.html?SKU=ANONUNV12'
class Cart
include PageObject
page_url CART_URL
checkbox(:eula, :class=>"annoy_cc")
button(:purchase, :value=>'purchase')
def complete_order(data = {})
self.eula.click
end
end
Udpated: I was changing the object type around trying to get it to work. Element was the last type I tried. I changed my example back to checkbox (my original attempt). Thanks for pointing that out.
When you call the class level checkbox method in page-object it generates five methods. The call:
checkbox(:summary, :id => 'valid_checkbox')
will generate:
check_summary # check the checkbox
uncheck_summary # uncheck the checkbox
summary_checked? # returns true if it is checked. otherwise false
summary_element # returns the Checkbox object
summary? # returns true if the element exists. otherwise false
These are the method to interact with when using the checkbox.
PageObject's checkbox generate the following method to check (i.e. click) it.
check_eula
See http://rubydoc.info/gems/page-object/0.6.3/PageObject/Accessors:checkbox
I'm not very familiar with page-objects, but is element a valid accessor? I'm looking at the documentation and don't see it. Perhaps it would be better to use the checkbox accessor?
As an aside, the easiest way to see if your problem is caused by having two similar checkboxes, would be to just remove one and see if the problem goes away!

Resources