无法在量角器中上传图像

时间:2016-12-02 09:31:52

标签: angularjs file-upload protractor ng-file-upload

面对以下错误:

  

失败:找不到使用定位器的元素:By(css选择器,输入[type =" images"])

量角器代码:

 $sql = "UPDATE complain"."SET area = $area".
        "WHERE num = $id";

上传图片的HTML代码:

it("image upload",function(){
    browser.ignoreSynchronization = true;
    browser.sleep(4000);
    element( by.css('[ng-click="imagePost()"]') ).click();
    browser.sleep(3000);
    var ele = element(by.model("files")).click();
    browser.wait(EC.visibilityOf(ele),8000,'Ele is not presented');

    var fileToUpload = "C:\Users\Shiva\Desktop\mdkg.jpg;"
    console.log(fileToUpload);
    var absolutePath = path.resolve(fileToUpload);
    console.log(absolutePath);
    $('input[type="images"]').sendKeys(absolutePath);
    element(by.buttonText('Post Image')).click();
    browser.sleep(5000);
});

在这里,我正在升级图片,当我点击上传图片时,它会导航到本地系统,它应该上传图像。上传后应该发布。

1 个答案:

答案 0 :(得分:1)

这是我使用页面对象的答案:

this.uploadImage = function(){

var path = require('path');
        var remote = require('../../../../node_modules/protractor/node_modules/selenium-webdriver/remote');
        browser.setFileDetector(new remote.FileDetector());


        var fileToUpload = '../../mdkg.jpg';
        var absolutePath = path.resolve(__dirname, fileToUpload);

        var fileElem = element(by.css('input[type="file"]'));

            // Unhide file input
        browser.executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px';  arguments[0].style.opacity = 1", fileElem.getWebElement());

        fileElem.sendKeys(absolutePath);

            // take a breath 
        browser.sleep(4000);
        element(by.buttonText('Post Image')).click();
        browser.sleep(4000);
    };

post_text.uploadImage();