Madhan
Madhan

Reputation: 5818

Get Emulator list from Chrome

As per ChromeDriver site, the user can use the emulators created/present in the chrome for Selenium execution.

Detailed View Here.

enter image description here

I wanted to display all the created/available emulators from Chrome. Chrome could be storing that details in some json file or something.If so how to access it and print it in Java

Upvotes: 4

Views: 3099

Answers (1)

Madhan
Madhan

Reputation: 5818

Did a Notepad++ Find in Files and found it.

The data is stored in JSON format in file

C:\Users\Your UserName\AppData\Local\Google\Chrome\User Data\Default\Preferences

Under Key

devtools>preferences>standardEmulatedDeviceList

I have used Jackson to parse the JSON

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {

    public static void main(String[] args) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map map = mapper.readValue(
                    new File("C:\\Users\\<UserName>\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences"),
                    Map.class);
            Map devTools = (Map) map.get("devtools");
            Map preferences = (Map) devTools.get("preferences");
            String standardEmulatedDeviceList = (String) preferences.get("standardEmulatedDeviceList");
            List emulatorMap = mapper.readValue(standardEmulatedDeviceList, List.class);
            System.out.println(emulatorMap.size());
            for (Object object : emulatorMap) {
                Map device = (Map) object;
                System.out.println(device.get("title"));
            }
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Upvotes: 5

Related Questions