2016-06-28 165 views
1

在應用程序構建教程angularjs.org一步8,測試部分,什麼是代碼的均值AngularJS - scenario.js代碼

element.all(by.css('.phones li a')).first().click(); 
expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s'); 

感謝以下行提前!

PS: 的準確URL是 - https://docs.angularjs.org/tutorial/step_08和代碼文件(scenarios.js)是 -

'use strict'; 

// Angular E2E Testing Guide: 
// https://docs.angularjs.org/guide/e2e-testing 

describe('PhoneCat Application', function() { 

    describe('phoneList', function() { 

    beforeEach(function() { 
     browser.get('index.html'); 
    }); 

    it('should filter the phone list as a user types into the search box', function() { 
     var phoneList = element.all(by.repeater('phone in $ctrl.phones')); 
     var query = element(by.model('$ctrl.query')); 

     expect(phoneList.count()).toBe(20); 

     query.sendKeys('nexus'); 
     expect(phoneList.count()).toBe(1); 

     query.clear(); 
     query.sendKeys('motorola'); 
     expect(phoneList.count()).toBe(8); 
    }); 

    it('should be possible to control phone order via the drop-down menu', function() { 
     var queryField = element(by.model('$ctrl.query')); 
     var orderSelect = element(by.model('$ctrl.orderProp')); 
     var nameOption = orderSelect.element(by.css('option[value="name"]')); 
     var phoneNameColumn = element.all(by.repeater('phone in $ctrl.phones').column('phone.name')); 

     function getNames() { 
     return phoneNameColumn.map(function(elem) { 
      return elem.getText(); 
     }); 
     } 

     queryField.sendKeys('tablet'); // Let's narrow the dataset to make the assertions shorter 

     expect(getNames()).toEqual([ 
     'Motorola XOOM\u2122 with Wi-Fi', 
     'MOTOROLA XOOM\u2122' 
     ]); 

     nameOption.click(); 

     expect(getNames()).toEqual([ 
     'MOTOROLA XOOM\u2122', 
     'Motorola XOOM\u2122 with Wi-Fi' 
     ]); 
    }); 

    it('should render phone specific links', function() { 
     var query = element(by.model('$ctrl.query')); 
     query.sendKeys('nexus'); 

     element.all(by.css('.phones li a')).first().click(); 
     expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s'); 
    }); 

    }); 

}); 

回答

1

它是路由到/phones/nexus-s的測試。 它寫在Protractor

第一行讀取DOM並查找所有.phones li a css規則。然後它只需要第一個,並呼叫click()就可以了。

element.all(by.css('.phones li a')).first().click(); 

第二行預期功能browser.getLocationAbsUrl()的輸出爲字符串/phone/nexus-s

expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s'); 

因此,所有的所有的測試框架,點擊一個按鈕,並預計其路由到一個新的一頁。

+0

謝謝,你的回答很有幫助。 – HardikT

+0

沒問題@Trivedi。如果你喜歡它,你也可以「回覆」這個答案。 – eikooc

+0

當然,但是我這樣做的聲望會下降1。 也許你可以幫助我;) – HardikT