Example #1
0
        private int ComputeIndex(RecEventArgs evt, String twebstTagName, String attr, String attrVal)
        {
            try
            {
                String           uniqueValue     = DateTime.Now.ToString();
                IHTMLElement     htmlTarget      = evt.TargetElement;
                String           searchCondition = null;
                IElementList     elements        = null;
                bool             isInnerText     = (attr == "innertext");
                ICustomRecording customRec       = this.languageGenerator as ICustomRecording;

                htmlTarget.setAttribute(CatStudioConstants.FIND_INDEX_HELPER_ATTR, uniqueValue, 0);

                if ((attr != null) && (attrVal != null) && !isInnerText)
                {
                    searchCondition = String.Format("{0}={1}", attr, attrVal);

                    if ((customRec == null) || !customRec.LocalIndex)
                    {
                        elements = evt.Browser.FindAllElements(twebstTagName, searchCondition);
                    }
                    else
                    {
                        ICore    core       = CoreWrapper.Instance;
                        IElement targetElem = core.AttachToNativeElement(evt.TargetElement);

                        // Search only elements in current frame.
                        elements = targetElem.parentFrame.FindChildrenElements(twebstTagName, searchCondition);
                    }
                }
                else
                {
                    if ((customRec == null) || !customRec.LocalIndex)
                    {
                        elements = evt.Browser.FindAllElements(twebstTagName, "");
                    }
                    else
                    {
                        ICore    core       = CoreWrapper.Instance;
                        IElement targetElem = core.AttachToNativeElement(evt.TargetElement);

                        // Search only elements in current frame.
                        elements = targetElem.parentFrame.FindChildrenElements(twebstTagName, "");
                    }
                }

                int index  = 0;
                int length = elements.length;

                if (length <= 1)
                {
                    return(0);
                }

                if (!isInnerText)
                {
                    for (int i = 0; i < length; ++i)
                    {
                        String findIndexAttrValue = (String)elements[i].GetAttribute(CatStudioConstants.FIND_INDEX_HELPER_ATTR);

                        if (uniqueValue == findIndexAttrValue)
                        {
                            index = i;
                            break;
                        }
                    }
                }
                else
                {
                    // Watir uses innerText but Twebst does not search for innerText.
                    for (int i = 0; i < length; ++i)
                    {
                        String findIndexAttrValue = (String)elements[i].GetAttribute(CatStudioConstants.FIND_INDEX_HELPER_ATTR);
                        if (uniqueValue == findIndexAttrValue)
                        {
                            break;
                        }

                        if (attrVal == elements[i].nativeElement.innerText)
                        {
                            index++;
                        }
                    }
                }

                return(index);
            }
            catch
            {
                // Something went wrong, give up using index.
                return(0);
            }
        }