无法显示转发器元素

时间:2014-02-14 17:25:30

标签: javascript angularjs selenium-webdriver protractor

使用量角器我正在测试Angular JS应用程序:

在自动导航部件触发搜索请求后,我的应用程序中有一个搜索功能。我试图用以下代码提取我的搜索结果:

element.all(by.repeater('object in objects')).then(function (arr){
    for (var i=0; i<arr.length;i++){
        arr[i].getText().then(function(text){
            console.log('item:'+i+text);
        });

这是我的控制台输出:

Using the selenium server at http://localhost:4444/wd/hub
item:20TSK(ACE700J)(000)(ACE700JU00)(000) upd tsk label
item:20TSK(BEN705JU39)(000)
item:20TSK(IAM710JUW5)(000)
item:20TSK(CDF010JD00)(000)
item:20TSK(IAL755JFZZ)(000)
item:20TSK(IAQ781JU70)(000)
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
item:20
.
Finished in 13.893 seconds
1 test, 0 assertions, 0 failures

所以实际上我面临两个问题:

  1. 为什么'我'总是20?
  2. 为什么我只能提取20个项目中的6个,而我可以看到 这20个项目在屏幕上显示得很好?
  3. 以下是我的搜索结果的结构:

    <div id="mainViewNav">
    <div id="sortByBand">
        <div id="jobHolder" class="thumbJobs" style="height: 63px; overflow: hidden;" tabindex="5017">
            <div id="job_1" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                <div id="job_1_viewerEye" class="viewerEye" ng-click="viewerEye($event,$index+1, object)" ng-class="object.viewerEyeClass"></div>
                <div id="job_1_jobBigPicto" class="jobBigPicto transition_2_opa">
                    <img class="jobBigImg" title="07" onerror="this.src = 'images/icons/search_results/application_types/application_default.png'" alt="" ng-src="images/icons/search_results/application_types/application_default.png" src="images/icons/search_results/application_types/application_default.png">
                    <img class="jobLaunchMethod" title="Scheduled" alt="" ng-src="images/icons/search_results/task_types/scheduled.png" src="images/icons/search_results/task_types/scheduled.png">
                    <div class="clear"></div>
                </div>
                <div id="job_1_jobsSmallPictos" class="jobsSmallPictos transition_2_opa">
                    <img class="jobsSmallImg ng-scope" ng-repeat="jobType in object.uprocTypes" title="CMD" alt="" ng-src="images/icons/search_results/uproc_types/CMD.png" src="images/icons/search_results/uproc_types/CMD.png">
                </div>
                <div id="job_1_jobDown" class="jobDown transition_2_opa">
                    <div class="jobTitle ng-binding">TSK(ACE700J)(000)(ACE700JU00)(000)</div>
                    <div class="jobText ng-binding">upd tsk label</div>
                </div>
                <div id="job_1_jobHoverInfo" class="jobHoverInfo" ng-click="hideJobHoverInfo($event,$index+1)">
                    <div class="jobInfoNameBlock">
                        <div class="jobInfoContextBlock">
                            <div class="jobDetailsBtn ng-binding" ng-click="viewWorkflow(object)">View details...</div>
                        </div>
                    </div>
                    <div id="job_2" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                        <div id="job_2_viewerEye" class="viewerEye" ng-click="viewerEye($event,$index+1, object)" ng-class="object.viewerEyeClass"></div>
                        <div id="job_2_jobBigPicto" class="jobBigPicto transition_2_opa">
                            <div id="job_2_jobsSmallPictos" class="jobsSmallPictos transition_2_opa">
                                <div id="job_2_jobDown" class="jobDown transition_2_opa">
                                    <div id="job_2_jobHoverInfo" class="jobHoverInfo" ng-click="hideJobHoverInfo($event,$index+1)">
                                    </div>
                                    <div id="job_3" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                        <div id="job_3_viewerEye" class="viewerEye" ng-click="viewerEye($event,$index+1, object)" ng-class="object.viewerEyeClass"></div>
                                        <div id="job_3_jobBigPicto" class="jobBigPicto transition_2_opa">
                                            <div id="job_3_jobsSmallPictos" class="jobsSmallPictos transition_2_opa">
                                                <div id="job_3_jobDown" class="jobDown transition_2_opa">
                                                    <div id="job_3_jobHoverInfo" class="jobHoverInfo" ng-click="hideJobHoverInfo($event,$index+1)">
                                                    </div>
                                                    <div id="job_4" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                        <div id="job_5" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                            <div id="job_6" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                <div id="job_7" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                    <div id="job_8" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                        <div id="job_9" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                            <div id="job_10" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                <div id="job_11" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                    <div id="job_12" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                        <div id="job_13" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                            <div id="job_14" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                <div id="job_15" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                    <div id="job_16" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                        <div id="job_17" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                            <div id="job_18" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                                <div id="job_19" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                                    <div id="job_20" class="job ng-scope" ng-click="showJobHoverInfo($index+1)" ng-repeat="object in objects" ng-class="object.jobStyle">
                                                                                                                    </div>
                                                                                                                    </section>
    

    感谢您的回答我已经下载了异步库我的代码现在看起来像这样:

    describe('search', function () {
    
    var ptor;
    var Q = require("C:/node_modules/protractor/node_modules/q");
    var async = require("C:/node_modules/protractor/node_modules/async");
    beforeEach(function ()
    
        {
            ptor = protractor.getInstance();
            ptor.get('http://frstmwarwebsrv2.orsyptst.com:9000/');
            //Delete cookies to avoid saving password or username
            ptor.manage().deleteAllCookies();
        });
    
    it('extract data', function ()
    
        {
            element(by.id('login_form_user_input')).sendKeys('user_001_X');
            element(by.id('login_form_password_input')).sendKeys('admin');
            element(by.id('login_form_signin_button')).click()
            element(by.id('mainGlobalSearch')).sendKeys('tsk');
            element(by.id('mainGlobalSearchBtn')).click()
    
            element.all(by.repeater('object in objects')).then(function (arr)
    
    
                {
    
                    var resultArray = [];
                    async.eachSeries(arr, function (element)
    
                        {
                            element.getText().then(function (text, cb)
    
                                {
                                    resultArray.push(text);
                                    cb(null);
                                });
                        },
    
                        function (err)
    
                        {
                            console.log(resultArray.join(' '));
                            // continue testing
                        });
                });
    
        });
    
    
    
     });
    

    但是我收到了这个错误:

    Failures:
    
     1) search extract data
     Message:
     TypeError: undefined is not a function
     Stacktrace:
     TypeError: undefined is not a function
     at C:\Users\orsyp\DUX\k_workload_ar\ui\e2e\searchbyall.spec.js:38:21
    at C:\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\base.
    
    Finished in 11.534 seconds
    1 test, 1 assertion, 1 failure
    

1 个答案:

答案 0 :(得分:0)

你的问题是getText是异步的。

最快的解决方法是使用async eachSeries

有关如何在案例中使用eachSeries的快速未经测试的示例:

var resultArray = [];
async.eachSeries(arr, function(element) {
  element.getText().then(function (text, cb) {
    resultArray.push(text);
    cb(null);
  });
}, function (err) {
  console.log(resultArray.join(' ');
  // continue testing
});