通常在量角器中,您可以选择奇异元素:
element(protractor.By.css('#fdfdf'));
Run Code Online (Sandbox Code Playgroud)
偶尔你会得到这样的东西:
element(protractor.By.css('.dfdf'));
Run Code Online (Sandbox Code Playgroud)
可能有多个元素.从定位器中选择一个定位多个元素的索引的正确方法是什么,并且仍然包含量角器发送密钥的方法?
我一直在尝试AngularJS e2e测试,并且确定是否选中了复选框.
我用端到端测试复选框输入作为样品(见端到端测试中的标签实施例).
Html代码段:
Value1: <input type="checkbox" ng-model="value1"> <br/>
控制器片段:
function Ctrl($scope) {
$scope.value1 = true;
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
1) expect(binding('value1')).toEqual('true');
只要value1在屏幕上显示,这适用于样本端到端测试{{value1}}.如果在本地测试并删除`{{value1}},绑定测试将失败.在我的大多数现实世界的例子中,我没有在屏幕上的任何地方显示复选框值.
2) expect(input('value1').val()).toEqual('true');
该值将始终默认为on与复选框是否处于已检查状态无关(取自此帖子).
注意:看来Angular E2E测试将来会被Protractor取代(参见文档)
我试图寻找类似的质量保证,但我找不到满足我的.所以基本上我在一些例子中看到它被使用了
ptor = protractor.getInstance();
ptor.get(url);
Run Code Online (Sandbox Code Playgroud)
在其他一些例子中,它被使用了.
browser.get(url);
Run Code Online (Sandbox Code Playgroud)
所以问题是:使用量角器实例和浏览器获取特定网址有什么区别?另外,如果我在PS中的假设是正确的,那么更好的做法是:仅使用量角器,还是将它们混合使用?
PS我也看到了调试器使用方面的相同差异.我知道量角器是Web驱动程序的包装器,我认为protractor.getInstance().get(url)是browser.get(url)的隐式invocatin.
describe('my homepage', function() {
var ptor = protractor.getInstance();
beforeEach(function(){
// ptor.ignoreSynchronization = true;
ptor.get('http://localhost/myApp/home.html');
// ptor.sleep(5000);
})
describe('login', function(){
var email = element.all(protractor.By.id('email'))
, pass = ptor.findElement(protractor.By.id('password'))
, loginBtn = ptor.findElement(protractor.By.css('#login button'))
;
it('should input and login', function(){
// email.then(function(obj){
// console.log('email', obj)
// })
email.sendKeys('josephine@hotmail.com');
pass.sendKeys('shakalakabam');
loginBtn.click();
})
})
});
Run Code Online (Sandbox Code Playgroud)
上面的代码返回
Error: Error while waiting for Protractor to sync with the page: {}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会这样,ptor正确加载页面,它似乎是选择失败的元素.
到SSHMSH:
谢谢,你几乎是正确的,并给了我正确的理念,所以关键是ptor.sleep(3000)让每个页面等待直到ptor与项目同步.
在量角器2.0中,我正在检查expect()是否显示了一个元素.我希望是假的,但奇怪的是我得到以下错误:
NoSuchElementError:找不到使用locator找到的元素:By.id("userForm")
我的代码是:
describe('closeModal', function() {
it('should close the alert that appears after registration.', function(){
element(by.id('closeAlertModalButton')).click();
expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
});
});
Run Code Online (Sandbox Code Playgroud)
我明白我得到了那个错误,因为元素不再在页面上(我想要确认),但是我不应该得到错误而不是错误?
我有一些使用protractor for angular.js应用程序编写的测试.我正在使用Page Objects设计模式,我有一些方法可以通过单击链接和按钮导航到其他页面.不久之后我就打电话了browser.waitForAngular().
页面对象
module.exports = function () {
this.companyNameLink = element(by.id('viewCompany'));
this.newMeetingButton = element(by.id('newMeetingButton'));
this.createNewGeneralMeeting = function () {
this.newMeetingButton.click();
browser.waitForAngular();
};
this.goToCompanyPage = function () {
this.companyNameLink.click();
browser.waitForAngular();
};
};
Run Code Online (Sandbox Code Playgroud)
在一些spec文件中,我使用这样的页面对象..
var DashboardPage = require('../dashboardPageObject.js');
dashboardPage = new DashboardPage();
...
dashboardPage.goToCompanyPage();
Run Code Online (Sandbox Code Playgroud)
但问题是有时我得到angular could not be found on the window错误,我的测试失败了.大多数时候测试运行.这个问题是随机的.我的问题是,我应该browser.waitForAngular()从页面对象方法中删除并在我进行方法调用后调用它...
修改了页面对象
...
this.goToCompanyPage = function () {
this.companyNameLink.click();
};
...
Run Code Online (Sandbox Code Playgroud)
规格文件
dashboardPage.goToCompanyPage();
browser.waitForAngular();
Run Code Online (Sandbox Code Playgroud)
呼叫browser.waitForAngular()导致问题?我应该在哪里打电话waitForAngular有关于如何使用它的最佳做法?
下面是我的标记
<tr ng-repeat="post in posts">
<td ng-click="activePost(post)" class="title">{{post.title}}</td>
<td><button class="btn btn-danger" ng-click="delete(post)">Delete</button>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我正在尝试用title类来获取元素.
我用来访问转发器的代码是:
postsList = element.all(by.repeater('post in posts'));
Run Code Online (Sandbox Code Playgroud)
有没有办法通过在jQuery中执行以下操作来获取元素:
var titleText = $("tr:first").find(".title").text();
Run Code Online (Sandbox Code Playgroud)
有没有办法用量角器做类似的事情?
我正在尝试测试通过DI接收对ElementRef的引用的组件.
import { Component, OnInit, ElementRef } from '@angular/core';
@Component({
selector: 'cp',
templateUrl: '...',
styleUrls: ['...']
})
export class MyComponent implements OnInit {
constructor(private elementRef: ElementRef) {
//stuffs
}
ngAfterViewInit() {
// things
}
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
和测试:
import {
beforeEach,
beforeEachProviders,
describe,
expect,
it,
inject,
} from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { Component, Renderer, ElementRef } from '@angular/core';
import { By } from '@angular/platform-browser';
describe('Component: My', () => {
let builder: TestComponentBuilder;
beforeEachProviders(() …Run Code Online (Sandbox Code Playgroud) 我正在编写一个potractor测试用例来下载一个可以是任何类型的文件.
__PRE__
我现在如何检查文件是否正确下载.
我想做的是:
expect(browser.getCurrentUrl()).toEqual(protractor.baseUrl + urls.cookiesPage());
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
- Expected 'https://somewebsite.com/about/cookies/' to equal 'undefined/about/cookies/'.
Run Code Online (Sandbox Code Playgroud)
如何将baseUrl从配置文件传递到我的测试中,以便我可以断言它?
angularjs-e2e ×10
protractor ×8
angularjs ×6
javascript ×2
testing ×2
angular ×1
browser ×1
jasmine ×1
selector ×1
unit-testing ×1