Ruby Watir, Dijit Widget, set value from "select list" - ruby

I'd like to be able to click (to see available options/list of accounts, seem to be hidden to start with) and then set a dijit widget "select_list" to a certain value.
I can find the object using
##ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").flash
#works
but cannot use click or set with it:
##ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").click
#no errors, but doesn't do anything
##ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").set("Account2")
#NoMethodError: undefined method `set' for <Watir::HTMLElement:0x4d6af60>
##ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").send_keys("Account3")
#NoMethodError: undefined method `send_keys' for <Watir::HTMLElement:0x4d6af60>
In the past the site was implemented using plain html and had a select_list, so
##ie.select_list(:name, "link").set(/Account1/i)
worked just fine.
Here's the current html behind the site:
<table id="accountSwitcherSelect" class="dijit dijitReset dijitInline dijitLeft dijitDownArrowButton dijitSelectFixedWidth dijitValidationTextBoxFixedWidth dijitSelect dijitValidationTextBox" lang="en-US" cellspacing="0" cellpadding="0" aria-haspopup="true" role="listbox" data-dojo-attach-point="_buttonNode,tableNode,focusNode" style="-moz-user-select: none; width: 207px;" tabindex="0" widgetid="accountSwitcherSelect" aria-expanded="false" aria-invalid="false">
<tbody role="presentation">
<tr role="presentation">
<td class="dijitReset dijitStretch dijitButtonContents" role="presentation">
<div class="dijitReset dijitInputField dijitButtonText" role="presentation" data-dojo-attach-point="containerNode,_popupStateNode" popupactive="true">
<span class="dijitReset dijitInline dijitSelectLabel dijitValidationTextBoxLabel " role="option">Account1</span>
</div>
<div class="dijitReset dijitValidationContainer">
</td>
<td class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" data-dojo-attach-point="titleNode">
<input class="dijitReset dijitInputField dijitArrowButtonInner" type="text" role="presentation" readonly="readonly" tabindex="-1" value="? ">
</td>

Assuming that the the "select list" is similar to those on the digit.form.Select documentation page, you need to:
Click the dropdown arrow (an input element)
Click the cell that has the text you want (a td element)
In your case, it would look like:
##ie.input(:class => 'dijitArrowButtonInner').click
##ie.td(:class => 'dijitMenuItemLabel', :text => /Account1/i).click
Here is a working example (though it relies on the dijit css files still being available online).
require 'watir-webdriver'
browser = Watir::Browser.new
browser.goto "data:text/html,#{DATA.read}"
browser.input(:class => 'dijitArrowButtonInner').click
browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click
sleep(10) # Just so that you can see the dropdown has changed
browser.close
__END__
<html>
<head>
<link rel="stylesheet" href="https://dojotoolkit.org/reference-guide/1.9/_static/js/dijit/themes/claro/claro.css">
<script>dojoConfig = {async: true, parseOnLoad: true}</script>
<script src="https://dojotoolkit.org/reference-guide/1.9/_static/js/dojo/dojo.js"></script>
<script>require(["dojo/parser", "dijit/form/Select"]);</script>
</head>
<body class="claro">
<div name="select3" value="AL" data-dojo-type="dijit/form/Select">
<span value="AL"><b>Alabama</b></span>
<span value="AZ"><i>Arizona</i></span>
</div>
</body>
</html>
Watir-Classic
The above solution works for watir-webdriver, but not watir-classic. Clicking the dropdown (actually a table) is not triggering sufficient events to open the dropdown. As a work around, it seems you can use send_keys to trigger the right events.
Assuming that browser is opened to a page with the above html, the following will work:
browser.table(:id => 'dijit_form_Select_0').send_keys :enter
browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click
Note that the table in this code is the selected option (when the dropdown is closed).

Related

Unable to change drop-down selection in Ruby Watir

I am trying to select values one by one in a dropdown and having difficulty.
selenium-webdriver (4.0.3)
watir (7.0.0)
ChromeDriver 94.0.4606.81
The dropdown is md-select which on click displays a hidden div with 2 choices.
<md-select ng-if="vm.datas.length" ng-model="vm.activeData" ng-change="vm.refreshDash()" id="active-data-select" aria-label="data selector" class="ng-pristine ng-valid md-default-theme ng-empty ng-touched" tabindex="0" aria-disabled="false" role="button" aria-haspopup="listbox" aria-invalid="false" aria-labelledby="active-data-select select_value_label_8" style="" aria-owns="select_listbox_10">
<md-select-value class="md-select-value" id="select_value_label_8"><span><div class="md-text">My test data 1</div></span><span class="md-select-icon" aria-hidden="true"></span></md-select-value>
</md-select>
<div class="md-select-menu-container md-default-theme md-active md-clickable" aria-hidden="false" role="presentation" id="select_container_9" style="display: block; left: 507px; top: 8px; min-width: 336.891px; font-size: 20px;">
<md-select-menu role="presentation" class="_md md-default-theme" style="transform-origin: 152.445px 32px 0px;">
<md-content role="listbox" tabindex="-1" aria-multiselectable="false" class="_md md-default-theme" id="select_listbox_10" aria-activedescendant="select_option_14">
<!---->
<md-option ng-repeat="data in ::vm.datas | orderBy:'name' track by $index" ng-value="data" value="[object Object]" ng-class="{'selected': data.name == vm.activeData.name }" tabindex="0" class="md-default-theme selected md-focused" role="option" id="select_option_14" selected="selected" aria-selected="true"><div class="md-text">My test data 1</div></md-option>
<!---->
<md-option ng-repeat="data in ::vm.datas | orderBy:'name' track by $index" ng-value="data" value="[object Object]" ng-class="{'selected': data.name == vm.activeData.name }" tabindex="0" class="md-default-theme" role="option" id="select_option_15"><div class="md-text">My test data 2</div></md-option>
<!---->
</md-content>
</md-select-menu>
</div>
When I am trying to do the below to display the hidden div to select next value:
browser.element(id: 'active-data-select').click
I get the following error:
element located, but timed out after 30 seconds, waiting for #<Watir::HTMLElement: located: true; {:id=>"active-data-select"}> to be present
I have tried many different things, but not able to find a solution yet. Any ideas?
Thank you,
-Andrey
If you are certain that clicking select element will solve your problem
browser.element(id: 'active-data-select').fire_event :click
will perform click even if element is obscured or not even on screen
You can also try
browser.element(id: 'active-data-select').send_keys :enter
or
browser.element(id: 'active-data-select').send_keys [:down, :down, :enter]
In your html it seems like div select_container_9 is holding the select with options elements, so try
browser.div(id: "select_container_9").select_list.options.first.select
It seems like it's id is autogenerated so different locator should be used in case it works
But you should probably debug how many selects there are on page with
browser.selects.count then you can flash each select to pinpoint the one you are after with browser.selects[x].flash then count how many options are available in that select with browser.selects[1].options.count and then you can proceed with selecting the option you want with browser.selects[1].options[1].select and then improve locators

Click on deeply nested div in Watir

I am trying to click on checkbox (which represents as ) in Firefox using Watir. I have code left to me from the previous tester, and this code works in Chrome, but not Firefox.
Here is what I have.
Here is the code that leads to this div:
<div class="x-grid3-body" style="width:515px;" id="ext-gen201">
<div class="x-grid3-row x-grid3-row-first" style="width:515px;">
<table class="x-grid3-row-table" border="0" cellspacing="0" cellpadding="0" style="width:515px;">
<tbody>
<tr>
<td class="x-grid3-col x-grid3-cell x-grid3-td-0 x-grid3-cell-first " style="width: 158px;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-0 x-unselectable" unselectable="on">Organisation</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-1 " style="width: 298px;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-1 x-unselectable" unselectable="on">Catch Software</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-scopeCheckColumn x-grid3-cell-last x-grid3-check-col-td" style="width: 53px;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-scopeCheckColumn x-unselectable" unselectable="on">
<div class="x-grid3-check-col x-grid3-cc-scopeCheckColumn"> </div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
What I need essentially is to click on this div
<div class="x-grid3-check-col x-grid3-cc-scopeCheckColumn"> </div>
I can't attach screenshots (rating is low), but this div is the checkbox that I need to click.
This piece of code works in Chrome:
#browser.div(:class => 'x-box-inner', :index => 1).table(:class => 'x-grid3-row-table').td(:text => 'Organisation').parent.td(:index => 2)
But in Firefox I can see that Watir is just click on the whole parent div (can see selection appear in browser), not on the checkbox div.
Thank you.
First off, there is only one element in the html you've showed that has the class 'x-grid3-cc-scopeCheckColumn', so is there a reason you can't do:
#browser.td(class: 'x-grid3-cc-scopeCheckColumn').click
If not, you can simplify your element location drastically to get the td you want, since there is only one td that has the text 'Organisation', and .parent would only get the td above it, but it looks like you want the tr tag above that, so perhaps you want:
#browser.td(text: 'Organisation').parent.parent.td(index: 2)
Using an XPath worked for me in Firefox:
#browser.div(:xpath, "//*[#id='ext-gen201']/div/table/tbody/tr/td[3]/div/div").click
I was able to verify it with .flash in place of .click since your div just contains a space and there was nothing to otherwise see.

Selenium IDE: How to detect the xpath's if div id's , class are randomly generated every time?

I am trying to detect the xpath or css but everytime I run the script, the div id's and class names change which there by fails the script.
<div class="yui-dt-bd" style="height: 300px; width: 100%;">
<table id="yuievtautoid-0" summary="" style="margin-top: 0px;">
<tr id="yui-rec28" class="yui-dt-rec yui-dt-first yui-dt-even yui-dt-selected" style="">
<td id="yui-gen52" class="yui-dt23-col-professorId yui-dt-col-professorId yui-dt- sortable yui-dt-first" headers="yui-dt23-th-professorId ">
<div id="yui-gen51" class="yui-dt-liner">1</div>
</td>
<td id="yui-gen44" class="yui-dt23-col-professorName yui-dt-col-professorName yui-dt-sortable yui-dt-last" headers="yui-dt23-th-professorName ">
<div id="yui-gen43" class="yui-dt-liner">John Power</div>
</td>
</tr>
</table>
</div>
I had written xpath=//*[#id="yui-gen46"], but id's keep changing. Tried writing table id too. But it does not work.
xpath=id('yuievtautoid-1').
Appreciate some input .
You can specify a part of class or id that is not changed. For example:
//*[contains(#class, 'col-professorName')]
or
//*[contains(#id, 'yuievtautoid')]
or CSS versions:
css=*[class*="col-professorName"]
css=*[id^="yuievtautoid"]

Firefox displays 3 columns in a table, IE8 only 2

Would love some help here... Firefox displays the last column in the table (an image they click on to edit their email address, it's a link), and IE8 displays nothing for the last column (doesn't even appear to display a column!) I've left out other rows in the table, but similar stuff happens.
Anyone know why?
<table class="profile-display">
<tr>
<td style="text-align: right; color: red;"> Email address: </td>
<td class="profile-content"> <?php echo("$evar"); ?> </td>
<td> <a href="profile_change.php?edit=13"
<img src="../images/writegreen.png" class="profile-edit" alt="Edit"
title="Edit Email Address"
border="0" />
</a>
</td>
</tr>
</table>
Your <a> tag is missing its >. That will cause a browser to not recognize the end of the tag until the first > it sees, which is the end of the img tag. Frankly, I'm surprised that Firefox shows the img.
Edit: Other common causes of this problem are missing quotes and misspelled tags.

Dojo Table not Rendering in IE6

I'm trying to use Dojo (1.3) checkBoxes to make columns appear/hide in a Dojo Grid that's displayed below the checkBoxes. I got that functionality to work fine, but I wanted to organize my checkBoxes a little better. So I tried putting them in a table. My dojo.addOnLoad function looks like this:
dojo.addOnLoad(function(){
var checkBoxes = [];
var container = dojo.byId('checkBoxContainer');
var table = dojo.doc.createElement("table");
var row1= dojo.doc.createElement("tr");
var row2= dojo.doc.createElement("tr");
var row3= dojo.doc.createElement("tr");
dojo.forEach(grid.layout.cells, function(cell, index){
//Add a new "td" element to one of the three rows
});
dojo.place(addRow, table);
dojo.place(removeRow, table);
dojo.place(findReplaceRow, table);
dojo.place(table, container);
});
What's frustrating is:
Using the Dojo debugger I can see that the HTML is being properly generated for the table.
I can take that HTML and put just the table in an empty HTML file and it renders the checkBoxes in the table just fine.
The page renders correctly in Firefox, just not IE6.
The HTML that is being generated looks like so:
<div id="checkBoxContainer">
<table>
<tr>
<td>
<div class="dijitReset dijitInline dijitCheckBox"
role="presentation" widgetid="dijit_form_CheckBox_0"
wairole="presentation">
<input class="dijitReset dijitCheckBoxInput"
id="dijit_form_CheckBox_0"
tabindex="0" type="checkbox"
name="" dojoattachevent=
"onmouseover:_onMouse,onmouseout:_onMouse,onclick:_onClick"
dojoattachpoint="focusNode" unselectable="on"
aria-pressed="false"/>
</div>
<label for="dijit_form_CheckBox_0">
Column 1
</label>
</td>
<td>
<div class="dijitReset dijitInline dijitCheckBox"
role="presentation" widgetid="dijit_form_CheckBox_1"
wairole="presentation">
<input class="dijitReset dijitCheckBoxInput"
id="dijit_form_CheckBox_1"
tabindex="0" type="checkbox"
name="" dojoattachevent=
"onmouseover:_onMouse,onmouseout:_onMouse,onclick:_onClick"
dojoattachpoint="focusNode" unselectable="on"
aria-pressed="false"/>
</div>
</td>
</tr>
<tr>
...
</tr>
</table>
</div>
I would have posted to the official DOJO forums, but it says they're deprecated and they're using a mailing list now. They said if a mailing list doesn't work for you, use StackOverflow.com. So, here I am!
It looks like you forgot to create a <tbody> element.
I also encountered this issue when trying to generate a table using dojo in IE7. The html is there but nothing is rendered. Again, the solution is to use thead, tbody tags.

Resources