如何在angularjs e2e量角器测试中上传文件 [英] How to upload file in angularjs e2e protractor testing

查看:156
本文介绍了如何在angularjs e2e量角器测试中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用angularjs e2e测试来测试文件上传。你如何在e2e测试中这样做?我通过grunt karma运行我的测试脚本。

I want to test file uploading using an angularjs e2e test. How do you do this in e2e tests? I run my test script through grunt karma.

推荐答案

是如何做到的:

var path = require('path');

it('should upload a file', function() {
  var fileToUpload = '../some/path/foo.txt',
      absolutePath = path.resolve(__dirname, fileToUpload);

  element(by.css('input[type="file"]')).sendKeys(absolutePath);    
  element(by.id('uploadButton')).click();
});




  1. 使用路径模块来解析要上传的文件的完整路径。

  2. 设置输入type =file元素的路径。 li>
  3. 点击上传按钮。

  1. Use the path module to resolve the full path of the file that you want to upload.
  2. Set the path to the input type="file" element.
  3. Click on the upload button.

这不适用于Firefox。量角器会抱怨,因为元素不可见。要在Firefox中上传,您需要使输入可见。这是我做的:

This will not work on firefox. Protractor will complain because the element is not visible. To upload in firefox you need to make the input visible. This is what I do:

browser.executeAsyncScript(function(callback) {
  // You can use any other selector
  document.querySelectorAll('#input-file-element')[0]
      .style.display = 'inline';
  callback();
});

// Now you can upload.
$('input[type="file"]').sendKeys(absolutePath);    
$('#uploadButton').click();

这篇关于如何在angularjs e2e量角器测试中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆