2017-05-10 1 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 메서드를 사용하여 부분 텍스트의 인덱스 번호를 찾습니다. –