Skip to content

Commit 0edbef4

Browse files
author
Shauvik Roy Choudhary
committed
update
1 parent b2b4f17 commit 0edbef4

File tree

10 files changed

+331
-67
lines changed

10 files changed

+331
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
/captures
1313
.externalNativeBuild
1414
.cxx
15+
github.properties

androidtest-library/build.gradle

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
apply plugin: 'com.android.library'
22

3-
apply plugin: 'com.github.dcendents.android-maven'
4-
group='com.moquality'
5-
version = '1.0'
6-
3+
//apply plugin: 'com.github.dcendents.android-maven'
4+
apply plugin: 'maven-publish' // Apply this plugin at the top of your library build.gradle
75

86
android {
97
compileSdkVersion 29
108
buildToolsVersion "29.0.2"
119

1210

1311
defaultConfig {
14-
minSdkVersion 21
12+
minSdkVersion 14
1513
targetSdkVersion 29
1614
versionCode 1
1715
versionName "1.0"
@@ -31,13 +29,49 @@ android {
3129

3230
dependencies {
3331
implementation fileTree(dir: 'libs', include: ['*.jar'])
34-
3532
implementation 'androidx.appcompat:appcompat:1.1.0'
36-
testImplementation 'junit:junit:4.12'
3733

3834
implementation 'androidx.test:runner:1.2.0'
35+
// implementation 'androidx.test.espresso:espresso-core:3.2.0'
36+
}
3937

38+
def githubProperties = new Properties()
39+
githubProperties.load(new FileInputStream(rootProject.file("github.properties"))) //Set env variable GPR_USER & GPR_API_KEY if not adding a properties file
4040

41-
androidTestImplementation 'androidx.test:runner:1.2.0'
42-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
41+
def getVersionName = { ->
42+
return "1.0.0"
4343
}
44+
45+
def getArtificatId = { ->
46+
return "androidtest-library"
47+
}
48+
49+
publishing {
50+
publications {
51+
bar(MavenPublication) {
52+
groupId 'com.moquality' // Replace with group ID
53+
artifactId getArtificatId()
54+
version getVersionName()
55+
artifact("$buildDir/outputs/aar/${getArtificatId()}-release.aar")
56+
}
57+
}
58+
59+
repositories {
60+
maven {
61+
name = "GitHubPackages"
62+
/** Configure path of your package repository on Github
63+
** Replace GITHUB_USERID with your/organisation Github userID
64+
** and REPOSITORY with the repository name on GitHub
65+
*/
66+
url = uri("https://maven.pkg.github.com/moquality/android-library")
67+
credentials {
68+
/** Create github.properties in root project folder file with
69+
** gpr.usr=GITHUB_USER_ID & gpr.key=PERSONAL_ACCESS_TOKEN
70+
** Set env variable GPR_USER & GPR_API_KEY if not adding a properties file**/
71+
72+
username = githubProperties['gpr.usr'] ?: System.getenv("GPR_USER")
73+
password = githubProperties['gpr.key'] ?: System.getenv("GPR_API_KEY")
74+
}
75+
}
76+
}
77+
}

androidtest-library/src/androidTest/java/com/moquality/android/ExampleInstrumentedTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

androidtest-library/src/main/java/com/moquality/android/MoQuality.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,38 @@
77
import androidx.test.runner.screenshot.Screenshot;
88

99
import java.io.IOException;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Random;
18+
import java.util.Set;
19+
import java.util.SortedSet;
1020

1121
public class MoQuality {
1222

1323
public static String TAG = "MQ";
24+
public static MoQuality instance;
25+
26+
RoboConfig config;
27+
28+
List<Object> pageObjects;
29+
Map<String, Object> objectMap;
30+
31+
private MoQuality(){
32+
pageObjects = new ArrayList<>();
33+
objectMap = new HashMap<>();
34+
}
35+
36+
public static MoQuality get() {
37+
if(instance == null) {
38+
instance = new MoQuality();
39+
}
40+
return instance;
41+
}
1442

1543
public int log(String message) {
1644
Log.i(TAG, message);
@@ -29,5 +57,143 @@ public void takeScreenshot(String name) throws IOException {
2957
throw e;
3058
}
3159
}
60+
61+
public void startRoboTest(Map<String, String> config) {
62+
log("Starting Robo Test");
63+
// TODO: Implement Android Robo
64+
65+
RoboConfig roboConfig = new RoboConfig(config);
66+
startPageObjectRobo(roboConfig);
67+
}
68+
69+
public void startPageObjectRobo(RoboConfig config) {
70+
this.config = config;
71+
72+
Object currentPage = getCurrentPage();
73+
int count = 1000;
74+
while(count>0) {
75+
76+
if (currentPage != null) {
77+
78+
Map<String, Map<String, String>> pageMethods = this.config.getPom().get(currentPage.getClass().getCanonicalName());
79+
List<String> mSignature = query(currentPage, pageMethods);
80+
Log.d(TAG, "query selected " + mSignature);
81+
String methodName = mSignature.remove(0);
82+
List<Class> mParamTypes = new ArrayList<>();
83+
try {
84+
for (String mParam : mSignature) {
85+
mParamTypes.add(getClass(mParam));
86+
}
87+
Method m = currentPage.getClass().getMethod(methodName, mParamTypes.toArray(new Class[0]));
88+
m.invoke(currentPage, generateParams(mSignature));
89+
} catch (NoSuchMethodException e) {
90+
e.printStackTrace();
91+
} catch (IllegalAccessException e) {
92+
e.printStackTrace();
93+
} catch (InvocationTargetException e) {
94+
e.printStackTrace();
95+
}
96+
97+
} else {
98+
Log.e(TAG, "Current Page is NULL. Cannot proceed");
99+
break;
100+
}
101+
count--;
102+
}
103+
104+
// Log.d(TAG, "Page Object = "+currentPage.getClass().getCanonicalName());
105+
// Method[] methods = currentPage.getClass().getDeclaredMethods();
106+
// for(Method m : methods) {
107+
// Log.d(TAG, "- Method="+m.getName());
108+
// try {
109+
// Object[] args = getOrGenerateParams(m.getParameterTypes());
110+
// m.invoke(currentPage, args);
111+
// } catch (IllegalAccessException e) {
112+
// e.printStackTrace();
113+
// } catch (InvocationTargetException e) {
114+
// e.printStackTrace();
115+
// }
116+
// }
117+
}
118+
119+
private Class getClass(String klass) {
120+
switch (klass) {
121+
case "int": return int.class;
122+
case "java.lang.Integer": return Integer.class;
123+
case "java.lang.String": return String.class;
124+
}
125+
return null;
126+
}
127+
128+
private Object[] generateParams(List<String> classes) {
129+
List<Object> params = new ArrayList<>();
130+
Random random = new Random();
131+
String[] strings = { "+", "-", "*", "/" };
132+
for(String klass : classes) {
133+
switch (klass) {
134+
case "int":
135+
case "java.lang.Integer":
136+
params.add(random.nextInt(10)); break;
137+
case "java.lang.String":
138+
params.add(strings[random.nextInt(strings.length)]); break;
139+
}
140+
}
141+
return params.toArray();
142+
}
143+
144+
private List<String> query(Object currentPage, Map<String, Map<String, String>> pageMethods) {
145+
// TODO: Change random to querying server.
146+
Set<String> methods = pageMethods.keySet();
147+
int i = new Random().nextInt(methods.size());
148+
String method = null;
149+
while(i>=0){
150+
method = methods.iterator().next();
151+
i--;
152+
}
153+
List<String> mSignature = new ArrayList<>();
154+
mSignature.add(method);
155+
String[] params = pageMethods.get(method).get("params").split(",");
156+
mSignature.addAll(Arrays.asList(params));
157+
158+
return mSignature;
159+
}
160+
161+
private Object[] getOrGenerateParams(Class<?>[] parameterTypes) {
162+
List<Object> objects = new ArrayList<>();
163+
164+
for(Class pType : parameterTypes) {
165+
Log.d(TAG, "-- pType class="+ pType.getCanonicalName());
166+
switch (pType.getCanonicalName()) {
167+
case "java.lang.String": objects.add("0"); break;
168+
case "int": objects.add(1); break;
169+
}
170+
}
171+
172+
return objects.toArray();
173+
174+
}
175+
176+
private Object getCurrentPage() {
177+
Map<String, String> mapping = this.config.getMapping();
178+
Set<String> matchers = mapping.keySet();
179+
for(String matcher : matchers) {
180+
switch(matcher) {
181+
case "*": String klass = mapping.get(matcher);
182+
return this.objectMap.get(klass);
183+
default:
184+
// TODO: apply matchers on page
185+
}
186+
}
187+
return null;
188+
}
189+
190+
public MoQuality registerPageObjects(Object ...objects) {
191+
for(Object object : objects) {
192+
pageObjects.add(object);
193+
objectMap.put(object.getClass().getCanonicalName(), object);
194+
objectMap.put(object.getClass().getSimpleName(), object);
195+
}
196+
return this;
197+
}
32198
}
33199

0 commit comments

Comments
 (0)