Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, November 23, 2016

Move File from a Directory to Another Directory With Java

There's no move() method on Java's File class. But there's a renameTo() method. We can use this method to move files on Java.

Let's see how that works.

import java.io.File;

public class MoveFileTest {
    public static void main(String[] args) {
        File srcFile = new File("test/file.txt");
        File destFile = new File("test2/file.txt");

        srcFile.renameTo(destFile);
    }
}

The file file.txt should move from test directory to test2 directory.




Cross Platform Version:

The code I've written before was for Linux only. But if you want it to work on both Linux and Windows you can rewrite the program as follows.

import java.io.File;

public class MoveFileTest {
    public static void main(String[] args) {
        File srcFile = new File("test" + File.separator + "file.txt");
        File destFile = new File("test2" + File.separator + "file.txt");

        srcFile.renameTo(destFile);
    }
}



Cross Platform and Modular Version:

If you want a more modular and reusable version of the code, you can rewrite the program as follows.

import java.io.File;

public class MoveFileTest {
    public static void main(String[] args) {
        moveFile("file.txt", "test", "test2");
    }

    private static void moveFile(String name, String srcDir, String dstDir) {
        File srcFile = new File( srcDir + File.separator + name);
        File destFile = new File(dstDir + File.separator + name);

        srcFile.renameTo(destFile);
    }
}

Here I've created a method moveFile() to move files. Now I can just call the method whenever I want to move a file.

Friday, November 4, 2016

Mobile Emulation on Desktop using Selenium WebDriver for Chrome

I got a work from a client on web automation. But everything was working on mobile browser. No matter what way I try, I couldn't get it to work on my desktop browser. Then I found that I can simulate mobile web browsers using chrome webdriver for Selenium. I would like to share this with you, in case any of you are having the same problem.

ChromeDriver Official Website This is the website where I got my solution.

I am using Java language for my work. So I am gonna write the example on Java.


Simulating Google Nexus 5

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.HashMap;
import java.util.Map;


public class ChromeMobileDemo {


    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
                           "tools\\chromedriver.exe");


        Map mobileEmulation = new HashMap();
        mobileEmulation.put("deviceName", "Google Nexus 5");

        Map chromeOptions = new HashMap();
        chromeOptions.put("mobileEmulation", mobileEmulation);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        WebDriver driver = new ChromeDriver(capabilities);



        driver.get("https://www.google.com");

        // wait 60 seconds before closing the window
        try {
            driver.wait(60000);
        } catch (InterruptedException e) {
            // do nothing
        }


        driver.close();
    }
}

Simulating iPhone 6

Just change mobileEmulation.put("deviceName", "Google Nexus 5") line to mobileEmulation.put("deviceName", "iPhone 6");


Simulate Other devices

You can select any device that you have on your Chrome's Developer Settings. Just put the same of the device.