File upload on a random node - Selenium WebDriver
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

How to easily upload a file to a random VM using Selenium GRID

Cloud-based Test Automation

“The worldwide public cloud services market is projected to grow 21.4 percent in 2018 to total $186.4 billion, up from $153.5 billion in 2017,” according to Gartner, Inc.

Considering this fact, I reckon the Cloud-based Test Automation is behind. Relatively recently, this type of testing has garnered so much attention over those normally delivered legacy testing approach. Let us discuss something practical rather new – that offers an approach to easily run automated tests with stringent timelines and with better efficiency. The key is to see the Automation Testers take a backseat and let the tool do its job. Cloud-based testing, in all possible ways, offers you quicker test cycles and higher productivity.

Your ability to perform tests on real devices on a grid restricts you to choose only between finite number of devices for hosting tests. On top of this, most of the organisations won’t be able to afford them. If you rely on the infrastructure that was provided to you, there are lots of chances that your tests may be limited in scope. This is where we get help of Cloud-Based Testing where we run as many parallel tests you want in the cloud.

Challenges during file upload:

Having said that, let us discuss one of the challenges that we face when running your Selenium WebDriver tests on the cloud. To make sense, let us assume we execute tests on a distributed system. Most commonly, the node gets to choose between thousands of random VMs in the cloud. We often run into a situation where we had to upload a file from local. This has always been an issue as the codebase remains in the Hub and the Node receives the commands to drive the test. This creates major issues like file uploads/downloads. The solution is simple. And this works like a charm in almost all the drivers.

Use the below method to upload a file from local to any of the machines in the cloud.


/*
* param by - locator of the element where you enter the path of the file
* param fileName – name of the file to be uploaded
*/
public void uploadFile(By by, String fileName)
    {
        try
        {
	    WebElement element = driver.findElement(by);
	    LocalFileDetector detector = new LocalFileDetector();
	    String path = new File("src/test/resources/testdata/").getAbsolutePath() 
	    +"/"+ fileName;
	    File file = detector.getLocalFile(path);
	    ((RemoteWebElement) element).setFileDetector(detector);
	    element.sendKeys(file.getAbsolutePath());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Assert.fail("Your message goes here");
        }
    }

As we all know, your driver type need to be of type RemoteWebDriver. And the RemoteWebDriver needs selenium-server-standalone to be running as a prerequisite.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up

Categories