2016-05-11 8 views
2

アップロードボタンをクリックして、Windowsファイルセレクタを開きます。分度器を使用してファイル選択ダイアログでファイルへのパスを書き込むことができません。分度器を使用してWindowsファイルセレクタを制御するには

私は、クリップボードにテキストをコピーし、Ctrl + vキーストッパーでkeypressを渡してファイルアップロードに貼り付けることを試みました。テキストをコピーすることはできますが、ペーストは機能しません。

これを実現するには、robotjsやその他のツールを使用する予定です。
分度器でこれをどのように行うことができますか?

+0

理想的にはそこにその走行試験は、あなたがマウスときにそれを移動するためならば中にへのディレクトリパスを投稿しrobotjsデスクトップ役に立たないレンダリングするので完全にファイルブラウザを避けることができ、入力フィールドになりますパスをタイプする必要があります。あなたのテキストエディタ)。私はこれを動作させることができましたが、後で家に帰るとコードサンプルを投稿することができます。 – sonhu

答えて

5

これを試しましたか?

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


var fileToUpload = '../sample.txt'; 
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.driver.sleep(100); 

// click upload button 
element(by.css('button[data-ng-click="uploadFile(file)"]')).click(); // does post request 

より多くの情報にthisthisを確認してください。

これが役に立ちます。 :)

+0

品質の良い製品。解決策は私のために働いた。 – Praveen

+0

お寄せいただきありがとうございます。 :) –

3

javascriptのレイヤー上に入力[type = "file]要素を表示する(Quality Productの回答を参照)ことができないと仮定すると、robotjsを使用してファイルブラウザからファイルをアップロードすることは可能です。

あなたはあなたの特定の状況に合わせて周りのコードスニペット以下に変更する必要がありますが、これは私がrobotjsは(ページオブジェクトのデザインパターンを利用して)仕事を得ることができた方法です

ページオブジェクト

var robot = require("robotjs"); 
var path = require("path"); 

var MediaPO = function() { 
    ... 
    ... 
    ...  

    this.clickBrowseFileLink = function() { 
     return browseFileLink.click(); //returns a promise 
    }; 

    this.uploadFile = function(filename) { 
     robot.moveMouse(648, 264) //May need to change based on desktop size 
     var uploadPath = path.resolve(__dirname,'../PATH/TO/UPLOAD/', filename); //change to your upload file's path 
     robot.setKeyboardDelay(1000); 
     console.log(uploadPath); //for debugging 
     robot.typeString(uploadPath); 
     robot.keyTap("enter"); 
     browser.sleep(3000); 
    }; 
    ... 
    ... 
    ... 

スペック

//Note: all code but uploadFile should be replaced with the code from your project 
// this is just an example 

it("should upload .png files successfully", function() { 
    ... 
    ... 
    ... 

    //your page object function for opening the file explorer 
    //must return a promise otherwise the string will be typed too early 

    mediaPO.clickBrowseFileLink().then(function() { 
     mediaPO.uploadFile("teddy.png"); 
     mediaPO.clickUploadNowButton(); //replace with your method of confirming upload. 
     expect(mediaPO.pngFileExists()).toBeTruthy(); 
    }); 
}; 
+0

ありがとうSonhu。私はrobotjsモジュールをファイルをアップロードするための私の回避策としてのみ使用していました。これは良いコードです。 – Praveen

関連する問題