2016-01-20 83 views
2

我在写一个Typescript/Protractor测试,需要同时评估和选择网格上的复选框元素。这些复选框缺少ID。因为我可以获得与这些复选框位于同一行的输入元素的句柄,所以我试图通过元素查找器(by.id,by.xpath和by.css)的组合来获取它们。但我的尝试失败了。如何使用量角器返回此复选框元素?

我想写的是一个小函数,它会根据行上的一个遥远的“兄弟”元素和该行上的复选框索引从行中返回目标复选框元素。我已经尝试了几件事,而我认为最有希望的尝试在这里发布。

这是我所针对的网格中的一部分标记。为了清楚起见,省略了几列信息。这就是我所争辩的。我没有权力改变标记,我只被要求写出量角器测试。

<div ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index" class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)"> 
    <div ui-grid-row="row" row-render-index="rowRenderIndex" class="ng-isolate-scope"> 
     <div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" class="ui-grid-cell ng-scope ui-grid-coluiGrid-09T" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" ui-grid-cell=""> 
      <div ng-click="grid.appScope.rowColumnSelect(row, 'rating')" ng-class="{'px-grid-row-selected': grid.appScope.highlightCell(row, 'rating'), 'px-can-edit': grid.appScope.clickableCell(row.entity, 'rating'), 'px-grid-total-row': row.entity.isTotal, 'px-grid-notable-row': row.entity.isNotable}" class="ng-scope"> 
       <div class="ui-grid-cell-contents ng-hide" ng-hide="grid.appScope.showCell(row, 'rating')"> 
       </div> 
       <div ng-show="grid.appScope.showCell(row, 'rating')" name="loanLossAssumption3" class="ui-grid-cell-contents"> 
        <div ng-if="grid.appScope.haveData(row)" class="text-right ng-scope"> 
         <div error-display-strategy="grid" px-input-field="" id="loanLossAssumptionrating3" name="loanLossAssumptionrating3" ng-if="grid.appScope.inEditMode(row, 'rating')" input-type="number" px-input-required="true" px-round-value="0" ng-model="row.entity.rating" class="ng-pristine ng-untouched ng-valid ng-scope ng-isolate-scope"> 
          <div ng-class="{'row': !vm.isGridErrorDisplay &amp;&amp; !vm.autoWidth}"> 
           <div> 
            <div tooltip="" tooltip-placement="bottom" tooltip-append-to-body="true" tooltip-trigger="open" class="ng-scope"> 
             <div px-validation="loanLossAssumptionrating3InputField" ng-class="{'col-xs-12': !vm.isGridErrorDisplay &amp;&amp; !vm.autoWidth}" min="" max="" input-type="number" maxlength="" minlength="" error-display-strategy="grid" px-tooltip="" class="ng-isolate-scope"> 
              <div class="row"> 
               <div ng-class="vm.inputFieldClass()" class="col-xs-12"> 
                <div class="input-group px-input-group-overrides px-no-grid-padding" ng-hide="vm.pxInputDisabled"> 
                 <span id="loanLossAssumptionrating3Prefix" name="loanLossAssumptionrating3Prefix" class="input-group-addon input-left-addon ng-binding ng-hide" ng-show="vm.prefix" ng-class="{'px-auto-width': vm.autoWidth}"> 

                 </span> 
                 <div class="inner-addon"> 
                  <input id="loanLossAssumptionrating3InputField" name="loanLossAssumptionrating3InputField" class="form-control ng-pristine ng-untouched ng-valid ng-valid-px-email ng-valid-required ng-valid-unique" ng-class="{'px-input-with-tooltip': !!vm.pxTooltip, 'px-auto-width': vm.autoWidth}" ng-required="vm.pxInputRequired" px-round-value="0" px-edit-rate="false" type="text" ng-model="vm.ngModel" px-email-validator="" ng-disabled="vm.pxControlDisabled" placeholder="" ng-change="vm.onChange()" ng-focus="vm.showTooltip(true)" ng-blur="vm.showTooltip(false)" px-unique-field="" autocomplete="off" required="required"> 
                 </div> 

                 <span class="fa fa-exclamation-circle inline-error form-control-feedback" ng-style="{left:vm.feedbackIconLeft()}" style="left: 0px;"> 
                 </span> 


                 <span class="input-group-addon input-empty-addon" ng-hide="vm.postfix" ng-class="{'px-auto-width': vm.autoWidth}"> 
                 </span> 
                </div> 

               </div> 
               <span ng-transclude="" class="px-vertical-middle"> 
               </span> 
              </div> 
             </div> 
            </div> 
           </div> 
           <div px-tooltip-view="" px-tooltip="vm.pxTooltip" ng-click="vm.applyAdjustment()" class="ng-isolate-scope"> 
           </div> 
          </div> 
         </div> 
         <span ng-hide="grid.appScope.inEditMode(row, 'rating')" ng-class="{'px-font-highlighted': grid.appScope.showHighlighted(row, 'rating'), 'px-font-enabled': grid.appScope.showEnabled(row, 'rating'), 'px-font-disabled': grid.appScope.showDisabled(row, 'rating'), 'px-cannot-edit': !grid.appScope.clickableCell(row.entity, 'rating')}" class="ng-binding px-font-enabled px-cannot-edit ng-hide">3</span> 
        </div> 
       </div> 
      </div> 
     </div> 
     <div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" 
       class="ui-grid-cell ng-scope ui-grid-coluiGrid-09Y" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" ui-grid-cell=""> 
      <div ng-click="grid.appScope.rowColumnSelect(row, 'isDefault')" 
        ng-class="{'px-grid-row-selected': grid.appScope.highlightCell(row, 'isDefault'), 'px-can-edit': grid.appScope.clickableCell(row.entity, 'isDefault'), 'px-grid-total-row': row.entity.isTotal, 'px-grid-notable-row': row.entity.isNotable}" 
        class="ng-scope"> 
       <div class="ui-grid-cell-contents ng-hide" ng-hide="grid.appScope.showCell(row, 'isDefault')"> 
       </div> 
       <div ng-show="grid.appScope.showCell(row, 'isDefault')" name="loanLossAssumptionfalse" class="ui-grid-cell-contents"> <!-- Changes to loanLossAssumptiontrue--> 
        <div ng-if="grid.appScope.haveData(row)" class="text-center ng-scope"> 
         <input ng-disabled="!grid.appScope.inEditMode(row, 'isDefault')" type="checkbox" ng-input="row.entity.isDefault" ng-model="row.entity.isDefault" 
           class="ng-pristine ng-valid ng-touched"> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

在这种情况下,我有一个输入字段的句柄,其ID为loanLossAssumptionrating3InputField。从这里,我需要从这个ui-grid-row获得其他复选框。在这个例子中,一个复选框出现在标记末尾附近,在6个div div标签之前。

随着时间的推移,我们构建了一个函数库,所以引用了“lib”。在这个代码中。这只是我们图书馆的工作参考。这段代码位于pageObject中。

lib.getCheckboxInRow(fields.ratingFieldId, 0).then(function (checkboxElement) { 
    // The fields.ratingFieldId value = "loanLossAssumptionrating3InputField" 
    checkboxElement.click(); 
}); 

这仅仅是一个“点击”的尝试,但我还需要获得该元素并确定是否被点击的复选框。在我的情况下,通过标记做到这一点的唯一方法是读取父元素中的属性。我知道该怎么做,但我提供这些细节来说明为什么我试图返回元素本身。

这是我的目标函数。下面显示了两次尝试。这两个都失败了。这是在我的图书馆。

// Attempt A 
public getCheckboxInRow(siblingElementId: string, checkboxIndex: number): webdriver.promise.Promise<protractor.ElementFinder> { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).then(function (row) { 
     return row.all(by.css("input[@type='checkbox']")).filter.then(function (checkboxArray) { 
      return checkboxArray[checkboxIndex]; 
     }); 
    }); 
} 

// Attempt B 
public getCheckboxInRow(siblingElementId: string, checkboxIndex: number): webdriver.promise.Promise<protractor.ElementFinder> { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    var cssToCheckbox : string = "input[@type='checkbox']"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).all(by.css(cssToCheckbox)).filter(function(checkbox) { 
     return true; 
    }).then(function (checkboxArray) { 
     return checkboxArray[checkboxIndex]; 
    }); 
} 

对于“尝试A”,会发生以下错误。

TypeError: element(...).element(...).then is not a function

“尝试B”的错误更加剧烈。

InvalidSelectorError: invalid selector: An invalid or illegal selector was specified (Session info: chrome=47.0.2526.111) (Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 30 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16' System info: host: 'PSIDEVKPALM', ip: '10.226.128.27', os.name: 'Windows Server 2012 R2', os.arch: 'x86', os.version: '6.3', java.version: '1.8.0_66' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\kpalmer\AppData\Local\Temp\2\scoped_dir7352_8503}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=47.0.2526.111, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 12959162beeed374185734ef29b03b57 *** Element info: {Using=css selector, value=input[@type='checkbox']}

花了很多时间在这方面的工作,我对这些失败的原因感到茫然。

最终我想要的是一个函数,该函数将根据复选框索引返回与该行内另一个元素位于同一行的复选框元素,以便我们可以单击复选框或确定该元素被选中或未选中。

感谢您的帮助。

===编辑2016年1月2日下午===

谢谢@alecxe,你是一个巨大的帮助。以下是我使用的最终实现。这两种方法都有效。

其中任何一个都可以在我的pageObject中工作。

lib.getCheckboxInRow(fields.ratingFieldId, 0).click(); 

lib.getCheckboxArrayInRow(fields.ratingFieldId).filter(function (checkboxArray, index) { 
    return (index === 0); 
}).first().click(); 

而这些是库中的目标函数。我必须调整的一件事是返回类型,从承诺到元素查找器。这些工作就像一个冠军。

public getCheckboxInRow(siblingElementId: string, checkboxIndex: number): protractor.ElementFinder { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).all(by.css("input[type=checkbox]")).filter(function (checkboxArray, index) { 
     return index === checkboxIndex; 
    }).first(); 
}; 

public getCheckboxArrayInRow(siblingElementId: string): protractor.ElementArrayFinder { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).all(by.css("input[type=checkbox]")); 
}; 

回答

2

让我们来回顾一下您的尝试。

  • Attempt A

你所得到的TypeError: element(...).element(...).then is not a function错误,因为你不能then()解析ElementFinder因为量角器2.0.0(changelog)。

只要继续在这种情况下链接elementall()

public getCheckboxInRow(siblingElementId: string, checkboxIndex: number): webdriver.promise.Promise<protractor.ElementFinder> { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).all(by.css("input[type=checkbox]")).filter(function (checkboxArray, index) { 
     return index === checkboxIndex; 
    }).first(); 
} 

我也是固定的方式在应用filter()功能。并注意first()调用 - 这将帮助我们获得单个过滤元素而不是数组。

  • Attempt B

你所得到的An invalid or illegal selector was specified错误,因为你确实有一个无效的CSS选择器input[@type='checkbox'] - 你不需要预先考虑@在CSS中属性名称 - 与input[type=checkbox]更换(无需对于单引号也是如此)。

而且,您也不会在此尝试中正确应用filter()函数。修正版本:

public getCheckboxInRow(siblingElementId: string, checkboxIndex: number): webdriver.promise.Promise<protractor.ElementFinder> { 
    var xPathToRow : string = "./ancestor::div[contains(concat(' ', @class, ' '), ' ui-grid-row ')][1]"; 
    var cssToCheckbox : string = "input[type=checkbox]"; 
    return element(by.id(siblingElementId)).element(by.xpath(xPathToRow)).all(by.css(cssToCheckbox)).filter(function(checkbox, index) { 
     return checkboxIndex === index; 
    }).first(); 
} 

这基本上成为为尝试A.同一

+0

哇!希望我昨天会问。感谢@alecxe。我现在就给这个镜头。 –

+0

这更接近该商标。我需要现在修复我对函数的调用,因为出现以下错误:TypeError:lib.getCheckboxInRow(...)。然后不是函数。 –

相关问题