Created
December 1, 2011 10:08
-
-
Save AutomatedTester/1415531 to your computer and use it in GitHub Desktop.
Fennec Native Testing using a WebDriver-esque API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
public interface Driver { | |
/** | |
* Find the first Element using the given method. | |
* | |
* @param by The locating mechanism | |
* @return The first matching element on the current context | |
* @throws RoboCopException If no matching elements are found | |
*/ | |
Element findElement(String name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
public interface Element { | |
void click(); | |
void sendKeys(String keysToSend); | |
boolean isDisplayed(); | |
String getText(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
import com.jayway.android.robotium.solo.Solo; | |
import android.app.Activity; | |
import android.test.ActivityInstrumentationTestCase2; | |
import android.test.PerformanceTestCase; | |
import android.util.Log; | |
import android.widget.Button; | |
@SuppressWarnings("unused") | |
public class ExampleTest extends ActivityInstrumentationTestCase2 { | |
@SuppressWarnings("unchecked") | |
public ExampleTest(Class activityClass) { | |
super(activityClass); | |
// TODO Auto-generated constructor stub | |
} | |
private static final String TARGET_PACKAGE_ID = "org.mozilla.gecko"; | |
private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME="org.mozilla.fennec_mozilla.App"; | |
private Solo solo; | |
private Activity activity; | |
private Driver driver; | |
private static Class<?> launcherActivityClass; | |
static{ | |
try{ | |
launcherActivityClass = Class.forName(LAUNCH_ACTIVITY_FULL_CLASSNAME); | |
} catch (ClassNotFoundException e){ | |
throw new RuntimeException(e); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public ExampleTest() throws ClassNotFoundException { | |
super(TARGET_PACKAGE_ID, launcherActivityClass); | |
} | |
@Override | |
protected void setUp() throws Exception | |
{ | |
activity = getActivity(); | |
solo = new Solo(getInstrumentation(), getActivity()); | |
driver = new FennecNativeDriver(activity, solo); | |
} | |
public void testShouldTypeInUrkBarAndGetTextBack() { | |
Element awesomebar = driver.findElement("awesome_bar"); | |
awesomebar.click(); | |
Element urlbar = driver.findElement("awesomebar_text"); | |
urlbar.sendKeys("foo"); | |
assertEquals("foo", urlbar.getText()); | |
} | |
@Override | |
public void tearDown() throws Exception { | |
try { | |
solo.finalize(); | |
}catch (Throwable e){ | |
e.printStackTrace(); | |
} | |
getActivity().finish(); | |
super.tearDown(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import android.app.Activity; | |
import android.util.Log; | |
import com.jayway.android.robotium.solo.Solo; | |
public class FennecNativeDriver implements Driver { | |
private HashMap locators = null; | |
private Activity activity; | |
private Solo solo; | |
public FennecNativeDriver(Activity activity, Solo robocop){ | |
this.activity = activity; | |
this.solo = robocop; | |
locators = convertTextToTable(getFile("/mnt/sdcard/fennec_ids.txt")); | |
} | |
@Override | |
public Element findElement(String name) { | |
if (name == null) | |
throw new IllegalArgumentException("Can not findElements when passed a null"); | |
if (locators.containsKey(name)){ | |
return new FennecNativeElement((Integer) locators.get(name), activity, solo); | |
} | |
throw new RoboCopException("Element does not exist in the list"); | |
} | |
private String getFile(String filename) | |
{ | |
File file = new File(filename); | |
StringBuilder text = new StringBuilder(); | |
try { | |
BufferedReader br = new BufferedReader(new FileReader(file)); | |
String line; | |
while ((line = br.readLine()) != null) { | |
text.append(line); | |
text.append('\n'); | |
} | |
} catch(IOException e) { | |
Log.e("Robocop", "exception with file reader on: " + filename); | |
} | |
return text.toString(); | |
} | |
//Takes a string of "key=value" pairs split by \n and creates a hash table. | |
private HashMap convertTextToTable(String data) | |
{ | |
HashMap retVal = new HashMap(); | |
String[] lines = data.split("\n"); | |
for (int i = 0; i < lines.length; i++) { | |
String[] parts = lines[i].split("="); | |
retVal.put(parts[0].trim(), parts[1].trim()); | |
} | |
return retVal; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
import java.util.List; | |
import android.app.Activity; | |
import android.view.KeyEvent; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.app.Instrumentation; | |
import com.jayway.android.robotium.solo.Solo; | |
public class FennecNativeElement implements Element { | |
private Integer id; | |
private Activity currentActivity; | |
private Solo robocop; | |
private Instrumentation instr = new Instrumentation(); | |
public FennecNativeElement(Integer id, Activity activity, Solo solo){ | |
this.id = id; | |
robocop = solo; | |
currentActivity = activity; | |
} | |
public Integer getId(){ | |
return id; | |
} | |
@Override | |
public void click() { | |
currentActivity.runOnUiThread( | |
new Runnable() { | |
public void run() { | |
Button awesomebar = (Button)currentActivity.findViewById(id); | |
awesomebar.performClick(); | |
} | |
}); | |
} | |
private Object text; | |
private Activity elementActivity; | |
@Override | |
public String getText() { | |
try { Thread.sleep(2000); } | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
elementActivity = robocop.getCurrentActivity(); | |
elementActivity.runOnUiThread( | |
new Runnable() { | |
public void run() { | |
EditText et = (EditText)elementActivity.findViewById(id); | |
text = et.getEditableText(); | |
} // end of run() method definition | |
} // end of anonymous Runnable object instantiation | |
); | |
try { Thread.sleep(2000); } | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return text.toString(); | |
} | |
@Override | |
public boolean isDisplayed() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public void sendKeys(String input) { | |
try { Thread.sleep(2000); } | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
for(int i = 0; i < input.length(); i++) { | |
char c = input.charAt(i); | |
if( c >= 'a' && c <='z') { | |
instr.sendCharacterSync(29+(int)(c-'a')); | |
continue; | |
} | |
else if( c >= 'A' && c <='Z') { | |
instr.sendCharacterSync(KeyEvent.KEYCODE_CAPS_LOCK); | |
instr.sendCharacterSync(29+(int)(c-'a')); | |
instr.sendCharacterSync(KeyEvent.KEYCODE_CAPS_LOCK); | |
continue; | |
} | |
else if( c >= '0' && c <='9') { | |
instr.sendCharacterSync(29+(int)(c-'0')); | |
continue; | |
} | |
switch (c) { | |
case '.': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_PERIOD); | |
break; | |
case '/': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_SLASH); | |
break; | |
case '\\': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_BACKSLASH); | |
break; | |
case '-': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_MINUS); | |
break; | |
case '+': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_PLUS); | |
break; | |
case ',': | |
instr.sendCharacterSync(KeyEvent.KEYCODE_COMMA); | |
break; | |
default: | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mozilla.fennec; | |
public class RoboCopException extends RuntimeException { | |
public RoboCopException(){ | |
super(); | |
} | |
public RoboCopException(String message){ | |
super(message); | |
} | |
public RoboCopException(Throwable cause){ | |
super(cause); | |
} | |
public RoboCopException(String message, Throwable cause){ | |
super(message, cause); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment