Javascript量角器-将外部函数视为未定义

时间:2018-09-17 11:45:03

标签: javascript function protractor automated-tests

  

在specs / Test.js中是一个测试定义:“ regex2”

     

在pages / TablePage.js中是页面对象

在regex2中,尝试使用TablePage.js中的函数

<button class="btn" ><span>View Demo</span></button>
<button class="btn" ><span>View Demo Demoooo</span></button>
<button class="btn" ><span>View </span></button>

它是说 it('regex2', function(){ table_page.matchPriceRegex(table_page.workingBalanceField) });

TablePage.js中的函数本身:

table_page.matchPriceRegex is not a function

需求文件已包含在规范文件中,因此它应该可以看到

var TablePage = (function () {

  function TablePage() {
    this.workingBalanceField = element(By.xpath('//*[@id="root"]/main/section/div/div/div[5]/div/div[1]'));
  }

  TablePage.prototype.matchPriceRegex = function (locator) {
    this.text = locator.getText();
    expect(this.text).toMatch("\d{0,3}?,?\d{0,3}?\.?\d{0,3}?");
  };
});
module.exports = TablePage;

在我的IDE(WebStorm)中,按住ctrl并单击函数名称,它会正确地重定向我,如它所见

TablePage中的函数或变量的类型未定义

您知道问题出在哪里吗?

1 个答案:

答案 0 :(得分:1)

错误来自public abstract class ExpandableRecyclerAdapter : RecyclerView.Adapter { protected internal Context mContext; protected internal IList<ListItem> allItems = new List<ListItem>(); protected internal IList<ListItem> visibleItems = new List<ListItem>(); private IList<int> indexList = new List<int>(); private SparseIntArray expandMap = new SparseIntArray(); private int mode; protected internal const int TYPE_HEADER = 1000; private const int ARROW_ROTATION_DURATION = 150; public const int MODE_NORMAL = 0; public const int MODE_ACCORDION = 1; public ExpandableRecyclerAdapter(Context context) { mContext = context; } public class ListItem { public int ItemType; public ListItem(int itemType) { ItemType = itemType; } } public override long GetItemId(int i) { return i; } public override int ItemCount { get { return visibleItems == null ? 0 : visibleItems.Count; } } protected internal virtual View inflate(int resourceID, ViewGroup viewGroup) { return LayoutInflater.From(mContext).Inflate(resourceID, viewGroup, false); } public class ViewHolder : RecyclerView.ViewHolder { public ViewHolder(View view) : base(view) { } } public class HeaderViewHolder : ViewHolder,View.IOnClickListener { internal ImageView arrow; public HeaderViewHolder(View view, ImageView arrow) : base(view) { this.arrow = arrow; view.SetOnClickListener(this); } protected internal virtual void handleClick() { if (toggleExpandedItems(LayoutPosition, false)) { openArrow(arrow); } else { closeArrow(arrow); } } public virtual void bind(int position) { arrow.Rotation = isExpanded(position) ? 90 : 0; } public void OnClick(View v) { handleClick(); } } public virtual bool toggleExpandedItems(int position, bool notify) { if (isExpanded(position)) { collapseItems(position, notify); return false; } else { expandItems(position, notify); if (mode == MODE_ACCORDION) { collapseAllExcept(position); } return true; } } public virtual void expandItems(int position, bool notify) { int count = 0; int index = indexList[position]; int insert = position; for (int i = index + 1; i < allItems.Count && allItems[i].ItemType != TYPE_HEADER; i++) { insert++; count++; visibleItems.Insert(insert, allItems[i]); indexList.Insert(insert, i); } NotifyItemRangeInserted(position + 1, count); int allItemsPosition = indexList[position]; expandMap.Put(allItemsPosition, 1); if (notify) { NotifyItemChanged(position); } } public virtual void collapseItems(int position, bool notify) { int count = 0; int index = indexList[position]; for (int i = index + 1; i < allItems.Count && allItems[i].ItemType != TYPE_HEADER; i++) { count++; visibleItems.RemoveAt(position + 1); indexList.RemoveAt(position + 1); } NotifyItemRangeRemoved(position + 1, count); int allItemsPosition = indexList[position]; expandMap.Delete(allItemsPosition); if (notify) { NotifyItemChanged(position); } } public class StaticViewHolder : ViewHolder { public StaticViewHolder(View view):base(view) { } } public class ItemViewHolder : ViewHolder { public ItemViewHolder(View view) : base(view) { } } protected bool isExpanded(int position) { int allItemsPosition = indexList[position]; return expandMap.Get(allItemsPosition, -1) >= 0; } public override int GetItemViewType(int position) { return visibleItems[position].ItemType; } public void setItems(List<ListItem> items) { allItems = items; List<ListItem> visibleItems = new List<ListItem>(); expandMap.Clear(); indexList.Clear(); for (int i = 0; i < items.Count; i++) { if (items[i].ItemType == TYPE_HEADER) { indexList.Add(i); visibleItems.Add(items[i]); } } this.visibleItems = visibleItems; NotifyDataSetChanged(); } protected void notifyItemInserted(int allItemsPosition, int visiblePosition) { incrementIndexList(allItemsPosition, visiblePosition, 1); incrementExpandMapAfter(allItemsPosition, 1); if (visiblePosition >= 0) { NotifyItemInserted(visiblePosition); } } protected void removeItemAt(int visiblePosition) { int allItemsPosition = indexList[visiblePosition]; //allItems.Remove(allItemsPosition); //visibleItems.Remove(visiblePosition); incrementIndexList(allItemsPosition, visiblePosition, -1); incrementExpandMapAfter(allItemsPosition, -1); NotifyItemRemoved(visiblePosition); } private void incrementExpandMapAfter(int position, int direction) { SparseIntArray newExpandMap = new SparseIntArray(); for (int i = 0; i < expandMap.Size(); i++) { int index = expandMap.KeyAt(i); newExpandMap.Put(index < position ? index : index + direction, 1); } expandMap = newExpandMap; } private void incrementIndexList(int allItemsPosition, int visiblePosition, int direction) { List<int> newIndexList = new List<int>(); for (int i = 0; i < indexList.Count; i++) { if (i == visiblePosition) { if (direction > 0) { newIndexList.Add(allItemsPosition); } } int val = indexList[i]; newIndexList.Add(val < allItemsPosition ? val : val + direction); } indexList = newIndexList; } public void collapseAll() { collapseAllExcept(-1); } public void collapseAllExcept(int position) { for (int i = visibleItems.Count - 1; i >= 0; i--) { if (i != position && GetItemViewType(i) == TYPE_HEADER) { if (isExpanded(i)) { collapseItems(i, true); } } } } public void expandAll() { for (int i = visibleItems.Count - 1; i >= 0; i--) { if (GetItemViewType(i) == TYPE_HEADER) { if (!isExpanded(i)) { expandItems(i, true); } } } } public void expendSingle() { if (!isExpanded(2)) { expandItems(2, true); } } public static void openArrow(View view) { view.Animate().SetDuration(ARROW_ROTATION_DURATION).Rotation(90); } public static void closeArrow(View view) { view.Animate().SetDuration(ARROW_ROTATION_DURATION).Rotation(0); } public int getMode() { return mode; } public void setMode(int mode) { this.mode = mode; } } ,应该是。

public class PeopleAdapter : ExpandableRecyclerAdapter
{
    public const int TYPE_PERSON = 1001;

    public PeopleAdapter(Context context):base(context)
    {         
        setItems(getSampleItems());
    }

    public class PeopleListItem : ExpandableRecyclerAdapter.ListItem
    {
        public string Text;

        public PeopleListItem(String group):base(TYPE_HEADER)
        {
            Text = group;
        }

        public PeopleListItem(String first, String last) : base(TYPE_PERSON)
        {
            Text = first + " " + last;
        }
    }

    public class HeaderViewHolder : ExpandableRecyclerAdapter.HeaderViewHolder
    {
        TextView name;

        public HeaderViewHolder(View view): base(view, (ImageView)view.FindViewById(Resource.Id.item_arrow))
        {
            name = (TextView)view.FindViewById(Resource.Id.item_header_name);
        }

        public void bind(int position)
        {
            base.bind(position);
            name.SetText(visibleItems[position].Text);
        }
    }

    public class PersonViewHolder : ExpandableRecyclerAdapter.ViewHolder
    {
        TextView name;

        public PersonViewHolder(View view):base(view)
        {
            name = (TextView)view.FindViewById(Resource.Id.item_name);
        }
        public void bind(int position)
        {
            name.SetText(visibleItems[position].Text);
        }
    }


    public override void OnBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position)
    {
        switch (GetItemViewType(position))
        {
            case TYPE_HEADER:
                ((HeaderViewHolder)holder).bind(position);
                break;
            case TYPE_PERSON:
            default:
                ((PersonViewHolder)holder).bind(position);
                break;
        }
    }

    public override ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        switch (viewType)
        {
            case TYPE_HEADER:
                return new HeaderViewHolder(inflate(Resource.Layout.item_header, parent));
            case TYPE_PERSON:
            default:
                return new PersonViewHolder(inflate(Resource.Layout.item_person, parent));
        }
    }

    private List<PeopleListItem> getSampleItems()
    {
        List<PeopleListItem> items = new List<PeopleListItem>();

        items.Add(new PeopleListItem("Friends"));
        items.Add(new PeopleListItem("Bill", "Smith"));
        items.Add(new PeopleListItem("John", "Doe"));
        items.Add(new PeopleListItem("Frank", "Hall"));
        items.Add(new PeopleListItem("Sue", "West"));
        items.Add(new PeopleListItem("Family"));
        items.Add(new PeopleListItem("Drew", "Smith"));
        items.Add(new PeopleListItem("Chris", "Doe"));
        items.Add(new PeopleListItem("Alex", "Hall"));
        items.Add(new PeopleListItem("Associates"));
        items.Add(new PeopleListItem("John", "Jones"));
        items.Add(new PeopleListItem("Ed", "Smith"));
        items.Add(new PeopleListItem("Jane", "Hall"));
        items.Add(new PeopleListItem("Tim", "Lake"));
        items.Add(new PeopleListItem("Colleagues"));
        items.Add(new PeopleListItem("Carol", "Jones"));
        items.Add(new PeopleListItem("Alex", "Smith"));
        items.Add(new PeopleListItem("Kristin", "Hall"));
        items.Add(new PeopleListItem("Pete", "Lake"));
        items.Add(new PeopleListItem("Friends"));
        items.Add(new PeopleListItem("Bill", "Smith"));
        items.Add(new PeopleListItem("John", "Doe"));
        items.Add(new PeopleListItem("Frank", "Hall"));
        items.Add(new PeopleListItem("Sue", "West"));
        items.Add(new PeopleListItem("Family"));
        items.Add(new PeopleListItem("Drew", "Smith"));
        items.Add(new PeopleListItem("Chris", "Doe"));
        items.Add(new PeopleListItem("Alex", "Hall"));
        items.Add(new PeopleListItem("Associates"));
        items.Add(new PeopleListItem("John", "Jones"));
        items.Add(new PeopleListItem("Ed", "Smith"));
        items.Add(new PeopleListItem("Jane", "Hall"));
        items.Add(new PeopleListItem("Tim", "Lake"));
        items.Add(new PeopleListItem("Colleagues"));
        items.Add(new PeopleListItem("Carol", "Jones"));
        items.Add(new PeopleListItem("Alex", "Smith"));
        items.Add(new PeopleListItem("Kristin", "Hall"));
        items.Add(new PeopleListItem("Pete", "Lake"));
        items.Add(new PeopleListItem("Friends"));
        items.Add(new PeopleListItem("Bill", "Smith"));
        items.Add(new PeopleListItem("John", "Doe"));
        items.Add(new PeopleListItem("Frank", "Hall"));
        items.Add(new PeopleListItem("Sue", "West"));
        items.Add(new PeopleListItem("Family"));
        items.Add(new PeopleListItem("Drew", "Smith"));
        items.Add(new PeopleListItem("Chris", "Doe"));
        items.Add(new PeopleListItem("Alex", "Hall"));
        items.Add(new PeopleListItem("Associates"));
        items.Add(new PeopleListItem("John", "Jones"));
        items.Add(new PeopleListItem("Ed", "Smith"));
        items.Add(new PeopleListItem("Jane", "Hall"));
        items.Add(new PeopleListItem("Tim", "Lake"));
        items.Add(new PeopleListItem("Colleagues"));
        items.Add(new PeopleListItem("Carol", "Jones"));
        items.Add(new PeopleListItem("Alex", "Smith"));
        items.Add(new PeopleListItem("Kristin", "Hall"));
        items.Add(new PeopleListItem("Pete", "Lake"));
        return items;
    }}