量角器中的'$'选择器 [英] '$' selector in Protractor
问题描述
我在许多示例中都看到过这样的$('.selector')
,并且我也在使用它.那么此 $ 变量的作用是什么.这是我从量角器 docs 中得到的.
I have seen in many examples like this $('.selector')
and I am also using this. So what this $ variable does. This is what I got from the protractor docs.
对$的调用可以链接在一起,以在父级中查找元素.
Calls to $ may be chained to find elements within a parent.
在文档中没有单独使用$
的示例.我们正在使用$
与element
选择器链接.
There is no example in docs which use $
alone. We are using $
to chain with element
selector.
$('.selector')
本身也是一个元素,当我们执行此element($('.selector'))
时,这是一个错误.
Also $('.selector')
itself is an element, when we does this element($('.selector'))
, it is an error.
所以如何在量角器中使用此$
选择器.它具有JQuery $
的所有功能吗?我尝试了$('.selector').children
,说children
不是函数.
So how to use this $
selector in protractor. Does it have all the features of JQuery $
. I tried $('.selector').children
which says children
is not a function.
非常感谢您的帮助.
谢谢!
推荐答案
外观类似于jQuery语法,但事实并非如此,它是量角器的一部分.这就是.children
抛出错误的原因,因为我们实际上并未使用jQuery. $
是element(by.css())
的简写版本,即
That looks like jQuery syntax but it is not, it's part of Protractor. That's why .children
is throwing an error because we're not actually using jQuery. $
is a shorthanded version of element(by.css())
i.e.
$('my-css');
与element(by.css('my-css'));
它们还具有与element.all(by.css())
相同的$$
尽管缺少文档,但不是 必须使用进行链接以查找子元素.即使用Julie的量角器演示程序(我修改了示例):
Despite the lack of documentation, it does not have to be used for chaining to find child elements. i.e. using Julie's protractor demo (I modified the example):
describe('Protractor Demo App', function() {
it('read the header', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
$('h3').getText().then(function (val) {
console.log(val);
});
});
});
这会打印出我所在的h3元素的标题. $
和$$
只是CSS选择器的简写.
That prints out the title of the h3 element that I located. The $
and $$
are simply a shorthand for css selectors.
来源:此处为$$ , 此处为$ 和这也是我找到的不错的文档(尽管它没有提到使用$$
: http://luxiyalu.com/protractor-locators-selectors/
Also this is a nice document I found (though it does not mention the use of $$
: http://luxiyalu.com/protractor-locators-selectors/
这篇关于量角器中的'$'选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!