cancel
Showing results for 
Search instead for 
Did you mean: 

[TestComplete JS] ClickItem() is not working for dropdown

Occasional Contributor

[TestComplete JS] ClickItem() is not working for dropdown

Hi,

 

I have a web application which has a multi-select dropdown with some values which looks the one in screenshot named dropdown

 

(Note: There are some values in dropdown but I have overwritten by the highlighter as its not necessary info for this).

 

My requirement is that I need to click on 1 of the items in the dropdown based on the text. For example, if I have a text named as "Apple" in the dropdown. I want to click on that. I similated the action and converted to script using Record Script. I got the below command for this:

vselect.ClickItem("Apple");

 

But this is not working. When I re-run this script it throws the error: "Cannot activate the 'Apple' item"

 

Can you please help?

5 REPLIES 5
Community Hero

Re: [TestComplete JS] ClickItem() is not working for dropdown

We need to know a bit more about the drop down component itself.  A screenshot like that with all the items masked... doesn't really help us help you.  If you can post the Object Browser views of the component, expanded to Advanced, with all properties and methods, that will help us help you.

 

That said... If this is a multi-select drop down, ClickItem might not be the best option.  Is there a "SelectItem" method available?


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Occasional Contributor

Re: [TestComplete JS] ClickItem() is not working for dropdown

Hi,

 

Yes, SelectItem() method is available. I have tried this method too but still it didn't work. I have attached the methods available for the web object in advanced view. PFA the screenshots. 

 

This is how the web element definition looks like:

<select name="kjsja" class="fieldList" id="" ondblclick="xyz();" size="13" multiple="multiple">
<option value="abc123">abc</option>
<option value="def123">def</option>
..
..
..
</select>

 

Few more details:

1) In the dropdown there is a scroll bar and there are multiple items. The item which I need to select, is not visible without scrolling, so I need to scroll the scroll bar and then select the item. Do I need to use the method scrollIntoView() first to achieve this? I have tried it, but not sure if the syntax was correct.

 

2) There are multiple items with similar name. For example, I want to select "Sales" but there are few more items starting with "Sales" such as "Sales Growth" , "Sales Growth Per Yr" and so on.

 

Code snippet:

getDropdownObj() {

//code to return the dropdown web object

}

//Below lines of code I have tried to select the desired item in dropdown (none of these worked)

this.getDropdownObj().ClickItem("Sales"); 

this.getDropdownObj().SelectItem("Sales",1); 

this.getDropdownObj().SelectItem("Sales"); 

this.getDropdownObj().SelectItem(14,1); //here 14 is the index of "Sales", counted from 0 for all elements present in the dropdown

 

Please let me know if any other details are required

 

Community Hero

Re: [TestComplete JS] ClickItem() is not working for dropdown

Yes, on the dropdown window panel that scrolls, you need to find a way to scroll to what you want if ClickItem or SelectItem isn't auto-scrolling.  THat's probably what's going on here.  


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Occasional Contributor

Re: [TestComplete JS] ClickItem() is not working for dropdown

Hi,

 

I am still facing issues with selecting an option in the dropdown. Please find below approaches that I have tried and its results below:

 

Tried below actions on the item which is within focus (No scrolling of the dropdown needed to select this item in dropdown)

var webElement = page.FindChildByXPath(xpath);

1) Directly clicking on the element using below code:

     webElement.Click() -> It doesn't throw any exception. But doesn't select the desired element too

2) Double clicking also selects the element, so tried this too using code below:

     webElement.DblClick() -> It throws runtime exception as shown in attached screenshot

3) Tried mouse hover and then click on the element using code below:

     webElement.HoverMouse() -> It throws runtime exception same as shown in attached screenshot

 

This dropdown is present in a pop-up. Only selecting an element within the dropdown is not working. But any other actions in the same pop-up works fine.

 

Please let me know what else could I do here to fix this issue.

Community Hero

Re: [TestComplete JS] ClickItem() is not working for dropdown

Hi,

 

> var webElement = page.FindChildByXPath(xpath);

Well... As it is documented, when native DOM XPath search returns a result, TestComplete tries to match the found web element to the object from TestComplete's Object Tree (displayed in the Object Browser). If match succeeds, then TestComplete returns wrapper object that contains .Click(), .DblClick(), .HoverMouse() and so on methods. If match attempt fails, native DOM object is returned without any method provided by TestComplete. In the latter case you must call native DOM methods - .click() and others.

The above is one out of many other reasons of why search by XPath is highly not recommended in the TestComplete world.

What I would try to do is to check using Object Spy whether or not dropdown items are identified as separate web elements. If they do, then you need to open dropdown, search for the required item and click it. Use .scrollIntoView() native DOM function to scroll web element into view (see below).

If the problematic dropdown is a native HTML web element, then you may ask Support directly via the https://support.smartbear.com/message/?prod=TestComplete form why .ClickItem() method does not work.

 

 

//----------------------------------------------------------------------------

function scrollIntoView(element) {
  if (aqObject.IsSupported(element, "scrollIntoView"))
  {
    if (!element.VisibleOnScreen)
      element.scrollIntoView(true);
    if (!element.VisibleOnScreen)
      element.scrollIntoView(false);
  }
  if (aqObject.IsSupported(element, "focus"))
    element.focus();
}
//----------------------------------------------------------------------------

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video:
Watch the New Interview
APITestingMistake#2
Top Kudoed Authors