量角器链式元素使用变量? [英] Protractor Chained Elements by Using Variables?

查看:40
本文介绍了量角器链式元素使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 Protractor 中的 pageObjects 尽可能干净,但在选择子元素时遇到了一些行为.

I am trying to keep my pageObjects in Protractor as clean as possible, but have run up against some behavior in selecting sub-elements.

这有效:

var parent = element(by.css('.parent-class'));

parent.element(by.css('.child-class')).getText();

然而这不是:

var parent = element(by.css('.parent-class'));
var child = element(by.css('.child-class'));

parent.child.getText();

有什么办法可以做类似第二个例子的事情吗?我宁愿不让元素定位器遍布我的 pageObjects 上的方法,但似乎这是定位子元素的唯一方法?

Is there someway to do something like the second example? I'd rather not have the element locators spread throughout the methods on my pageObjects, but it seems thats the only way to locate subelements?

在实际应用中,我有一长串卡片,我从中筛选出我正在寻找的卡片.然后我想对卡片的子元素做一些事情.

In actual application I have a long list of cards, from which I filter down to just the one I am looking for. I then want to do things with subelements of the card.

推荐答案

您可以使用 locator() 函数获取子元素的定位器并使用它来查找父元素的子元素.这类似于您在评论中提供的解决方案,但允许您将页面对象上的所有属性定义为网络元素,而不是元素和定位器的混合:

You could use the locator() function to get the locator of the child element and use it to find a child of the parent. This is similar to the solution you provided in your comment, but allows you to define all properties on your page object as web elements instead of a mix of elements and locators:

var parent = element(by.css('.parent-class'));
var child = element(by.css('.child-class'));

parent.element(child.locator()).getText();

这篇关于量角器链式元素使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆