如何为通过“element.all”找到的对象保存定位器?
How to save a locator for objects found via `element.all`?
我有一些代码可以找到页面中的所有 "modal-content" div,并为它找到的每个元素创建一些 Modal
包装器对象:
var deferred = protractor.promise.defer();
element.all(by.className('modal-content')).then(function(modals) {
var wrappedModals = [];
modals.forEach(function(webElementPromise) {
// the "modal-content" should only be findable for modals
// that are visible:
expect(webElementPromise.isPresent()).toBe(true);
expect(webElementPromise.isDisplayed()).toBe(true);
wrappedModals.push(new Modal(webElementPromise.locator()));
console.log(webElementPromise.locator());
});
deferred.fulfill(wrappedModals);
});
问题是我想检查这些元素稍后是否已经消失(在页面上进行一些更改之后)。但是,每个元素的 .locator()
是相同的(只是 "modal-content")。我可以通过编程计算这些对象的其他一些属性吗(要保存的东西,以便我稍后可以让 Protractor 找到对象?)
请注意,如果我只保存 webElementPromise
本身,我会在对象消失的情况下得到 StaleElementReferenceError: Element is no longer attached to the DOM
(这是有道理的,因为 DOM 已经完全改变了一点点)。 (我很担心引用是否会因为其他原因而过时,所以我不确定是否应该依赖此异常来测试元素是否隐藏。)
您不需要 "an identifier to find the elements later again"。您的 elementFinder/elementArrayFinder 是您对元素的引用。
var EC = protractor.ExpectedConditions;
var myElems = element.all(by.className('modal-content'));
expect(myElems.count()).toBeGreaterThan(0);
myElems.each(function(elem) {
// assert that they're all present and visible
expect(EC.visibilityOf(elem)()).toBeTruthy();
});
// now you do whatever actions that make your elements disappear.
myElems.each(function(elem) {
// assert that they're all invisible (either not present or not visible)
expect(EC.visibilityOf(elem)()).toBeFalsy();
});
我有一些代码可以找到页面中的所有 "modal-content" div,并为它找到的每个元素创建一些 Modal
包装器对象:
var deferred = protractor.promise.defer();
element.all(by.className('modal-content')).then(function(modals) {
var wrappedModals = [];
modals.forEach(function(webElementPromise) {
// the "modal-content" should only be findable for modals
// that are visible:
expect(webElementPromise.isPresent()).toBe(true);
expect(webElementPromise.isDisplayed()).toBe(true);
wrappedModals.push(new Modal(webElementPromise.locator()));
console.log(webElementPromise.locator());
});
deferred.fulfill(wrappedModals);
});
问题是我想检查这些元素稍后是否已经消失(在页面上进行一些更改之后)。但是,每个元素的 .locator()
是相同的(只是 "modal-content")。我可以通过编程计算这些对象的其他一些属性吗(要保存的东西,以便我稍后可以让 Protractor 找到对象?)
请注意,如果我只保存 webElementPromise
本身,我会在对象消失的情况下得到 StaleElementReferenceError: Element is no longer attached to the DOM
(这是有道理的,因为 DOM 已经完全改变了一点点)。 (我很担心引用是否会因为其他原因而过时,所以我不确定是否应该依赖此异常来测试元素是否隐藏。)
您不需要 "an identifier to find the elements later again"。您的 elementFinder/elementArrayFinder 是您对元素的引用。
var EC = protractor.ExpectedConditions;
var myElems = element.all(by.className('modal-content'));
expect(myElems.count()).toBeGreaterThan(0);
myElems.each(function(elem) {
// assert that they're all present and visible
expect(EC.visibilityOf(elem)()).toBeTruthy();
});
// now you do whatever actions that make your elements disappear.
myElems.each(function(elem) {
// assert that they're all invisible (either not present or not visible)
expect(EC.visibilityOf(elem)()).toBeFalsy();
});