Auto-tools/Projects/Robocop: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Replaced Code Snippet with reference to github.)
Line 9: Line 9:
Install Eclipse with Android Support. Make sure your tablet has USB Debugging enabled.  
Install Eclipse with Android Support. Make sure your tablet has USB Debugging enabled.  


Follow these instructions to install fennec on your android:&nbsp;[https://wiki.mozilla.org/QA/Automation_Services/Projects/FennecAutomation/FAQ wiki.mozilla.org/QA/Automation_Services/Projects/FennecAutomation/FAQ]<br>
Follow these instructions to install fennec on your android:&nbsp;[https://wiki.mozilla.org/QA/Automation_Services/Projects/FennecAutomation/FAQ wiki.mozilla.org/QA/Automation_Services/Projects/FennecAutomation/FAQ]<br>
 
This should somewhat replicate [https://github.com/tfairey/Robocop this github repository] .<br>


<br> Create a new java project.  
<br> Create a new java project.  
Line 25: Line 27:
*Select src folder. It should be empty  
*Select src folder. It should be empty  
*Add new Package. This should have the same name as your fennec package (org.mozilla.fennec or org.mozilla.fennec_&lt;username&gt;)  
*Add new Package. This should have the same name as your fennec package (org.mozilla.fennec or org.mozilla.fennec_&lt;username&gt;)  
*Add Example.java. NOTE:&nbsp;org.mozilla.fennec should be replaced by the package name of your build if it is different.
*Add ExampleTest.java from the github repository. NOTE:&nbsp;org.mozilla.fennec_tfairey should be replaced by the package name of your build if it is different.
<pre>package org.mozilla.fennec;
import java.lang.Boolean;
import com.jayway.android.robotium.solo.Solo;
import org.mozilla.robotest.R;
import android.test.ActivityInstrumentationTestCase2;
import android.app.Instrumentation;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.app.Activity;
import android.database.Cursor;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.view.Display;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import java.lang.Class;
import android.os.SystemClock;
 
@SuppressWarnings("unused")
public class Example extends ActivityInstrumentationTestCase2{
public Example(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.App";
//private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME="org.mozilla.gecko.AwesomeBar";
private static Class&lt;?&gt; launcherActivityClass;
static{
  try{
  launcherActivityClass = Class.forName(LAUNCH_ACTIVITY_FULL_CLASSNAME);
  } catch (ClassNotFoundException e){
  throw new RuntimeException(e);
  }
}
 
@SuppressWarnings("unchecked")
public Example() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
 
private Solo solo;
private Activity activity;
private Activity awesome;
private Object assertValue;
private Instrumentation inst;
 
@Override
protected void setUp() throws Exception
{
  //setActivityInitialTouchMode(true);
  activity = getActivity();
 
  inst = getInstrumentation();
  solo = new Solo(inst, activity);
}
public void testExampleTest() {
try {
waitForFennecContent();
//Thread.sleep(5500);
//Click on Awesomebar
selectAwesomeBar();
waitForAwesomeBar();
//Thread.sleep(5500);
//Switch to using AwesomeBar activity
selectURLBar();
//Thread.sleep(5500);
//Input Webpage
inputText("www.");
String[] firstAll = getAwesomeBarTabs();
inputText("mozilla.com");
String[] secondAll = getAwesomeBarTabs();
String[] thirdAll = getAwesomeBarTabs();
//Assert URL, and that the AwesomeBarTabs changed, and then didn't
assertURL("www.mozilla.com");
assertEquals(areSArraysEqual(firstAll, secondAll), false);
assertEquals(areSArraysEqual(secondAll, thirdAll), true);
//Assert that content isn't showing yet
assertEquals(contentShown(), false);
//Press Enter
sendKeys(KeyEvent.KEYCODE_ENTER);
waitForFennecContent();
//Assert Content is Showing.
assertEquals(contentShown(), true);
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void waitForFennecContent() {
while(!contentShown()) {continue;}
}
public void waitForAwesomeBar() {
awesome = solo.getCurrentActivity();
while(!awesome.toString().contains("AwesomeBar")) {
awesome = solo.getCurrentActivity();
}
}
public boolean contentShown() {
awesome = solo.getCurrentActivity();
//RelativeLayout rl = (RelativeLayout)awesome.findViewById(0x7f090015);
RelativeLayout rl = (RelativeLayout)solo.getView(0x7f090011);
if(rl == null)
{
return false;
}
    return true;
}
public boolean areSArraysEqual(String [] a, String [] b) {
boolean eq = true;
if(a.length&nbsp;!= b.length) {
return false;
}
for(int i = 0; i &lt; a.length; i++) {
if(!a[i].equals(b[i])) {
return false;
}
}
return true;
}
public String [] getAwesomeBarTabs() {
awesome = solo.getCurrentActivity();
    awesome.runOnUiThread(
      new Runnable() {
        public void run() {
          String[] result;
          ListView lv = (ListView)awesome.findViewById(0x7f090006);
          int length = lv.getCount();
          result = new String[length];
          Cursor cursor;
          for(int i = 0; i &lt; length; i++) {
          cursor = (Cursor)lv.getItemAtPosition(i);
              result[i] = cursor.getString(cursor.getColumnIndexOrThrow("url"));
          }
         
          assertValue = result;
        } // end of run() method definition
      } // end of anonymous Runnable object instantiation
        );
    try { Thread.sleep(500); }
    catch (InterruptedException e) {
e.printStackTrace();
}
return (String[])assertValue;
}
public void assertURL(String s)
{
awesome = solo.getCurrentActivity();
    awesome.runOnUiThread(
      new Runnable() {
        public void run() {
          EditText et = (EditText)awesome.findViewById(0x7f090004);
          assertValue = et.getText().toString();
        } // end of run() method definition
      } // end of anonymous Runnable object instantiation
        );
    try { Thread.sleep(5000); }
    catch (InterruptedException e) {
e.printStackTrace();
}
    assertEquals(s,(String)assertValue);
}
 
public void inputText(String input)
{
for(int i = 0; i &lt; input.length(); i++) {
char c = input.charAt(i);
if( c &gt;= 'a' &amp;&amp; c &lt;='z') {
sendKeys(29+(int)(c-'a'));
continue;
}
else if( c &gt;= 'A' &amp;&amp; c &lt;='Z') {
sendKeys(KeyEvent.KEYCODE_CAPS_LOCK);
sendKeys(29+(int)(c-'a'));
sendKeys(KeyEvent.KEYCODE_CAPS_LOCK);
continue;
}
else if( c &gt;= '0' &amp;&amp; c &lt;='9') {
sendKeys(29+(int)(c-'0'));
continue;
}
switch (c) {
case '.':
sendKeys(KeyEvent.KEYCODE_PERIOD);
break;
case '/':
sendKeys(KeyEvent.KEYCODE_SLASH);
break;
case '\\':
sendKeys(KeyEvent.KEYCODE_BACKSLASH);
    break;
case '-':
sendKeys(KeyEvent.KEYCODE_MINUS);
    break;
case '+':
sendKeys(KeyEvent.KEYCODE_PLUS);
    break;
case ',':
sendKeys(KeyEvent.KEYCODE_COMMA);
    break;
default:
}
}
}
 
public void selectAwesomeBar()
{
activity.runOnUiThread(
new Runnable() {
public void run() {
Button awesomebar = (Button)activity.findViewById(0x7f09000a);
awesomebar.performClick();
}
});
}
public void selectURLBar()
{
awesome = solo.getCurrentActivity();
    awesome.runOnUiThread(
      new Runnable() {
        public void run() {
          EditText et = (EditText)awesome.findViewById(0x7f090004);
          et.requestFocus();
        } // end of run() method definition
      } // end of anonymous Runnable object instantiation
        );
}
@Override
public void tearDown() throws Exception {
try {
solo.finalize();
}catch (Throwable e){
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
 
 


</pre>
Edit the Manifest:<br>  
Edit the Manifest:<br>  



Revision as of 18:52, 8 November 2011

Problem

With advent of NativeFennec, a new framework was needed to test the UI of the Mobile Browser. Robotium was identified as a useful framework to take advantage of, but is not complete. Robocop incorporates all of Activity, Instrumentation, and Robotium Classes to inject events into the new Java front-end of Fennec.

Example

To get the example running:

Install Eclipse with Android Support. Make sure your tablet has USB Debugging enabled.

Follow these instructions to install fennec on your android: wiki.mozilla.org/QA/Automation_Services/Projects/FennecAutomation/FAQ

This should somewhat replicate this github repository .


Create a new java project.

  • Project name doesn't matter.
  • Select the appropriate Android Version

Add Robotium

  • Project->Properties->Java Build Path->Add External JARs...
  • Select your robotium jar. It can be downloaded from here

Add Test.

  • Select src folder. It should be empty
  • Add new Package. This should have the same name as your fennec package (org.mozilla.fennec or org.mozilla.fennec_<username>)
  • Add ExampleTest.java from the github repository. NOTE: org.mozilla.fennec_tfairey should be replaced by the package name of your build if it is different.

Edit the Manifest:

  • Replace Application Section with
       <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="org.mozilla.fennec" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
<span style="font-family: sans-serif;" />

If your android device is connected by ADB, you should simply have to save everything and click the execute button. It is run as an Android JUnit test.