Showing posts with label chromedriver. Show all posts
Showing posts with label chromedriver. Show all posts

Saturday, November 5, 2016

Selenium WebDriver use a Specific Profile with ChromeDriver to Save Login Session and Cookies

I've been looking all over the internet to find a way to login to a website with Selenium and keep the cookies saved. So I won't have to relogin every time. But all I got was disappointment. Then I came across the Google Chrome Driver. It has command line options to just what I was trying to do. I just want to share it with you guys. I know it's a pain in the ass to fix things like this.

System.setProperty("webdriver.chrome.driver", "driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments(String.format("user-data-dir=%s", "profile\\dir"));
options.addArguments(String.format("profile-directory=%s", "User"));

WebDriver driver = new ChromeDriver(options);

That's how I initialized the ChromeDriver and it worked like a charm. Do remember that, profile\\dir is the directory where you want to save the google chrome profile data. User is the username for which you would like to save the preference of. Usually a directory will be created inside the user-data-dir with "User", where all the user specific configurations will be saved.

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.