New Ranks & Badges For The Community!
Notice something different? The ranks and associated badges have gone "Star Fleet". See what they all mean HERE
Highlighted
Captain
Captain
1050 views

TruClient - forcing OnEvent Trigger after performing a ListBox SelectedIndex change

I am working on this page which contains two separate listboxes where the second listbox does not activate (and retrieve values) until the first listbox value has been selected.

I don't want to override a size if one has been selected already, so I am checking to determine if the first index item is currently selected, and - if NOT - using object.selectedIndex to change the index selection to index #1 instead of index #0 using the below code:

if (object.selectedIndex == 0){
  object.selectedIndex = 1;
  }

The challenge that I am having is that this selection method is not triggering the listbox onchange event, thus the second listbox never populates and turn editable.

How do I trigger the object onload event using TruClient JS, or do I need to use an if statement to detect the selectedindex == 0 and then have a followup step (if true) to change the listbox value? If I need to use this second option how do I force the selection to be the listitem with Index == 1 regardless of what txt might be present?

0 Likes
3 Replies
Highlighted
Admiral
Admiral

Hi, kjackey,

You can achieve this functionality within TruClient's model, without Javascript code:

  1. Add an "If Verify" step to check if the first listbox does not have a value selected (its visible text is "Select Size").
  2. Drag your listbox selection step (either recorded or added manually as a "Generic Object Action" step) into the "If Verify" step group.
  3. You can select the first item in a listbox by stating its ordinal number in the selection step arguments. Note that ordinal 0 indicates selecting a random value and ordinal 1 is the "Select Size" option, so you should choose ordinal 2.

See this screenshot for an example:

2017-12-13 15_45_30-TruClient - TruClient18.png

 

Edo

0 Likes
Captain
Captain

Thanks for the reply, apparently the "Visible Test" element of the indicated listbox contains ALL the values listed in the listbox because regardless of what value is currently selected the IF condition is being met and the selected value gets changed. This creates an infinite loop if there is more than one listbox on the screen because setting the first resets the values in the second and I can't proceed past the screen if all listboxes for a given item are not populated correctly.

This is complicated by the fact that there can be a varying number of listboxes on the screen as well (possibly in changing order), so I have to be able to ensure that pre-populated listboxes are left alone.

Wondering if I need to switch to using a "Execute JS on object" for each individual listbox, record the object.selectedIndex to a parameter, then evaluate that parameter in a follow-on step to see if the parameter is currently set to zero or not. Thoughts?

 

EDIT: Not finding the TruClient item I need to create an IF statement around a parameter value that would allow me to use the TruClient methods to then select the listbox value

0 Likes
Highlighted
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

Hi, kjackey,

For my understanding, you are trying to change the value of a select item if, and only if, the selectionIndex is 0. Meaning, if the select was not previously modified. The issue you are encountering is while changing the selectedIndex, the change event is not being triggered. One way to do so, is to use Evaluate JS on Object, as you've already done and write the following code:

// Create change event for a given object

function dispatchChangeEvent(obj){
   var changeEvent = obj.ownerDocument.createEvent("Event");
   changeEvent.initEvent("change", true, true);
   obj.dispatchEvent(changeEvent);
}

if (object.selectedIndex == 0) { // Check item was not modified

   // Select the first item

   object.selectedIndex = 1;

  // Trigger change event for the select item
   dispatchChangeEvent(object);
}

Also, in case you would like to select a random item, you can use the following code:

// Get max item to select

var maxElement = Math.max(0, object.length - 1);
if (object.selectedIndex == 0) { // Check item was not modified

   // Select a random element starting from 1 to maxElement
   object.selectedIndex = Math.floor((Math.random() * maxElement) + 1);

  // Trigger change event for the select item
  dispatchChangeEvent(object);
}

Hope that helps,

 Eyal

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.