Skip to content

Instantly share code, notes, and snippets.

@naelabdeljawad
Last active January 29, 2021 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naelabdeljawad/95fb86354316e04bc1daabb43006e1ed to your computer and use it in GitHub Desktop.
Save naelabdeljawad/95fb86354316e04bc1daabb43006e1ed to your computer and use it in GitHub Desktop.
Manage Appium Capabilities from CSV file.
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import com.opencsv.CSVReader;
public class CSVUtils {
// Please add this opencsv dependency
// <dependency>
// <groupId>com.opencsv</groupId>
// <artifactId>opencsv</artifactId>
// <version>version</version>
// </dependency>
static String path;
public HashMap<String, String> getCSVTableHashMap(String filePath) {
HashMap<String, String> hashMap = null;
CSVReader reader;
try {
reader = new CSVReader(new FileReader(filePath));
String[] row = null;
while ((row = reader.readNext()) != null) {
/*** Add Rows ***/
hashMap = new HashMap<String, String>();
hashMap.put("port", row[0]);
hashMap.put("udid", row[1]);
hashMap.put("deviceName", row[2]);
hashMap.put("platformversion", row[3]);
}
} catch (Exception e) {
e.printStackTrace();
}
return hashMap;
}
public ArrayList<HashMap<String, String>> getCSVRowByIndex(String filePath, int index) {
ArrayList<HashMap<String, String>> myArrList = null;
CSVReader reader;
try {
reader = new CSVReader(new FileReader(filePath));
String[] row = null;
int indexOfLoop = 0;
myArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
while ((row = reader.readNext()) != null) {
if (index == indexOfLoop) {
/*** Add Rows ***/
map = new HashMap<String, String>();
map.put("port", row[0]);
map.put("udid", row[1]);
map.put("deviceName", row[2]);
map.put("platformversion", row[3]);
myArrList.add(map);
break;
}
indexOfLoop++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return myArrList;
}
public int getIndexOfRowByPort(String filePath, int port) {
CSVReader reader;
try {
reader = new CSVReader(new FileReader(filePath));
String[] row = null;
int index = 0;
while ((row = reader.readNext()) != null) {
if (row[0].equals(String.valueOf(port))) {
reader.close();
return index;
}
index++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
public static void main(String[] args) {
path = System.getProperty("user.dir") + "//resources//" + "appium_capabilities.csv";
try {
CSVReader reader = new CSVReader(new FileReader(path));
String[] row = null;
ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
while ((row = reader.readNext()) != null) {
/*** Add Rows ***/
map = new HashMap<String, String>();
map.put("port", row[0]);
map.put("udid", row[1]);
map.put("deviceName", row[2]);
map.put("platformversion", row[3]);
myArrList.add(map);
}
reader.close();
// Display
for (int i = 1; i < myArrList.size(); i++) {
System.out.println("port = " + myArrList.get(i).get("port").toString());
System.out.println("udid = " + myArrList.get(i).get("udid").toString());
System.out.println("deviceName = " + myArrList.get(i).get("deviceName").toString());
System.out.println("platformversion = " + myArrList.get(i).get("platformversion").toString());
System.out.println("=========================");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment