Javascript 量角器清除()不工作
我有两个测试:Javascript 量角器清除()不工作,javascript,protractor,Javascript,Protractor,我有两个测试: it('should filter the phone list as user types into the search box', function() { var results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name')); results.then(function(arr) { expect(arr.
it('should filter the phone list as user types into the search box', function() {
var results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
results.then(function(arr) {
expect(arr.length).toEqual(3);
});
var queryInput = ptor.findElement(protractor.By.input('query'));
queryInput.sendKeys('nexus');
results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
results.then(function(arr) {
expect(arr.length).toEqual(1);
});
queryInput.clear();
queryInput.sendKeys('motorola');
results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
results.then(function(arr) {
expect(arr.length).toEqual(2);
});
});
it('should display the current filter value within an element with id "status"',
function() {
//expect(element('#status').text()).toMatch(/Current filter: \s*$/);
var queryInput = ptor.findElement(protractor.By.input('query'));
queryInput.clear();
expect(ptor.findElement(protractor.By.id('status')).getText()).toMatch('Current Filter:');
//input('query').enter('nexus');
//queryInput.clear();
//queryInput.sendKeys('nexus');
//expect(element('#status').text()).toMatch(/Current filter: nexus\s*$/);
//expect(ptor.findElement(protractor.By.id('status')).getText()).toMatch('^\Current Filter:.');
//alternative version of the last assertion that tests just the value of the binding
//using('#status').expect(binding('query')).toBe('nexus');
});
第一个测试,搜索框,效果很好。
第二个测试status未通过,因为在queryInput中输入的最后一个值被带入第二个测试,并且queryInput.clear()不起作用。
但是,在第二个测试中,如果我调用queryInput.sendKeys(“something”),“something”将显示出来。
如果我在第二次测试中取出clear(),我会看到“motorolasomething”。
因此,虽然clear()似乎在工作,但如果我在第二个测试中只有clear(),我的测试就不会通过,当我运行第二个测试时,我会看到“motorola”,即使在第二个测试之前调用了clear()
我想知道为什么clear()在第二次测试中没有清除,而后面没有sendKeys()。clear()的文档说明如下:
[!webdriver.promise.promise]清除()
安排要清除的命令
此元素的{@code value}。如果
底层DOM元素既不是文本输入元素,也不是TEXTAREA
元素
返回:一个承诺,该承诺将在元素
被清除了
因此,为了清楚地做你想做的事情,你必须承诺它会回来!为此,您必须使用then()
以下是它的工作原理:
queryInput.clear().then(function() {
queryInput.sendKeys('motorola');
})
因此,
clear()
返回一个清除输入的承诺,然后()
告诉承诺在清除输入后要做什么。您可以将承诺组合成一个链:
queryInput.clear().sendKeys('nexus')代码>Clear()。然后(…)
对我不起作用
这就是我的工作:
queryInput.sendKeys(protrator.Key.chord(protrator.Key.CONTROL, 'a'));
queryInput.sendKeys('nexus')
使用PageObject模式和async/await,我有这样的代码:
async clearForm() {
await this.nameInput.clear();
await this.descriptionInput.clear();
}
这对我很有用:
click(`${fieldId}`).then(() => {
yourElement.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a")).then(() => {
yourElement.sendKeys(protractor.Key.BACK_SPACE);
yourElement.clear();
}
请忽略,我看到问题了。clear()正在工作,但调用clear()后,需要使用另一个方法在页面上更新status div。您还可以链接这些承诺以节省一些空间:queryInput.clear().sendKeys('motorola').sendKeys(Gragrator.Key.ENTER);不,你不能束缚他们。这并不能解决清晰的承诺。你必须使用.then()
这些不是承诺,承诺实际上只有then()
方法。但我对此投了更高的票,因为它是有效的,因为它们只是同一个对象上的链接方法。这个解决方案在其他平台上有效吗?(像macOS)不适合我。这里有一个解决跨平台和/或顽固问题的方法:stackoverflow.com/a/65477389/3021889(例如,自动格式化字段或量角器/OS不兼容)
click(`${fieldId}`).then(() => {
yourElement.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a")).then(() => {
yourElement.sendKeys(protractor.Key.BACK_SPACE);
yourElement.clear();
}