I am using protractor for a Node.js application with Chrome, in one test case I have to upload image and then click add/update button. The test case is working perfectly on my machine, but on Saucelabs it cant interact with the button after the image is uploaded. If I don't upload image its able to find button and click on it but when using with image upload it doesn't.

I have printed the current URL after image upload and it shows "data:text/html,", whereas in the Saucelabs video I can see previous page.

Approaches I have tried

  1. Tried to print all available handles and it only prints 1 handle i.e. "handles are [chrome 90.0 Windows 10 #01-1] CDwindow-B29A329D03022E36745F1A844177FAA8"
  2. Print Current URL "data:text/html,"

Code

    const remote = require('selenium-webdriver/remote');
    browser.driver.setFileDetector(new remote.FileDetector());
    

    //  absolute path resolution
    const path = require('path');
    const fileToUpload = './Test_Images/' + imageName;
    const absolutePath = path.resolve(__dirname, fileToUpload);
    console.log('path is ', absolutePath);


    // Find the file input element
    const fileElem = element(by.css('input[type="file"]'));


    //upload image
    await browser.sleep(2000);
    fileElem.sendKeys(absolutePath);
    console.log('\n Image Uploaded for report');

    //print current url after image upload
    const currentUrl = (await browser.getCurrentUrl()).toString();
    console.log('\n Current URL', currentUrl);
    await browser.sleep(2000);

    //get all handles 
    browser.getAllWindowHandles().then(function (handles) {
    for (const handle of handles) {
    console.log('\n\nhandles are\n', handle.toString());
    }});

    //click on the submit button
    await adminReportsPage.createReportButton.click();
    console.log('Report Submit button Clicked ');
2

There are 2 best solutions below

3
wswebcreation On

The reason why this fails is because a remote machine in the cloud, as with Sauce Labs, does not has access to your local file system and your local machine has. I'm not 100% sure if Protractor has a workaround for this, but with a framework like for example WebdriverIO, you have the option to use a method like uploadFile. That method will only work on Chrome and will translate the file to a base64 string so you can upload it form your local machine to the remove machine.

A working example can be found here

Last but not least, keep in the back of your mind that Protractor is deprecated and with the upcoming release of Selenium 4 you can't use it anymore because it doesn't support W3C. Also check the Protractor repository for more information.

0
farhanziaf On

@wswebcreation I have found a solution, it worked by changing the

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

to

const fileElem = browser.driver.findElement(by.css('input[type="file"]'));