rahulrajeevan / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

Test DataDriven with Robotium #171

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi all!

I'm trying test datadriven on Robotium, but when i build, it have some
error and i can't fix them:

This is my code:

package com.hlt.stocktrade.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.hlt.stocktrade.myaccount.*;
import com.jayway.android.robotium.solo.Solo;

import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;

public class TestDatadriven extends
               ActivityInstrumentationTestCase2<CashTransferActivity_> {
       private static final String TARGET_PACKAGE_ID =
"com.hlt.stocktrade.myaccount";
       private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME =
"com.hlt.stocktrade.myaccount.CashTransferActivity_";
       private static Class<?> launcherActivityClass;
       Workbook wb;
       WritableWorkbook copy;
       WritableSheet sheet;
       Boolean FuntionResult = false;
       Cell rowData[] = null;
       private String[] FirstValue;
       private String[] SecondValue;
       private String[] ThirdValue;
       private String[] FourthValue;
       private String[] FifthValue;
       private String[] SixthValue;
       int rowCount = '0';
       WorkbookSettings ws = null;
       Workbook workbook = null;
       Sheet s = null;
       String[] TestResults;
       static {
               try {
                       launcherActivityClass = Class
                                       .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
               } catch (ClassNotFoundException e) {
                       throw new RuntimeException("Class not found" + e);
               }
       }

       public TestDatadriven() throws ClassNotFoundException {
               super(TARGET_PACKAGE_ID,
                               (Class<CashTransferActivity_>) launcherActivityClass);
       }

       private Solo solo;

       protected void setUp() throws Exception {
               solo = new Solo(getInstrumentation(), getActivity());
       }

       public void CanOpenSettings() {
               solo.pressMenuItem(0);
       }

       public void testblackbox() {
               getTestDataFile();
               TestResults = new String[rowCount];

               System.out.println("solo : " + solo);
               for (int i = 1; i < rowCount; i++) {
                       //Enter value in First editfield
                       solo.enterText(0, FirstValue[i].toString());
                       //Enter value in Second editfield
                       solo.enterText(1, SecondValue[i].toString());
                       //Enter value in Third editfield
                       solo.enterText(2, ThirdValue[i].toString());
                       //Enter value in Fourth editfield
                       solo.enterText(3, FourthValue[i].toString());
                       //Enter value in Fifth editfield
                       solo.enterText(4, FifthValue[i].toString());
                       //Enter value in Sixth editfield
                       solo.enterText(5, SixthValue[i].toString());
                       solo.clickOnText("Cash Transfer");
                       solo.clickOnText("OK");

                       TestResults[i] = "Pass";

                       //String First = String.valueOf(FirstValue[i].toString());
                       //String Second = String.valueOf(SecondValue[i].toString());
                       //String Third = String.valueOf(ThirdValue[i].toString());
                       //String Fourth = String.valueOf(FourthValue[i].toString());
                       //String Fifth = String.valueOf(FifthValue[i].toString());
                       //String Sixth = String.valueOf(SixthValue[i].toString());

               }
               writeResults(TestResults);
       }

       public void readTestData(InputStream fileInputStream) throws
Exception {
               ws = null;
               workbook = null;
               s = null;

               int columnCount = '0';
               int totalSheet = 0;
               String firstSheet = "TestCases";
               String secondSheet = "TestScenarios";

               try {
                       ws = new WorkbookSettings();
                       ws.setLocale(new Locale("en", "EN"));
                       workbook = Workbook.getWorkbook(fileInputStream, ws);

                       totalSheet = workbook.getNumberOfSheets();
                       if (totalSheet > 0) {

                               if (!workbook.getSheet(0).getName().equals(firstSheet))
                               {
                                       System.out.println("contents are not fine");
                               }
                               if (!workbook.getSheet(1).getName().equals(secondSheet))
                               {
                                       System.out.println ("contents are not fine");
                               }
                       }

                       else {
                               System.out.println("There is not any sheet available.");
                       }

                       s = workbook.getSheet(0);

                       rowCount = s.getRows();
                       FirstValue = new String[rowCount]; // First Value array size
                       SecondValue = new String[rowCount];// Second Value array size
                       ThirdValue = new String[rowCount]; // Third Value array size
                       FourthValue = new String[rowCount];// Fourth Value array size
                       FifthValue = new String[rowCount]; // Fifth Value array size
                       SixthValue = new String[rowCount];// Sixth Value array size

                       columnCount = s.getColumns();
                       rowData = s.getRow(0);

                       if (rowData[0].getContents().length() != 0) {
                               for (int i = 1; i < rowCount; i++) {

                                       rowData = s.getRow(i);

                                       if (rowData[8].getContents().equals("Yes")) {
                                               System.out.println("Executed: "
                                                               + rowData[1].getContents());
                                               FirstValue[i] = rowData[2].getContents().toString();
                                               SecondValue[i] = rowData[3].getContents().toString();
                                               ThirdValue[i] = rowData[4].getContents().toString();
                                               FourthValue[i] = rowData[5].getContents().toString();
                                               FifthValue[i] = rowData[6].getContents().toString();
                                               SixthValue[i] = rowData[7].getContents().toString();
                                       } else {
                                               System.out.println("We will skip "
                                                               + rowData[1].getContents());
                                       }
                               }
                               System.out.println("Success");

                       }
                       workbook.close();
               } catch (IOException e) {
                       e.printStackTrace();
               } catch (BiffException e) {
                       e.printStackTrace();
               }

       }
       public void writeResults(String[] results)
       {
               FileOutputStream outputStream = null;

               try{
                       outputStream = getActivity().openFileOutput("TestResultData.txt",
Context.MODE_WORLD_READABLE);

                       for (int i=0; i <results.length; i ++)
                       {
                       outputStream.write(results[1].toString().getBytes());
                       }

                       }catch (IOException e) {
                               e.printStackTrace();
                       }finally {
                                       if (outputStream != null) {
                           try {
                               outputStream.flush();
                               outputStream.close();
                           } catch (IOException e) {
                               e.printStackTrace();}
                                               }
                       }
               }

       public void getTestDataFile() {
               FileInputStream fs = null;
               try {
                       fs = getActivity().openFileInput("TestData.csv");
                       readTestData(fs);

               } catch (IOException e) {
                       e.printStackTrace();
               } catch (Exception e) {
                       e.printStackTrace();
               } finally {
                       try {
                               fs.close();
                       } catch (IOException e) {
                               e.printStackTrace();
                       }
               }
       }

       public void tearDown() throws Exception {
               try {
                       solo.finalize();
               } catch (Throwable e) {
                       e.printStackTrace();
               }
               getActivity().finish();
               super.tearDown();
       }
}

And this is error:
java.lang.NullPointerException
at
com.hlt.stocktrade.test.TestDatadriven.testblackbox(TestDatadriven.java:
76)
at java.lang.reflect.Method.invokeNative(Native Method)
at
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:
204)
at
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:
194)
at
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTes
tCase2.java:
186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:
520)
at android.app.Instrumentation
$InstrumentationThread.run(Instrumentation.java:1447)

Can anyone guide me how to fix errors?

Thanks and Regards,
Duy

Original issue reported on code.google.com by duynguye...@gmail.com on 5 Oct 2011 at 8:08

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by renasr...@gmail.com on 5 Oct 2011 at 4:51