How to click a button element in watir? - html

Can I write watir code to click this button element?
The onclick event has to be triggered.
<button onclick="wizardAction('start', 'Starte Upgrade')">
<table align="center" cellspacing="0">
<tbody>
<tr>
<td><img src="/static/images/stock/gtk-ok.png" align="left" height="16" width="16"></td>
<td nowrap="nowrap">Starte Upgrade</td>
</tr>
</tbody>
</table>
</button>
Edit:
I tried it this way:
ie.button(:onclick, /start/).click
But it seems onclick is not supported:
> ruby upgrade-module.rb
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:143:in `match?':
onclick is an unknown way of finding an <INPUT> element ((?-mix:start)) (Watir:
:Exception::MissingWayOfFindingObjectException)
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:126
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:124
:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:124
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:122
:in `catch'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:122
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:119
:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:119
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/container.rb:8
04:in `locate_input_element'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/input_elements
.rb:5:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:53:
in `assert_exists'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:288
:in `enabled?'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:60:
in `assert_enabled'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:233
:in `click!'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:219
:in `click'
from upgrade-module.rb:19

One of these could work (not tested):
browser.image(:src => /gtk-ok/).click
browser.cell(:text => "Starte Upgrade").click
browser.button(:index => 1).click # assumes that it is the first button on the page
browser.button(:xpath => "//button[#onclick='wizardAction(\'start\', \'Starte Upgrade\')']").click
More information:
http://wiki.openqa.org/display/WTR/Buttons
http://wiki.openqa.org/display/WTR/XPath
http://wiki.openqa.org/display/WTR/Tutorial

I've run into problems like this. If the onclick attribute is not supported, you can do something like this.
b.
buttons.
to_a.
select{|but| but.attribute_value(:onclick).match(/start/) }.
first.
click
This is just something I'm typing out from memory, so it might need some tweaking. But it allows you to match things that Watir doesn't necessarily support.
I think there's also some way to do XPath queries with Watir, you might want to look into that as well

Related

Double popover window bug

When ever I click the button that enables the popover window, it works as it should, but when I click another button to show the popover menu for a different account, the first menu does not dissapear, and Im not sure what I need to add to my code to make that happen.
<td ng-class="bsPolicy.adjustBodyCell(grouplvl, '5', 'attach')" class="popover-row"
ng-if="dirvm.tabs[0].isActive">
<button class="vismaicon vismaicon-attach"
uib-popover-template="bsPolicy.dirvmConstant.AppendicesOfGroupPopoverTemplate"
popover-enable="false"
popover-trigger="outsideClick" ng-click="dirvm.setCollapsedSubAppendices($event)" popover-placement="auto bottom"
ng-if="grouplvl.accountRows.length > 0" ng-class="grouplvl.subAppendices.length > 0 ? '': 'disabled'"></button>
Here is how it currently looks like
The popover-trigger attribute value needs to be wrapped in single quotes (as of AngularUI-Bootstrap version 2.0.0).
<td ng-class="bsPolicy.adjustBodyCell(grouplvl, '5', 'attach')" class="popover-row" ng-if="dirvm.tabs[0].isActive">
<button class="vismaicon vismaicon-attach"
uib-popover-template="bsPolicy.dirvmConstant.AppendicesOfGroupPopoverTemplate"
popover-enable="false"
popover-trigger="'outsideClick'" ng-click="dirvm.setCollapsedSubAppendices($event)" popover-placement="auto bottom"
ng-if="grouplvl.accountRows.length > 0"
ng-class="grouplvl.subAppendices.length > 0 ? '': 'disabled'"></button>
Link to docs: https://github.com/angular-ui/bootstrap/tree/master/src/popover

Click the second button of an img in VBA

I'm trying to click an imagebutton on IE by using getAttribute("src"), but there are to identical buttons with the same src-code, so when I use this code, it clicks the first and I want to click the second. Can somebody help me?
I'm thinking of creating a loop to search through and choose the second one, but I have no idea how to write the code! Can somebody help me out?
This is the code I use to click the first button:
Set allInputs = IE.Document.getElementsByTagName("img")
For Each Element In allInputs
If Element.getAttribute("src") = "/forms/Content/Images/0006.gif" Then
Element.Click
Exit For
End If
Next Element
The HTML code for the buttons are:
<tr class="jqgfirstrow" role="row"
<tr tabindex="-1" class="ui-widget-content jqgrow ui-row-ltr" id="1" role="row"
< img src="/forms/Content/Images/0006.gif"
<tr tabindex="-1" class="ui-widget-content jqgrow ui-row-ltr" id="2" role="row
< img src="/forms/Content/Images/0006.gif"
Thanks for all of your help!
I did it with code here:
Set allInputs = IE.Document.getElementsByTagName("img")
allInputs(2).click

ClojureScript Dropdown Menu

I have a html page that contains a navigation bar at the top of the screen. In the navigation bar, I have a search box and what I want it to do is, you type in this box, hit enter and the results are displayed as a dropdown menu
<li><input type="text" id="search-bar" placeholder="Search"></li>
This is the html search input box. I have given it an id search-bar to eventually create the dropdown menu in ClojureScript
(when-let [section (. js/document (getElementById "search-bar"))]
(r/render-component [search-bar-component] section))
Currently I have a search-form that looks like the following
(defn search-form
[]
[:div
[:p "What are you searching for? "
[:input
{:type :text
:name :search
:on-change #(do
(swap! fields assoc :search (-> % .-target .-value))
(search-index (:search #fields)))
:value (:search #fields)}]]
[:p (create-links #search-results)]])
(defn- search-component
[]
[search-form])
This is my search-component.
What I want to happen is when you type in the input box on the navbar (say "hello", it calls search-index from the search-form with the parameter being the value you type in ("hello") and then returns the results as a dropdown menu below.
search-form works right now as a form on a html page, where you input some text into a form and then the results are displayed below. I want to change it to be on the navbar instead of as a separate page, where the input form is on the navbar and the results are displayed below
How would I have to change my search-form in order to do this?
I think I can do something along the lines of this
(defn search-bar-form
[]
[:div
[:input
{:type :text
:name :search
:on-change #(do
(swap! fields assoc :search (-> % .-target .-value))
(search-index (:search #fields)))
:value (:search #fields)}]
[:p (create-links #search-results)]])
(defn- search-bar-component
[]
[search-form])
Any help would be much appreciated.
re-com provides a typeahead component. It looks like you're using reagent, so you could just use that, otherwise you can use it as inspiration.

Watir and Cucumber. link not clickable

I am new to Watir and Cucumber, and I am trying to run an automation to create Live IDs. Below is the HTML for the link that I want to click, the "New" text is what it is showing on webpage. It would lead me to the form to add new contact to my Live account.
<ul class="c_cc" role="presentation" styple="overflow:visible;">
<li class="c_sm c_mcp" id = "new">
<a id href="#" class="c_nobdr t_prs">
<span class="is_c" dir="ltr" style="padding-right: 5px;">
<img class="is_img" src="https://p.pfx.ms/is/invis.gif" onload="this.onload=null;$Do.when('$IS.Init',0,this);" style="width:26px;height:26px;background-position:-1px -1px;background-image:url('https://p.pfx.ms/h/command4.png');" alt="New contact" title />
</span>
"New"
</a>
<span class="c_ms"></span>
</li>
</ul>
the watir code I wrote to click the "New" is below:
#browser.div(:id, "c_header").div(:id, "c_cb0").ul(:class, "c_cc").span(:text, "is_c").when_present.click
I get this error:
Watir::Wait::TimeoutError: timed out after 30 seconds, waiting for {:id=>"is_c", :tag_name=>"span"} to become present
Then I tried below code:
#browser.div(:id, "c_header").div(:id, "c_cb0").ul(:class, "c_cc").span(:text, "New").when_present.click
but this code does not really clicking on the "New" link, so the next form won't show up, and the rest of the code cannot run. Does anyone know any solution to this problem?
I found out a new window popped up, so it could not find the element in the old window. Thanks guys for helping.
In the first watir code snippet, a :text locator is being used for the .span method instead of a :class locator. For example:
browser.ul(:class, "c_cc").span(:text, "is_c").exists? #=> false
browser.ul(:class, "c_cc").span(:class, "is_c").exists? #=> true
In the second watir code snippet, a :text locator with a value of "New" is being used for the .span method. In this case, a .link method should be used. Additionally, the string includes double-quotes, so the double-quotes must be escaped if enclosed in another set of double-quotes (or enclose in single-quotes). For example:
browser.ul(:class, "c_cc").span(:text, "New").exists? #=> false
browser.ul(:class, "c_cc").span(:text, "\"New\"").exists? #=> false
browser.ul(:class, "c_cc").link(:text, "\"New\"").exists? #=> true
browser.ul(:class, "c_cc").link(:text, '"New"').exists? #=> true
So, one of the following examples should work:
browser.link(:text, "\"New\"").when_present.click
browser.link(:text, '"New"').when_present.click
browser.link(:class, "c_nobdr t_prs").when_present.click
Wow. This is old and I have encountered the same behavior. First, this is in a Cucumber step definition. The following works perfectly in open Ruby code. It simply refuses to function as a step.
It finds the link in the table and clicks it. Click doesn't function.
link = browser.table(class: 'alert-table').tbody.rows[1].cells.last.link(text: 'View')
# <Watir::Anchor: located: false; {:class=>"alert-table", :tag_name=>"table"} --> {:tag_name=>"tbody"} --> {:index=>1} --> {:index=>-1} --> {:text=>"View", :tag_name=>"a"}>
link.click
Ruby 2.4
watir (6.16.5)
regexp_parser (~> 1.2)
selenium-webdriver (~> 3.6)

File Upload using Selenium Webdriver and ruby with < button type="button">

I am using Selenium WebDriver and Ruby and I need to automate the file upload feature.
I tried with
WAIT.until { driver.find_element(:css,'div.ui-dialog-buttonset').displayed? }
by using this it gives time out error
elem = driver.find_element(:css,'div.ui-dialog-buttonset')
puts "Hi"
js = <<-end
var buttons = document.querySelectorAll('button');
buttons[0].setAttribute('aria-disabled', true);
end
elem = driver.execute_script(js, elem)
puts elem
elem.send_keys("C:\\Users\\seema\\Desktop\\1413.jpg")
Above code is not working and script fails by following error
here is HTML code
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<div class="ui-dialog-buttonset">
<button class="otherButtonClass ui-state-hover ui-state-focus" type="button" role="button" aria-disabled="false">
<button class="otherButtonClass" type="button" role="button" aria-disabled="false" style="display: none;">
<button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
</div>
</div>
here is error stack
,"message":"error - undefined me
thod `send_keys' for nil:NilClass \n [\"tests/upload_branding.rb:38:in `create_publish_pla
yer'\", \"tests/upload_branding.rb:80:in `PublishPlayerTest'\", \"org/jruby/RubyBasicObjec
t.java:1703:in `__send__'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib
/minitest/unit.rb:1258:in `run'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-rep
orters-0.14.20/lib/minitest/reporters.rb:62:in `run_with_hooks'\", \"c:/jruby-1.7.4/lib/ru
by/gems/shared/gems/scope-0.2.4/lib/scope.rb:105:in `run'\", \"org/jruby/RubyProc.java:255
:in `call'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/scope-0.2.4/lib/scope.rb:153:in `
recursively_run_setup_and_teardown'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/scope-0.
2.4/lib/scope.rb:155:in `recursively_run_setup_and_teardown'\", \"c:/jruby-1.7.4/lib/ruby/
gems/shared/gems/scope-0.2.4/lib/scope.rb:139:in `run_setup_and_teardown'\", \"c:/jruby-1.
7.4/lib/ruby/gems/shared/gems/scope-0.2.4/lib/scope.rb:105:in `run'\", \"c:/jruby-1.7.4/li
b/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:933:in `_run_suite'\", \"org/j
ruby/RubyArray.java:2417:in `map'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4
.7.4/lib/minitest/unit.rb:926:in `_run_suite'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gem
s/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb:45:in `_run_suite'\", \"c:/jr
uby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/parallel_each.rb:71:in `_r
un_suites'\", \"org/jruby/RubyArray.java:2417:in `map'\", \"c:/jruby-1.7.4/lib/ruby/gems/s
hared/gems/minitest-4.7.4/lib/minitest/parallel_each.rb:70:in `_run_suites'\", \"c:/jruby-
1.7.4/lib/ruby/gems/shared/gems/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb
:37:in `_run_suites'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/mini
test/unit.rb:877:in `_run_anything'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest
-4.7.4/lib/minitest/unit.rb:1085:in `run_tests'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/g
ems/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb:30:in `run_tests'\", \"org/
jruby/RubyBasicObject.java:1703:in `__send__'\", \"org/jruby/RubyKernel.java:2209:in `send
'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:1072:i
n `_run'\", \"org/jruby/RubyArray.java:1617:in `each'\", \"c:/jruby-1.7.4/lib/ruby/gems/sh
ared/gems/minitest-4.7.4/lib/minitest/unit.rb:1071:in `_run'\", \"c:/jruby-1.7.4/lib/ruby/
gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:1059:in `run'\", \"tests/upload_brand
ing.rb:87:in `(root)'\"]","result":false}
In IRB i found following errors
irb(main):038:0> Wait.until { $driver.find_element(:css,'div.ui-dialog-buttonse>button')[1].displayed? }
Selenium::WebDriver::Error::TimeOutError: timed out after 80 seconds (Unable to locate element: {"method":"css selector","selector":"div.ui-dialog-buttonse>button"})}
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/wait.rb:57:in `until'
from (irb):38:in `evaluate'
from org/jruby/RubyKernel.java:1093:in `eval'
from org/jruby/RubyKernel.java:1489:in `loop'
from org/jruby/RubyKernel.java:1254:in `catch'
from org/jruby/RubyKernel.java:1254:in `catch'
from C:\jruby-1.7.4\/bin/jirb_swing:53:in `(root)'
also for second command
irb(main):043:0> elem = $driver.find_element(:css,'div.ui-dialog-buttonse>button')[1]
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"css selector","selector":"div.ui-dialog-buttonse>button"}
from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/fxdriver#googlecode.com/components/driver_component.js:8444:in `FirefoxDriver.prototype.findElementInternal_'
from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/fxdriver#googlecode.com/components/driver_component.js:8453:in `FirefoxDriver.prototype.findElement'
from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/fxdriver#googlecode.com/components/command_processor.js:10456:in `DelayedCommand.prototype.executeInternal_/h'
from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/fxdriver#googlecode.com/components/command_processor.js:10461:in `DelayedCommand.prototype.executeInternal_'
from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/fxdriver#googlecode.com/components/command_processor.js:10401:in `DelayedCommand.prototype.execute/<'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in `raw_execute'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in `execute'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:575:in `find_element_by'
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/search_context.rb:42:in `find_element'
from (irb):43:in `evaluate'
from C:\jruby-1.7.4\/bin/jirb_swing:53:in `(root)'
Kindly help
First check out the link Selenium::WebDriver::Wait to avoid such time out error.
You can try the below code :
# implicit time out declarations
wait = Selenium::WebDriver::Wait.new(:timeout => 60)
# wait till the required button element being displayed
# Use the link : http://www.w3schools.com/cssref/css_selectors.asp
# for css selectors
wait.until { driver.find_element(:css,'div.ui-dialog-buttonse>button')[1].displayed? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse>button')[1]
elem.sendKeys("C://path/To/File.jpg")
Update
Looking at the link aria-disabled attribute and your part html below, I am seeing aria-disabled is set as false :
<button class="otherButtonClass ui-state-hover ui-state-focus" type="button" role="button" aria-disabled="false">
That desired button element is disabled.
Now see the question : Does WebDriver support file uploads?
You can't interact with the native OS file browser dialog directly, but we do some magic so that if you call WebElement#sendKeys("/path/to/file") on a file upload element, it does the right thing. Make sure you don't WebElement#click() the file upload element, or the browser will probably hang.
Update
#implicit time out declarations
wait = Selenium::WebDriver::Wait.new(:timeout => 60)
#wait till the div element is being displayed
wait.until { driver.find_element(:css,'div.ui-dialog-buttonse').displayed? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse')
js = <<-end
var buttons = document.querySelectorAll('button');
buttons[0].setAttribute('aria-disabled', true);
end
elem = driver.execute_script(js, elem)
elem.sendKeys("C://path//To//File.jpg")