I'm trying to perform automated testing using Watir gem. I need to test whether the checkbox works by setting and clearing it.
The HTML for the checkbox is:
<md-checkbox aria-label="Remove" aria-invalid="false" aria-checked="false" tabindex="0" class="ng-pristine ng-valid md-default-theme ng-touched" role="checkbox" ng-model="user.remove_photo" style="margin-top: 0px;">
<div class="md-container" md-ink-ripple="" md-ink-ripple-checkbox="">
<div class="md-icon"></div>
<div class="md-ripple-container"></div>
</div>
<div ng-transclude="" class="md-label">
<span class="ng-scope">Remove</span>
</div>
</md-checkbox>
My test script:
require 'watir'
require 'watir-webdriver/wait'
browser = Watir::Browser.new :firefox
browser.goto 'https://54.69.254.137/webui#/landing'
browser.driver.manage.window.maximize
browser.button(:class =>'sign-in md-button md-default-theme').click
browser.text_field(:id =>'input_001').set('abcadmin#example.com')
browser.text_field(:id =>'input_002').set('password')
browser.button(:class =>'md-primary md-raised md-button md-default-theme').click
browser.button(:class =>'md-icon-button md-primary main-menu-toggle md-button md-default-theme').when_present.click
browser.link(:text =>'Edit Profile').when_present.click
browser.checkbox(:class => 'ng-pristine ng-valid md-default-theme-ng-touched').set
browser.button(:class => 'md-primary md-raised md-button md-default-theme').click
Gives following error:
unable to locate element using checkbox(:class => 'ng-pristine ng-valid md-default-theme- ng-touched').
The problem with the checkbox is that it is not an HTML checkbox - ie <input type="checkbox">. Instead it is a md-checkbox, which I believe is from the Angular Material library. As a result, Watir's checkbox method will not see it.
You will need to treat the md-checkbox as a generic element and use the click method to set/clear it:
browser.element(class: 'ng-pristine ng-valid md-default-theme-ng-touched').click
The class attribute does not look unique. You might want to use one of the other attributes instead. For example:
browser.element(aria_label: 'Remove').click
Related
Does anybody have any insight as to how to trigger a selection in a pulldown using Watir? I can't seem to get this to find that first field. I've tried class (deprecated apparently), id, and name.
require 'webdrivers/chromedriver'
require 'open-uri'
require 'json'
Selenium::WebDriver::Chrome.path = '/Applications/RF_Utilities/Google Chrome.app/Contents/MacOS/Google Chrome'
url = 'https://www.finditatmpg.com/Default.aspx'
a = Watir::Browser.new :chrome, headless: false
a.goto url
# a.select(id: 'dpdCemetery_Input').select('Beechwood')
a.select(name: 'dpdCemetery').select('Beechwood') # doesn't work
puts "cemetery selected" # a marker
a.text_field(id: 'txtSurname').set 'Smith' # this works if set first
puts "last name selected" # a marker
a.text_field(id: 'txtFirstname').set 'John'
puts "first name selected" # a marker
a.button(id: 'btnSearch_input').click # submit
The error:
timed out after 5 seconds, waiting for false condition on #<Watir::Select: located: false; {:name=>"dpdCemetery", :tag_name=>"select"}> (Watir::Wait::TimeoutError)
Update: Here is the copied syntax of the form:
<div id="dpdCemetery" class="RadComboBox RadComboBox_Bootstrap" style="width:300px;white-space:normal;">
<!-- 2021.1.119.45 --><table summary="combobox" style="border-width:0;border-collapse:collapse;width:100%">
<tbody><tr>
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input name="dpdCemetery" type="text" class="rcbInput radPreventDecorate" id="dpdCemetery_Input" value="Select a Cemetery" autocomplete="off">
</td>
<td class="rcbArrowCell rcbArrowCellRight">
<a id="dpdCemetery_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a>
</td>
</tr>
</tbody></table><div class="rcbSlide" style="z-index:6000;display:none;"><div id="dpdCemetery_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Bootstrap "><div class="rcbScroll rcbWidth"></div></div></div><input id="dpdCemetery_ClientState" name="dpdCemetery_ClientState" type="hidden" autocomplete="off">
</div>
The problem is that these are not "real" HTML Select/Option element dropdowns, so you can't use the Select class. The tag name is a div not a select.
You'll need to use something like (update - correct locator is an id not a name):
a.div(id: 'dpdCemetary').click
a.li(text: /Beechwood/).click
Solved:
a.text_field(name: 'dpdCemetery').set('Beechwood')
Seems it behaves like a text field. JS provides the dropdown list. I was under the impression Watir could handle that sort of thing.
I'm trying to upload a picture with watir and phantomjs but it's failed.
Html code is:
<div class="qq-uploader">
<div class="qq-upload-drop-area" style="display: none;">
<span>Drop files here to upload</span>
</div>
<div class="qq-upload-button" style="position: relative; overflow: hidden; direction: ltr;">
<div>Click or Drop for upload images</div>
<input multiple="multiple" type="file" name="qqfile">
</div>
<span class="qq-drop-processing">
<span>Processing dropped files...</span>
<span class="qq-drop-processing-spinner"></span>
</span>
<ul class="qq-upload-list"></ul>
</div>
</div>
i tried
require 'phantomjs'
require 'watir'
require 'watir-webdriver'
Selenium::WebDriver::PhantomJS.path = '/phantomjs/bin/phantomjs'
b = Watir::Browser.new(:phantomjs)
b.goto("http://website.com")
b.text_field(:name => /title/).set 'My title'
b.div(:id => "restricted-fine-uploader").fire_event :click
file = "/Users/Tom/Pictures/image.jpg"
b.file_field.set(file)
and
b.div(:id => "restricted-fine-uploader").fire_event :click
file = "/Users/Tom/Pictures/image.jpg"
b.file_field(:name => "qqfile").set(file)
But it doesn't work, i have a Net::ReadTimeout error. Where am I wrong? thx
Yes this is a known phantomjs bug: https://github.com/detro/ghostdriver/issues/183
Phantomjs driver has not been actively maintained for years. The Selenium team generally recommends against its use for testing.
The new version of watir-webdriver (0.8.0) has been released. I have installed it and have been running my suites to check for changes. During this process I have noticed the text_field methods I am using to set text in <input id="some_id" type="text"> tags has been deprecated from the message Locating textareas with '#text_field' is deprecated. Please, use '#textarea' method instead. I changed the text_field method to textarea, but once I do this the element can no longer be located.
Am I using the method wrong? I tried to use it as browser.textarea(:id=> 'some_id').when_present.set "Text". Once the method times out, I get the message saying the element with the tagname textarea could not be located. Was wondering if there is something else I need the do to use the textarea method to set text in a <input id="some_id" type="text"> tag.
EDITED => html I am trying to automate on:
<form action="some_href" method="post">
<p class="align">
<label for="user">Username</label>
<input id="login" type="text" value="">
</p>
<p class="align">
<label for="pass">Password</label>
<input id="password" type="password" value="">
</p>
</form>
EDITED Watir code used to automate page
#RUBY CODE
class Login
def open
#browser = Watir::Browser.new :firefox
#browser.goto some_website_url #URL leading to page with form HTML above
end
def login_as
user_field.when_present(10).set username
password_field.when_present(10).set password
login_link.when_present(10).click
end
def user_field
#browser.text_field(:id=> "login")
end
def password_field
#browser.text_field(:id=> 'password')
end
def username
"user123"
end
def password
"p#ssw0rd"
end
def login_link
#browser.link(:text=> 'Sign-in')
end
end#Login
#STEP DEFINITION
Given(/^login to website$/) do
login = Login.new
login.open
login.login_as
end
The previous versions I have been using without the deprecation message is:
watir-webdriver (0.6.11)
selenium-webdriver (2.44.0)
Any answers would be appreciated if someone has some insight on the problem.
I have a save button on my window whose html is
<div class="popup supportsLink editor">
<h2></h2>
<a class="close icon small" href="#"></a>
<style></style>
<form method="post" enctype="multipart/form-data" action="/AirWatch/Blob/upload-blobs" data-name="blobform">
<div class="editor"></div>
<div class="footer">
<div class="actions" align="center">
<input type="submit" value="Save"></input>
<input type="button" value="Cancel" onclick="closeClosestTray($(this));"></input>
I am not able to click on the save button. when i do an exists? in irb i get a true value. Can someone help me with this.
I am still new to Watir and still am getting my bearings but maybe
browser.form(:value=>'Save').submit
If that doesn't work, is the button in a pop up? if so did you account for this?
so maybe something like this:
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end
What error do you get when you try clicking?
I made a page with your provided html and made script for clicking that and that normal code works for me, and it clicked that button and no error, I used:
browser.button(value: 'Save').click
I'm new to Watir and I'm trying to click the following login button:
<div class="container login" style="display: table;">
<div class="left">
<div class="right">
<div class="joinbox">
<form id="form_login" class="hidden-submit" method="post">
<input type="submit" value="Submit">
<div class="header">
<div class="left">
<div class="mid">
<div class="right">
<a class="button button-red submit" href="#">Log In</a>
...
So far I'm able to access this button by going through every nested div:
b.div(:class, "container login").div(:class, "right").div(:class, "joinbox")......
and so on. Is this really the best way to access this button? I assume I'm missing something. Any help is appreciated!
If there is only one link (that looks like a button) with text Log In on the page, try this:
browser.a(:text => "Log In").click
You could also use class attribute:
browser.a(:class => "button button-red submit").click
May this work?
browser.form(id,form_login).submit
BTW: I have seen some tips said safari only support GET mode for submit.
I would try:
browser.a(:href => "#").click
Assumption - "#" is unique. may need a slash so it doesn't misinterpret the #. I used something similar when I was trying to reference a button with only a href value of "/login" e.g., browser.a(:href => "/login").click.