Friday 6 January 2012

Creating Android driver project to test Google Search in Eclipse


Open Eclipse IDE
Follow the below steps to create a project

1. New> Other > Android> Android Project



2. Enter the below details
Project name:
The name of the folder where your project will be created.
Click Next
Build Target:
You want to choose as low of a version as possible unless you absolutely need to use something in a later API.
Click Next
 

Application name:
This is the human-readable title for your application — the name that will appear on the Android device.
Package name:
This is the package namespace (following the same rules as for packages in the Java programming language) where all your source code will reside.
Create Activity:
This is the name of the first Activity that will be generated in the src folder.
Min SDK Version:
This is an integer that indicates the minimum API Level required to properly run your application:
Click Finish

3.Select project from Project Explorer> Right Click> Go to Build path > Configure Build Path>  Add External JARs> Select jar files in extracted selenium web driver java source.
 


4. Select project from Project Explorer> Right Click> Go to Build path > Configure Build Path>libraries > Add Library>select User Library>Next>Click User Libraries> Click on New>Enter name of user library >OK>Click Add Jars>Select all jar files in libs folder of extracted selenium web driver java source> Click OK


Click on AndroidScriptActivity.java > Editor will open> Write the below script in Editor



import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;


public class AndroidScriptActivity extends TestCase {
    public void testProjectActivity() throws Exception{
      AndroidDriver driver=new AndroidDriver();
      driver.get("http://google.co.in");
      WebElement element=driver.findElement(By.name("q"));
      element.sendKeys("selenium");
      element.submit();
      System.out.println("The page Title is "+driver.getTitle());
     }

2 comments:

  1. Hi,

    After doing all the setup and making the activity class, and also the test class(in android test project) as written in the blog. Also, added the necessary jars.

    When running the test class it gives error at runtime like:-


    java.lang.NoClassDefFoundError: org.openqa.selenium.remote.DesiredCapabilities
    at com.simpleapp.test.OneTest.testGoogle(OneTest.java:15)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)


    Thanks,
    Mukesh
    mrawat213@gmail.com

    ReplyDelete
  2. heyy i got NoClassDefFoundError
    Could not find class 'org.openqa.selenium.android.AndroidDriver', referenced from method com.example.androidtestproj.MainActivity.testProjectActivity

    ReplyDelete