2017-05-10 4 views
0

配列の内容をグローバル定義の空の配列&にプッシュしようとしていて、その内容を別の関数の中で取得しようとしています。分度器 - 関数内の大域配列の値を取得する

以下

私が試したコードです:

describe('My Test', function() { 
var arrayf3=[]; 
var indexf3='not found'; 
    it('Test starts', function() { 
    browser.ignoreSynchronization = true; 
    browser.get('https://www.w3schools.com/angular/'); 
    var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]')); 
    elm.count().then(function(count) { 
     Methods.pushToArray(0, count, elm); 
    }) 
    var texttocheck='Data Binding'; 
    Methods.getIndex(0, arrayf3.length, arrayf3, texttocheck); 
    console.log('Text content of global array is ' + arrayf3); 
    console.log('index of the array number having texttofind is ' + indexf3); 
    }) 
    var Methods = { 
    getIndex :function (i, max, array, texttocheck) { 
     if (i < max) { 
     console.log('text[' + i + '].indexOf = ' + array[i].indexOf(texttocheck)) 
     if (array[i].indexOf(texttocheck) > 0) { 
      indexf3 = i; 
     } else { 
      Methods.getIndex(i + 1, max, array, texttocheck); 
     } 
     } 
    }, 

    pushToArray :function (i, max, elm) { 
     if (i < max) { 
     elm.get(i).getText().then(function(tmpText) { 
      console.log("The array "+tmpText); 
      arrayf3.push(tmpText);  
     }) 
     Methods.pushToArray(i + 1, max, elm); 
     } 

    }, 

    } 
}); 

問題は、私はプレースホルダの値以下の場合はnull値を取得しています:グローバル配列の

テキストコンテンツは、配列番号の

指標でありますtexttofindを持っているのは

このグローバルな空の配列にコピーされた配列の値を&に表示したい同じそれをブロックする機能 'テスト開始'

答えて

1

分度器のelement.allは本質的に各要素のgetText()を知っていて、値を配列として返します。

it('Test starts', function() { 
     browser.ignoreSynchronization = true; 
     browser.get('https://www.w3schools.com/angular/'); 

     var getIndexOfElementByPartialText = function(inputText) { 
      return element(by.id('leftmenuinner')).all(by.css('[target="_top"]')).getText().then(function(values) { 
       var indexNumber; 
       values.forEach(function(value, index) { 
        if (new RegExp(inputText).test(value)) { 
         if (indexNumber === undefined) { 
          indexNumber = index; 
         } else { 
          throw new Error('multiple elements match the input text'); 
         } 
        } 
       }); 
       if (indexNumber === undefined) { 
        throw new Error('no elements match the input text'); 
       } else { 
        return indexNumber; 
       } 
      }); 
     }); 

     expect(getIndexOfElementByPartialText('thing1').toBe(1); 
     expect(getIndexOfElementByPartialText('thing2').toBe(2); 
    }); 

再利用可能な機能に関して回答を編集しました。

+0

これは私が探しているものではありません。それが何を意図しているのか、私が実際に望んでいることを理解するために、質問とコードを再度読んでください。あなたが投稿した答えは、配列の値を出力します。配列の内容を空の配列(グローバルに定義された配列)にプッシュし、indexOfメソッドを使って部分テキストのインデックス番号を探します。 –

関連する問題