2016-03-08 56 views
0

我是一個新出生的QA,我在這裏試圖編寫一個量角器腳本來從下拉列表中選擇一個選項。在我的下拉菜單中有2個選項,我試圖從數字中選擇它。在量角器中選擇一個特定的選項

這是我正在使用的代碼。

var selectDropdownbyNum = function (element, optionNum) { 
    if (optionNum){ 
    var options = element.findElements(by.tagName('entity.company_id as entity.company_name for entity in entities')) 
     .then(function(options){ 
     options[1].click(); 
     }); 
    } 
    }; 

當頁面加載時默認選擇選項2。我需要的是從下拉菜單中選擇選項1。但是,我的代碼沒有這樣做。

以下是select選項的代碼片段。

<select required="required" class="form-control empty ng-pristine ng-valid ng-not-empty ng-valid-required ng-touched" name="entity_id" ng-model="invoice.entity_id" ng-options="entity.company_id as entity.company_name for entity in entities" ng-required="true" ng-change="entitySelect(invoice.entity_id)"> 

    <option value="string:568f97841a4885e5de39900e" label="Option Global">Option Global</option> 

    <option selected="selected" value="string:568f976a1a4885e5de39900d" label="Option Computer Studies">OptionComputer Studies</option> 

</select> 

感謝提前:)

+0

看看http://stackoverflow.com/questions/28723419/select-option-abstraction。很方便。 – alecxe

回答

1

找到完美的答案。

element.all(by.css('cssSelector of the dropdown')).each(function (eachElement, index) 
    { 
     eachElement.click();// select the <select> 
     browser.driver.sleep(500);// wait for the renderings to take effect 
     element(by.css('cssSelector of option want to select')).click();// select the first md-option 
     browser.driver.sleep(500);// wait for the renderings to take effect 
    }); 

這很像在平靜:)

+0

而不是'browser.driver.sleep(500)',你可以使用'browser.waitForAngular()'。 – Richh94

2

變種selectDropdownbyNum =函數(下拉,OPTIONNUMBER){

dropDown.click()。然後(函數(){ element.all(通過。 tagName('entity.company_id as entity.company_name for entity in entities'))。first()。click(); }); };

0

得到了完美的答案

VAR selectOption =元素(by.model( 「invoice.entity_id」))所有(by.tagName( '選項')); selectOption.get(1).click();

我們還可以通過各功能

selectOption.each(函數(項目,索引){

//Resolve the promise 
    item.getText().then(function(data){ 
     console.log(data+" "+index); 
    }); 

})獲得所有期權價值;

相關問題