Javascript 单击按钮之前,请等待上载完成

Javascript 单击按钮之前,请等待上载完成,javascript,file-upload,selenium-webdriver,automation,protractor,Javascript,File Upload,Selenium Webdriver,Automation,Protractor,我有一个允许上传的模式。该模式有一个按钮,用于多次上载、要上载的文件、上载、取消和确定(选择文件后按该按钮,单击上载按钮,然后选择确定返回上一屏幕)。一切正常,但这是一个非角度的应用程序,所以在选择“确定”之前,它不会等待上传完成。我加入了browser.driver.sleep,这给了它足够的时间,但我知道这是一个不可靠的解决方案。在选择“确定”之前如何等待上传完成 测试代码: this.selectsUploadButton = function () { this.waitsFor

我有一个允许上传的模式。该模式有一个按钮,用于多次上载、要上载的文件、上载、取消和确定(选择文件后按该按钮,单击上载按钮,然后选择确定返回上一屏幕)。一切正常,但这是一个非角度的应用程序,所以在选择“确定”之前,它不会等待上传完成。我加入了browser.driver.sleep,这给了它足够的时间,但我知道这是一个不可靠的解决方案。在选择“确定”之前如何等待上传完成

测试代码:

this.selectsUploadButton = function () {
    this.waitsForElementToBeClickable(uploadButton);
    uploadButton.click().then(function () {
        browser.driver.sleep(5000);
        uploadButtonOkLogo.click();
    });
};
html:由于覆盖,我收到的错误无法单击


图像必须小于50 MB,并采用以下格式:.JPG、.GIF、.PNG
选择文件
0%

这里的最佳选择是使用量角器与
预期条件
对象一起具有的
wait()
函数,等待上传按钮被禁用
wait()
函数始终等待给定的条件为真,因此您必须提供一个参数,使其在一段时间后变为真(这应该是您所需的值)。这是怎么做的-

var EC = protractor.ExpectedConditions;
browser.wait(EC.not(EC.elementToBeClickable(uploadButton)),10000).then(function(){
    uploadButtonOkLogo.click();
}); //Wait until element is not clickable (checks if element is enabled to decide if its clickable)
browser.wait(function(){
    return uploadButton.isEnabled().then(function(enabled){
        return enabled === false;  // wait until element is disabled and if it is disabled then return true
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});
browser.wait(function(){
    return $('.percentage').getText().then(function(percent){
        return percent === '100 %'; //return true only if percentage is 100%
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});
或者,您可以编写自己的ExpectedCondition,直到元素未启用,然后在等待函数中使用它。这是怎么做的-

var EC = protractor.ExpectedConditions;
browser.wait(EC.not(EC.elementToBeClickable(uploadButton)),10000).then(function(){
    uploadButtonOkLogo.click();
}); //Wait until element is not clickable (checks if element is enabled to decide if its clickable)
browser.wait(function(){
    return uploadButton.isEnabled().then(function(enabled){
        return enabled === false;  // wait until element is disabled and if it is disabled then return true
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});
browser.wait(function(){
    return $('.percentage').getText().then(function(percent){
        return percent === '100 %'; //return true only if percentage is 100%
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});
另一种方法是通过手动写入自己要等待的ExpectedCondition,等待上传进度条达到100%。这是怎么做的-

var EC = protractor.ExpectedConditions;
browser.wait(EC.not(EC.elementToBeClickable(uploadButton)),10000).then(function(){
    uploadButtonOkLogo.click();
}); //Wait until element is not clickable (checks if element is enabled to decide if its clickable)
browser.wait(function(){
    return uploadButton.isEnabled().then(function(enabled){
        return enabled === false;  // wait until element is disabled and if it is disabled then return true
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});
browser.wait(function(){
    return $('.percentage').getText().then(function(percent){
        return percent === '100 %'; //return true only if percentage is 100%
    });
}, 10000).then(function(){
    uploadButtonOkLogo.click();
});

希望有帮助。

上传完成后是否会生成任何通知?如果是这样,您可以使用它等待通知出现在页面上。或者,如果有一些上传,也许你可以使用它。你也可以在你的问题中更新html代码吗?Thanks@Girish有一个上传栏,上传按钮变暗。上传操作完成后会发生什么?没有任何通知吗?@Girish没有,它只有那个酒吧。你能更新你问题中上传栏和上传按钮的html代码吗?谢谢。最后的EC也会有帮助