量角器 – 核心 APIS(CONTD…)
量角器 – 核心 APIS(CONTD…)
在本章中,让我们学习更多 Protractor 的核心 API。
元素 API
元素是量角器公开的全局函数之一。此函数采用定位器并返回以下内容 –
- ElementFinder,根据定位器查找单个元素。
- ElementArrayFinder,根据定位器查找元素数组。
上述两种方法都支持下面讨论的链接方法。
ElementArrayFinder 的链式函数及其说明
以下是 ElementArrayFinder 的功能 –
element.all(locator).clone
顾名思义,此函数将创建元素数组的浅表副本,即 ElementArrayFinder。
element.all(定位器).all(定位器)
这个函数基本上返回一个新的 ElementArrayFinder ,它可以是空的或包含子元素。它可以用于选择多个元素作为数组,如下所示
例子
element.all(locator).all(locator) elementArr.all(by.css(‘.childselector’)); // it will return another ElementFindArray as child element based on child locator.
element.all(locator).filter(filterFn)
顾名思义,在对 ElementArrayFinder 中的每个元素应用过滤函数后,它会返回一个新的 ElementArrayFinder,其中包含所有通过过滤函数的元素。它基本上有两个参数,第一个是 ElementFinder,第二个是索引。它也可以用于页面对象。
例子
看法
<ul class = "items"> <li class = "one">First</li> <li class = "two">Second</li> <li class = "three">Third</li> </ul>
代码
element.all(by.css('.items li')).filter(function(elem, index) {
return elem.getText().then(function(text) {
return text === 'Third';
});
}).first().click();
element.all(locator).get(index)
借助这个,我们可以通过索引获取 ElementArrayFinder 中的元素。请注意,索引从 0 开始,负索引被包裹。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let list = element.all(by.css('.items li'));
expect(list.get(0).getText()).toBe('First');
expect(list.get(1).getText()).toBe('Second');
element.all(locator).first()
顾名思义,这将获得 ElementArrayFinder 的第一个元素。它不会检索底层元素。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let first = element.all(by.css('.items li')).first();
expect(first.getText()).toBe('First');
element.all(locator).last()
顾名思义,这将获得 ElementArrayFinder 的最后一个元素。它不会检索底层元素。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let first = element.all(by.css('.items li')).last();
expect(last.getText()).toBe('Third');
element.all(定位器).all(选择器)
当对 $$ 的调用可能被链接时,它用于在父级中查找元素数组。
例子
看法
<div class = "parent">
<ul>
<li class = "one">First</li>
<li class = "two">Second</li>
<li class = "three">Third</li>
</ul>
</div>
代码
let items = element(by.css('.parent')).$$('li');
element.all(locator).count()
顾名思义,这将计算 ElementArrayFinder 表示的元素数量。它不会检索底层元素。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let list = element.all(by.css('.items li'));
expect(list.count()).toBe(3);
element.all(locator).isPresent()
它将与查找器匹配元素。它可以返回真或假。True,如果存在与查找器匹配的任何元素,否则为 False。
例子
expect($('.item').isPresent()).toBeTruthy();
element.all(定位器).定位器
顾名思义,它将返回最相关的定位器。
例子
$('#ID1').locator();
// returns by.css('#ID1')
$('#ID1').$('#ID2').locator();
// returns by.css('#ID2')
$$('#ID1').filter(filterFn).get(0).click().locator();
// returns by.css('#ID1')
element.all(locator).then(thenFunction)
它将检索由 ElementArrayFinder 表示的元素。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
element.all(by.css('.items li')).then(function(arr) {
expect(arr.length).toEqual(3);
});
element.all(locator).each(eachFunction)
顾名思义,它会在每个由 ElementArrayFinder 表示的 ElementFinder 上调用输入函数。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
element.all(by.css('.items li')).each(function(element, index) {
// It will print First 0, Second 1 and Third 2.
element.getText().then(function (text) {
console.log(index, text);
});
});
element.all(locator).map(mapFunction)
顾名思义,它将在 ElementArrayFinder 中的每个元素上应用映射函数。它有两个论点。第一个是 ElementFinder,第二个是索引。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let items = element.all(by.css('.items li')).map(function(elm, index) {
return {
index: index,
text: elm.getText(),
class: elm.getAttribute('class')
};
});
expect(items).toEqual([
{index: 0, text: 'First', class: 'one'},
{index: 1, text: 'Second', class: 'two'},
{index: 2, text: 'Third', class: 'three'}
]);
element.all(locator).reduce(reduceFn)
顾名思义,它将对累加器和使用定位器找到的每个元素应用 reduce 函数。此函数会将每个元素减少为一个值。
例子
看法
<ul class = "items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
代码
let value = element.all(by.css('.items li')).reduce(function(acc, elem) {
return elem.getText().then(function(text) {
return acc + text + ' ';
});
}, '');
expect(value).toEqual('First Second Third ');
element.all(locator).evaluate
顾名思义,它会评估输入是否在当前底层元素的范围内。
例子
看法
<span class = "foo">{{letiableInScope}}</span>
代码
let value =
element.all(by.css('.foo')).evaluate('letiableInScope');
element.all(locator).allowAnimations
顾名思义,它将确定当前底层元素是否允许动画。
例子
element(by.css('body')).allowAnimations(false);
ElementFinder 的链接函数及其说明
ElementFinder 的链接功能及其描述 –
元素(定位器)。克隆
顾名思义,此函数将创建 ElementFinder 的浅表副本。
元素(定位器)。getWebElement()
它将返回由此 ElementFinder 表示的 WebElement,如果该元素不存在,则会引发 WebDriver 错误。
例子
看法
<div class="parent"> some text </div>
代码
// All the four following expressions are equivalent.
$('.parent').getWebElement();
element(by.css('.parent')).getWebElement();
browser.driver.findElement(by.css('.parent'));
browser.findElement(by.css('.parent'));
元素(定位器)。所有(定位器)
它将在父级中找到一组元素。
例子
看法
<div class = "parent">
<ul>
<li class = "one">First</li>
<li class = "two">Second</li>
<li class = "three">Third</li>
</ul>
</div>
代码
let items = element(by.css('.parent')).all(by.tagName('li'));
元素(定位器)。元素(定位器)
它将在父级中查找元素。
例子
看法
<div class = "parent">
<div class = "child">
Child text
<div>{{person.phone}}</div>
</div>
</div>
代码
// Calls Chain 2 element.
let child = element(by.css('.parent')).
element(by.css('.child'));
expect(child.getText()).toBe('Child text\n981-000-568');
// Calls Chain 3 element.
let triple = element(by.css('.parent')).
element(by.css('.child')).
element(by.binding('person.phone'));
expect(triple.getText()).toBe('981-000-568');
元素(定位器)。所有(选择器)
当对 $$ 的调用可能被链接时,它将在父级中找到一组元素。
例子
看法
<div class = "parent">
<ul>
<li class = "one">First</li>
<li class = "two">Second</li>
<li class = "three">Third</li>
</ul>
</div>
代码
let items = element(by.css('.parent')).$$('li'));
元素(定位器)。$(定位器)
当对 $ 的调用可能被链接时,它将在父级中查找元素。
例子
看法
<div class = "parent">
<div class = "child">
Child text
<div>{{person.phone}}</div>
</div>
</div>
代码
// Calls Chain 2 element.
let child = element(by.css('.parent')).
$('.child'));
expect(child.getText()).toBe('Child text\n981-000-568');
// Calls Chain 3 element.
let triple = element(by.css('.parent')).
$('.child')).
element(by.binding('person.phone'));
expect(triple.getText()).toBe('981-000-568');
元素(定位器)。 isPresent()
它将确定元素是否出现在页面上。
例子
看法
<span>{{person.name}}</span>
代码
expect(element(by.binding('person.name')).isPresent()).toBe(true);
// will check for the existence of element
expect(element(by.binding('notPresent')).isPresent()).toBe(false);
// will check for the non-existence of element
元素(定位器)。isElementPresent()
它与 element(locator).isPresent() 相同。唯一的区别是它将检查由 sublocator 标识的元素是否存在,而不是当前元素查找器。
element.all(locator).evaluate
顾名思义,它会评估输入是否在当前底层元素的范围内。
例子
看法
<span id = "foo">{{letiableInScope}}</span>
代码
let value = element(by.id('.foo')).evaluate('letiableInScope');
元素(定位器)。允许动画
顾名思义,它将确定当前底层元素是否允许动画。
例子
element(by.css('body')).allowAnimations(false);
元素(定位器)。等于
顾名思义,它将比较元素是否相等。
定位器(按)API
它基本上是元素定位器策略的集合,提供了通过绑定、模型等在 Angular 应用程序中查找元素的方法。
功能及其说明
ProtractorLocators API 的功能如下 –
by.addLocator(locatorName,fuctionOrScript)
它将向 ProtrcatorBy 的这个实例添加一个定位器,它可以进一步与 element(by.locatorName(args)) 一起使用。
例子
看法
<button ng-click = "doAddition()">Go!</button>
代码
// Adding the custom locator.
by.addLocator('buttonTextSimple', function(buttonText, opt_parentElement, opt_rootSelector) {
var using = opt_parentElement || document,
buttons = using.querySelectorAll('button');
return Array.prototype.filter.call(buttons, function(button) {
return button.textContent === buttonText;
});
});
element(by.buttonTextSimple('Go!')).click();// Using the custom locator.
通过绑定
顾名思义,它会通过文本绑定来查找元素。将完成部分匹配,以便返回绑定到包含输入字符串的变量的任何元素。
例子
看法
<span>{{person.name}}</span>
<span ng-bind = "person.email"></span>
代码
var span1 = element(by.binding('person.name'));
expect(span1.getText()).toBe('Foo');
var span2 = element(by.binding('person.email'));
expect(span2.getText()).toBe('[email protected]');
通过精确绑定
顾名思义,它将通过精确绑定来查找元素。
例子
看法
<spangt;{{ person.name }}</spangt;
<span ng-bind = "person-email"gt;</spangt;
<spangt;{{person_phone|uppercase}}</span>
代码
expect(element(by.exactBinding('person.name')).isPresent()).toBe(true);
expect(element(by.exactBinding('person-email')).isPresent()).toBe(true);
expect(element(by.exactBinding('person')).isPresent()).toBe(false);
expect(element(by.exactBinding('person_phone')).isPresent()).toBe(true);
expect(element(by.exactBinding('person_phone|uppercase')).isPresent()).toBe(true);
expect(element(by.exactBinding('phone')).isPresent()).toBe(false);
by.model(modelName)
顾名思义,它会通过 ng-model 表达式查找元素。
例子
看法
<input type = "text" ng-model = "person.name">
代码
var input = element(by.model('person.name'));
input.sendKeys('123');
expect(input.getAttribute('value')).toBe('Foo123');
by.buttonText
顾名思义,它会通过文本找到一个按钮。
例子
看法
<button>Save</button>
代码
element(by.buttonText('Save'));
by.partialButtonText
顾名思义,它会通过部分文本找到一个按钮。
例子
看法
<button>Save my file</button>
代码
element(by.partialButtonText('Save'));
中继器
顾名思义,它会在 ng-repeat 中找到一个元素。
例子
看法
<div ng-repeat = "cat in pets">
<span>{{cat.name}}</span>
<span>{{cat.age}}</span>
<</div>
<div class = "book-img" ng-repeat-start="book in library">
<span>{{$index}}</span>
</div>
<div class = "book-info" ng-repeat-end>
<h4>{{book.name}}</h4>
<p>{{book.blurb}}</p>
</div>
代码
var secondCat = element(by.repeater('cat in
pets').row(1)); // It will return the DIV for the second cat.
var firstCatName = element(by.repeater('cat in pets').
row(0).column('cat.name')); // It will return the SPAN for the first cat's name.
by.exactRepeater
顾名思义,它将通过精确的中继器查找元素。
例子
看法
<li ng-repeat = "person in peopleWithRedHair"></li> <li ng-repeat = "car in cars | orderBy:year"></li>
代码
expect(element(by.exactRepeater('person in
peopleWithRedHair')).isPresent())
.toBe(true);
expect(element(by.exactRepeater('person in
people')).isPresent()).toBe(false);
expect(element(by.exactRepeater('car in cars')).isPresent()).toBe(true);
by.cssContainingText
顾名思义,它将通过 CSS 查找包含精确字符串的元素
例子
看法
<ul> <li class = "pet">Dog</li> <li class = "pet">Cat</li> </ul>
代码
var dog = element(by.cssContainingText('.pet', 'Dog'));
// It will return the li for the dog, but not for the cat.
by.options(optionsDescriptor)
顾名思义,它会通过 ng-options 表达式查找元素。
例子
看法
<select ng-model = "color" ng-options = "c for c in colors"> <option value = "0" selected = "selected">red</option> <option value = "1">green</option> </select>
代码
var allOptions = element.all(by.options('c for c in colors'));
expect(allOptions.count()).toEqual(2);
var firstOption = allOptions.first();
expect(firstOption.getText()).toEqual('red');
by.deepCSS(选择器)
顾名思义,它会在 shadow DOM 中通过 CSS 选择器找到一个元素。
例子
看法
<div>
<span id = "outerspan">
<"shadow tree">
<span id = "span1"></span>
<"shadow tree">
<span id = "span2"></span>
</>
</>
</div>
代码
var spans = element.all(by.deepCss('span'));
expect(spans.count()).toEqual(3);