简体   繁体   中英

Uploading multiple images using Protractor

I have a scenario in my test suite, where I need to

  1. Click on a button.
  2. Upload an image from a specified directory.
  3. Wait for 15 seconds
  4. Repeat Steps 1-3 for all the images in the specified directory.

How can I achieve this - uploading an array of images, or a group of images, in specified folder, one by one. The test also includes the check that an image should not have been uploaded before. I am able to upload a single file using the code below -

var fileUpload = 'path_to_file';
absolutePath = path.resolve(__dirname,fileUpload);
console.log(absolutePath);
this.file_Upload2.sendKeys(absolutePath);
browser.actions().sendKeys(protractor.Key.ENTER).perform();
browser.sleep(20000);

Please note that there is only a single button for uploading the images and it remains constant.

If you change your fileUpload variable to point to the directory where the files are held, rather than the file itself, you can just loop over everything in the directory. Something like this:

var fileUpload = 'path_to_directory';
var file_Upload2 = this.file_Upload2;
var absolutePath = path.resolve(__dirname, fileUpload);
fs.readdir(absolutePath, (err, files) => {
    for (i = 0; i < files.length; i++) {
        var fullPath = path.resolve(absolutePath, files[i]);
        file_Upload2.clear().sendKeys(fullPath);
        browser.actions().sendKeys(protractor.Key.ENTER).perform();
        browser.sleep(20000);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM