javascript 量角器 clear() 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19966240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor clear() not working
提问by bmw0128
I have two tests:
我有两个测试:
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');
});
the first test, search box, works great. the second test, status, does not pass because the last value entered in queryInput is carried over to the second test, and the queryInput.clear() does not work. However, in the second test, if i make a call queryInput.sendKeys("something"), "something" will display. If I take out the clear() in the second test, I'll see "motorolasomething". So, while it seems the clear() is working, my test is not passing if I just have clear() in the second test, when i run the second test, I will see "motorola", even when clear() is called prior to the second test.
第一个测试,搜索框,效果很好。第二个测试 status 没有通过,因为在 queryInput 中输入的最后一个值被转移到第二个测试中,并且 queryInput.clear() 不起作用。但是,在第二个测试中,如果我调用 queryInput.sendKeys("something"),则会显示"something"。如果我在第二次测试中取出 clear(),我会看到“motorolasomething”。因此,虽然似乎 clear() 正在工作,但如果我在第二个测试中只有 clear(),我的测试不会通过,当我运行第二个测试时,我会看到“motorola”,即使调用 clear()在第二次测试之前。
I'm wondering why the clear() is not clearing in the second test when I do not have a sendKeys() after it.
我想知道为什么 clear() 在我之后没有 sendKeys() 时没有在第二次测试中清除。
回答by Michael K.
The Documentation of clear() says the following:
clear() 的文档说明如下:
[ !webdriver.promise.Promise ] clear( )
Schedules a command to clear the {@code value} of this element. This command has no effect if the underlying DOM element is neither a text INPUT element nor a TEXTAREA element.
Returns: A promise that will be resolved when the element has been cleared.
[ !webdriver.promise.Promise ]清除( )
调度命令以清除此元素的 {@code value}。如果底层 DOM 元素既不是文本 INPUT 元素也不是 TEXTAREA 元素,则此命令无效。
返回:当元素被清除时将被解析的承诺。
so in order to get clear to do what you want, you have to work with the promise that it returns! to do so you have to use then()
所以为了清楚地做你想做的事,你必须接受它返回的承诺!为此,您必须使用then()
here is how it works:
下面是它的工作原理:
queryInput.clear().then(function() {
queryInput.sendKeys('motorola');
})
so clear()
returns you a promise to clear the input and then()
tells the promise what to do as soon as the input is cleared.
soclear()
向您返回一个清除输入的承诺,并then()
告诉承诺在清除输入后立即执行什么操作。
回答by Josf
Clear().then(..)
doesn't work for me.
Clear().then(..)
对我不起作用。
So this is my work around:
所以这是我的工作:
queryInput.sendKeys(protrator.Key.chord(protrator.Key.CONTROL, 'a'));
queryInput.sendKeys('nexus')
回答by Mary
await element.sendKeys(Key.chord(Key.CONTROL, 'a'));
await element.sendKeys(Key.DELETE);
回答by Dmytro Tolkachov
You can combine promisses into a chain:
您可以将承诺组合成一个链:
queryInput.clear().sendKeys('nexus');
queryInput.clear().sendKeys('nexus');
回答by tanacsg
Using PageObject pattern and async/await, I have such code that works:
使用 PageObject 模式和 async/await,我有这样的代码可以工作:
async clearForm() {
await this.nameInput.clear();
await this.descriptionInput.clear();
}