首先,要使文件上传与远程 selenium 服务器一起使用,您需要最新的protractor
(当前为 3.0.0)(它将具有最新的selenium-webdriver
nodejs 包作为依赖项)。
然后,这两行对于能够通过线路将文件发送到 selenium 节点至关重要:
var remote = require('selenium-webdriver/remote');
browser.setFileDetector(new remote.FileDetector());
而且,现在您应该能够像在本地运行测试一样上传文件。
完整的工作测试(在 BrowserStack 上测试,非常适合我):
var path = require('path'),
remote = require('selenium-webdriver/remote');
describe("File upload test", function () {
beforeEach(function () {
browser.setFileDetector(new remote.FileDetector());
browser.get("https://angular-file-upload.appspot.com/");
});
it("should upload an image", function () {
var input = element(by.model("picFile")),
uploadedThumbnail = $("img[ngf-src=picFile]");
// no image displayed
expect(uploadedThumbnail.isDisplayed()).toBe(true);
// assuming you have "test.jpg" right near the spec itself
input.sendKeys(path.resolve(__dirname, "test.jpg"));
// there is a little uploaded image displayed
expect(uploadedThumbnail.isDisplayed()).toBe(true);
});
});
另见相关问题: