PhantomJS文件上传无法使用XPath表达式

时间:2016-04-06 14:42:18

标签: javascript file-upload phantomjs casperjs

我似乎无法使fileUpload正常工作,我正在使用下面的脚本,但从屏幕截图中可以看出,文件甚至没有以下列形式选择:{{3} }

  casper.thenOpen('https://encodable.com/uploaddemo/', function () {

    this.waitForSelector(x('//input[@type="file"]'), function () {
      casper.page.uploadFile(x('//input[@type="file"]'), '/Users/stratos/Desktop/IMG_1344.png');
    });
  });
  casper.then(function () {
    this.wait(5000, function () {
       this.capture('test/integration/screenshots/uploadTest.png');
    });
  });

测试运行良好,没有错误,所以我没有迹象表明发生了什么:(

我使用的是PhantomJS 2.1.1和CasperJS 1.1.0-beta5

如果测试按下“上传”按钮,则会因未选择文件而触发页面验证。

1 个答案:

答案 0 :(得分:1)

这可能对您有所帮助

casper.then(function () {
    var fileName = "/file path/";
    this.evaluate(function (fileName) {
        __utils__.findOne('input[type="file"]').setAttribute('value', fileName)
    }, {fileName: fileName});
    this.page.uploadFile('input[type="file"]', fileName);
    console.log('Selecting file');
});
casper.then(function () {
    console.log("Clicking on Upload to notebook check box");
    this.click(x("Xpath of uploading button"));
    console.log("Clicking on Submit icon");
});