0

I'm trying to use Webdriver to test websites on emulator using eclipse with JUnit. but I'm always getting:

java.lang.NoClassDefFoundError: org.openqa.selenium.android.AndroidWebDriver
at my.empty.project.test.SdkDemoTest.setUp(SdkDemoTest.java:22)
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)

In the Java build path Referenced libraries I've added:

  • android_webdriver_library.jar
  • android_webdriver_library-srcs.jar
  • guava-12.0.jar
  • selenium-java-2.21.0.jar
  • selenium-server-2.21.0.jar
  • selenium-server-standalone-2.0b2.jar
  • selenium-server-standalone-2.21.0.jar

The code is below. Are someone has such problem and maybe could help to resolve it?

Thanks in advance.

package my.empty.project.test;
import android.test.ActivityInstrumentationTestCase2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android;
import org.openqa.selenium.android.AndroidWebDriver;
import my.empty.project.MyAndroidProjectActivity;
public class SdkDemoTest extends ActivityInstrumentationTestCase2<MyAndroidProjectActivity>{
    private WebDriver driver;
public SdkDemoTest() {
    super("my.empty.project", MyAndroidProjectActivity.class);}
     @Override
          protected void setUp() throws Exception {
 driver = new AndroidWebDriver(getActivity());
        }
@Override
        protected void tearDown() {
           driver.quit();
        }
        public void testSDKdemoprep() {
            driver.get("http://sdkdemoprep.playphone.pluto.vn.ua/");
            WebElement element = driver.findElement(By.xpath("/html/body/img"));
            element.click();
            element = driver.findElement(By.xpath("/html/body/div[3]/div/div[9]/div[7]/div/div[2]/div/div[2]/div/div[2]"));
        }
}
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user1409817
  • 1
  • 1
  • 1

4 Answers4

1

I undergo this problem and finally got a solution to this.

1) Right Click Project > Build Path > Configure Build Path > Order and Export

2) select & check "android_webdriver_library.jar"

3) Move the jar file just below "Android 4.X" and above "Android Dependencies" (or) place it somewhere in top order.

4) Click the button, "select all" and ok.

go to project > clean; now run the test.

This is applicable only for "java.lang.NoClassDefFoundError: org.openqa.selenium.android.AndroidWebDriver" error

Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
0

Assuming that you are using JUnit. Try changing a test a little:

@Before
@Override
      protected void setUp() throws Exception {
      driver = new AndroidWebDriver(getActivity());
    }

@Test
public void testSDKdemoprep() {
        driver.get("http://sdkdemoprep.playphone.pluto.vn.ua/");
        WebElement element = driver.findElement(By.xpath("/html/body/img"));
        element.click();
        element = driver.findElement(By.xpath("/html/body/div[3]/div/div[9]/div[7]/div/div[2]/div/div[2]/div/div[2]"));
    }

@After
@Override
    protected void tearDown() {
       driver.quit();
    }

In other words: If you do not use the @Before, @Test and @After annotations, you have to tell the program somehow that it has to start the driver somehow. So other approach you can try is updating the test method a little:

 public void testSDKdemoprep() {
        setUp(); // Here you are telling to start the driver
        driver.get("http://sdkdemoprep.playphone.pluto.vn.ua/");
        WebElement element = driver.findElement(By.xpath("/html/body/img"));
        element.click();
        element = driver.findElement(By.xpath("/html/body/div[3]/div/div[9]/div[7]/div/div[2]/div/div[2]/div/div[2]"));
        tearDown(); // stop the driver
    }
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
0

I hit this problem recently myself. I'm not sure what the exact solution was, but the following steps fixed it for me. Make sure the libraries are in a subdirectory of the project. Since I was using eclipse, I've noticed that eclipse doesn't always rebuild projects, so I deleted the apk from my bin/ folder before rebuilding. After that, I wasn't hitting the problem again.

EDIT: I believe the error you're receiving is a namespace issue. It should be: org.openqa.selenium.android.library.AndroidWebDriver

I think this change was made to the selenium tree after AndroidWebDriver was published through the Google SDK, so most of the documentation in the wiki reflects the published jar, not what's in the svn tree currently.

anshumans
  • 4,045
  • 2
  • 18
  • 25
0

It took me awhile, but I finally figured this out.

All though my situation is a little different than yours since you are doing:

super("my.empty.project", MyAndroidProjectActivity.class);}

While I am doing:

super("simple.app", SimpleAppActivity.class);

Which is one of the Default Extra projects provided with the Web Driver Installation it should still apply since you'll need to make the modification to the my.empty.project

What I did was I had both Projects from the Google Web Driver extra imported into Eclipse, this left me with:

  1. SimpleApp
  2. TestAnAnroidWebApp

Which had the first working just fine but not the second which is what was throwing the:

java.lang.NoClassDefFoundError: org.openqa.selenium.android.AndroidWebDriver
at simple.app.test.SimpleGoogleTest.setUp(SimpleGoogleTest.java:21)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

What I figured out was I had to add the Libraries from the TestAnAndroidWebApp Project into the SimpleApp project. Those two library entries were:

 1. guava-r09.jar
 2. android_webdriver_library.jar

But, as I found with Android its not as simple as Copying and Pasting so what I did was follow this Answer already on Stack Overflow on how to import the Jar files and add them to my Libraries list.

This is so that they would be considered Dalvik-converted and bundled into my simple.app (Or my.empty.project in your case) which then gets copied to the Android Emulator.

This allowed me to get past the exception.

Note: It probably isn't required to bundle the guava-r09.jar but I did so anyway during my initial attempts and haven't re-tested without it.

Community
  • 1
  • 1
Welsh
  • 5,138
  • 3
  • 29
  • 43