There is 2 ways to test your website on mobile browser-

1) if your domain name in mobile open like-> m.domainName.com 
then you can directly open the mobile site on simple browser opened by WebDriver and start working. 
for ex- http://m.jabong.com/,  http://m.goibibo.com/

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MobileSite {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get(“http://m.goibibo.com/“);
    }
}

2)  if your domain name in mobile open like–> www.domainName.com then it is required to change the user agent while opening the browser thru selenium. By changing the user agent, it will open the browser in mobile mode (same as opened in mobile). 

Please find the code below-

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class MobileBrowser {
    public static void main(String[] args) throws InterruptedException, IOException {
        System.out.println(“program start”);
        FirefoxProfile ffprofile = new FirefoxProfile();
        ffprofile.setPreference(“general.useragent.override”, “iPhone”); //this will change the user agent which will open mobile browser 
        WebDriver driver = new FirefoxDriver(ffprofile); 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().setSize(new Dimension(400,800)); //just to change the window size so that it will look like mobile 😉
        driver.get(“http://www.fabfurnish.com/”);
        
        //lets try sign in
        driver.findElement(By.id(“nav-menu”)).click(); //click on right corner bar icon
        driver.findElement(By.xpath(“//a[@href=’/customer/login’]/img”)).click(); //click on login
        driver.findElement(By.id(“m_log_button”)).click();
        driver.findElement(By.id(“LoginForm_email”)).sendKeys(“yourEmailId@gmail.com”);
        driver.findElement(By.id(“LoginForm_password”)).sendKeys(“yourPassword”);
        driver.findElement(By.xpath(“//input[@value=’Login’]”)).click();
        System.out.println(“End”);
    }
}

Note- To write the xpath, you can use simply use as usual Firepath with Firebug only. After changing the user-agent manually in Firefox, when you will open the website, it will open as in mobile browser and now you can use firebug/firepath as usual.

 

Source: http://selenium-makeiteasy.blogspot.com