Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Protractor 如何为通过“element.all”找到的对象保存定位器?_Protractor - Fatal编程技术网

Protractor 如何为通过“element.all”找到的对象保存定位器?

Protractor 如何为通过“element.all”找到的对象保存定位器?,protractor,Protractor,我有一些代码可以找到页面中的所有“模式内容”div,并为找到的每个元素创建一些modalwrapper对象: var deferred = protractor.promise.defer(); element.all(by.className('modal-content')).then(function(modals) { var wrappedModals = []; modals.forEach(function(webElementPromise) { // the

我有一些代码可以找到页面中的所有“模式内容”div,并为找到的每个元素创建一些
modal
wrapper对象:

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()
是相同的(只是“模式内容”)。这些对象是否还有其他属性可以通过编程进行计算(保存一些东西以便我以后可以让量角器查找对象?)

注意,如果我只保存
webElementPromise
本身,我会得到一个
StaleElementReferenceError:在对象消失的情况下,元素不再附加到DOM
(这是有意义的,因为DOM已经改变了很多)。(我担心引用是否会因为其他原因而过时,因此我不确定是否应该依赖此异常来测试元素是否隐藏。)

您不需要“标识符来稍后再次查找元素”。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();
});

太好了,谢谢!ExpectedConditions很好地简化了事情。使用这种方法,我仍然会遇到零星的
StaleElementReferenceError
异常。我不确定重用元素承诺的形式是否不好,或者量角器是否应该更好地隐藏这个异常。我有一个测试用例,我将在github上归档。