Posted in Automation Testing Tool, chrome, Selenium

Automate and Test your Web page in Chrome emulator and Chrome View point using selenium and Java.

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&#8221;);
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&#8221;);
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

Posted in Automation Testing Tool, blogs, chrome, Selenium

WebDriverManager :A solution to Launch the browser in selenium framework without setup of local path

To run automation script we have to set executable file path to launch any browser,Here i found some solution  i.e WebDrivermanager. By using this you don’t have to set executable path to launch the browser.

For this We have to use WebDriverManager as Java dependency to Pom.xml file to launch the Desired browser.

The Dependencies are:

<dependencies>
<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java –>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.0</version>
</dependency>

<!– https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager –>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.1.0</version>
</dependency>

<!– https://mvnrepository.com/artifact/org.testng/testng –>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>

<!– https://mvnrepository.com/artifact/com.google.guava/guava –>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>

The sample Program file is

package com.Demo.webDriverManager;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DemoTest {

private WebDriver driver;

@BeforeClass
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
/* WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}*/

@Test
public void TestWebDriverManager() throws InterruptedException {
driver.get(“https://automationtesting365.wordpress.com/author/automationtesting365/”);
Assert.assertTrue(driver.getTitle().contains(“automationtesting365”), “Test Failed”);
System.out.println(“WebDriver Manager demo Test successsfully executed”);
}

@AfterClass
public void tearDown() {
if(driver!=null)
driver.quit();
}
}

  • WebDriverManager resolves the driver binaries for the browsers Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, and Internet Explorer. For that, it provides several drivers managers for these browsers. These drivers managers can be used as follows:

WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.iedriver().setup();

  • To set Specific version of browser version you can use:

WebDriverManager.chromedriver().version(“2.40”).setup();

 

NOTE: The old WebDriverManager API (version 1.x) has been deprecated as of version 3.x 

For More about WebDriverManager you can visit

https://github.com/bonigarcia/webdrivermanager

Reference:https://github.com/bonigarcia/webdrivermanager

https://dzone.com/articles/webdrivermanager-to-manage-browser-drivers-easily.