2016-06-28 152 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