2016-09-23 8 views
1

필자는 함수 내에 넣고 싶은 코드와 동일한 비트를 반복하는 여러 개의 각도기/각도 요소를 차단했습니다. 이 함수를 반복해서 반복하지 말고 그냥 호출하고 싶습니다.중첩 된 함수에서 javascript에 변수를 전달하는 방법

it('should move directly to Draft', function() { 
    posting_sum_page.locate_action_button.click(); 
    posting_action_page.move_action.filter(function(elem) { 
    return elem.getText().then(function(text) { 
     return text === 'Draft'; 
    }); 
    }).click(); 
}); 

이 부분은 기능을 만들고 싶은 반복 부분입니다. 나는 자바 스크립트를 처음 사용하므로이 작업을 수행하는 방법을 알지 못한다.

return elem.getText().then(function(text) { 
     return text === 'Draft'; 
    }); 
    }).click(); 

다른 변수로 '초안'을 대체 할 수 있어야합니다. 이 부분에 대한 페이지 개체를 사용하고 있고 잘 모르겠습니다)이 같은 함수를 만들고 내 텍스트를 전달하는 방법 & B) 사양면 또는 페이지면에 가야하는 경우? 이것은 아마 대부분의 사람들에게 기본입니다. 그러나 나는 자바 스크립트에 익숙하지 않기 때문에이 문제에 대해 머리를 감싸는 데 어려움을 겪고있다.

답변

0

내가 전체 필터 기능를 추출하는 것 "도우미"모듈에 복귀 블록을 다시 사용합니다.

helpers.js : 테스트에서

var Helpers = function() { 
    this.filterByText = function (text) { 
     return function (elem) { 
      return elem.getText().then(function(actualText) { 
       return actualText === text; 
      }); 
     }; 
    } 
} 

module.exports = new Helpers(); 

사용법 :

var helpers = require("helpers"); 

describe("My Test", function() { 
    it('should move directly to Draft', function() { 
     posting_sum_page.locate_action_button.click(); 
     posting_action_page.move_action.filter(helpers.filterByText('Draft')).click(); 
    }); 
}); 
+0

누구나 입력 해 주셔서 감사합니다. 귀하의 솔루션이 가장 효과적이었습니다. 모든 사람들에게 높이 평가되었습니다. –

0

어쩌면 이렇게 될까요?

describe('...something...', function() 
{ 
    var clickBtn; 


    beforeEach(function() 
    { 
    clickBtn = function(testText) 
    { 
     return posting_action_page.move_action.filter(function(elem) 
     { 
     return elem.getText().then(function(currentText) 
     { 
      return currentText === testText; 
     }); 
     }).click(); 
    }; 
    }); 


    it('should move directly to Draft', function() 
    { 
    posting_sum_page.locate_action_button.click(); 
    expect(clickBtn('Draft')).toEqual('...something...'); 
    }); 
}); 
0

it('should move directly to' + targetText, function() { 
    posting_sum_page.locate_action_button.click(); 
    posting_action_page.move_action.filter(function(elem) { 
    checkSameText(elem, targetText); 
    }).click(); 
}); 

function checkSameText(el, targetText) { 
    return el.getText().then(function(text) { 
     return text === targetText; 
    }); 
} 
0

내가 확실 해요 어떤 페이지 오브젝트 유형이 posting_action_page.move_action입니다하지만 난 당신이 by.buttonText를 사용 찾고있는 생각 또는 by.linkText.

// html: <button>Draft</button> 
element(by.buttonText('Draft')).click(); 

// html: <a href="">Draft</button> 
element(by.linkText('Draft')).click(); 

by.partialButtonTextby.partialLinkText처럼 도움이 될 수있는 다른 로케이터가 있습니다.

관련 문제