Now a days most of the application/url having compatibility to open in responsive or in emulator,So lot of people worried how to open these url in chrome emulator or view point using Selenium and java.
So here is the solution,Please go through this code and verify your site in chrome emulator or in View point.
Prerequisite:
You should have Java8, Selenium 3.14 + Jar in your system.I already mentioned in previous blog how to set browser path.Link
Example 1.
To open Url in chrome Nexus emulator:
Here i am using Google page to open in chrome Iphone Emulator
package testEmulatorOnChrome;
/*
* prerequisite to run this code you need
* Java 8, Selenium 3.14 +, and chrome driver
* To download all these you can visit
*/
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestEmulatorChrome {
public static void main(String[] args) {
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put(“deviceName”, “iPhone 5”); //You can use various device like iPad,iPhone,Nexus etc.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption(“mobileEmulation”, mobileEmulation);
System.setProperty(“webdriver.chrome.driver”,”D:\\Software\\chromedriver.exe”);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get(“https://www.google.com”);
driver.findElement(By.name(“q”)).sendKeys(“globallogic”);
driver.findElement(By.xpath(“//*[@id=\”tsf\”]/div[2]/div[1]/div[1]/button[2]”)).click();
}
}
Example 2:
To open Url in chrome View Point:
package testEmulatorOnChrome;
/*
* prerequisite to run this code you need
* Java 8, Selenium 3.14 +, and chrome driver
* To download all these you can visit
*/
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestMultibrowser2 {
public static void main(String[] args) {
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put(“width”, 360);
deviceMetrics.put(“height”, 640);
deviceMetrics.put(“pixelRatio”, 4.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put(“deviceMetrics”, deviceMetrics);
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setExperimentalOption(“mobileEmulation”, mobileEmulation);
System.setProperty(“webdriver.chrome.driver”,”D:\\Software\\chromedriver.exe”);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get(“https://www.google.com”);
driver.findElement(By.name(“q”)).sendKeys(“globallogic”);
driver.findElement(By.name(“btnK”)).submit();
}
}
In case you find difficulty please let me know,
Here are some list of device you can use in this code..
Thanks, Keep learning New things….
Abhishek