0

我正在尝试使用 Webdriver 使用带有 JUnit 的 eclipse 在模拟器上测试网站。但我总是得到:

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)

在我添加的 Java 构建路径引用的库中:

  • android_webdriver_library.jar
  • android_webdriver_library-srcs.jar
  • 番石榴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

代码如下。有人有这样的问题,也许可以帮助解决它?

提前致谢。

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]"));
        }
}
4

4 回答 4

1

我遇到了这个问题,终于找到了解决方案。

1)右键单击项目>构建路径>配置构建路径>排序和导出

2)选择并检查“android_webdriver_library.jar”

3)将 jar 文件移动到“Android 4.X”下方和“Android Dependencies”上方(或)将其放在最高位置。

4)单击按钮,“全选”并确定。

转到项目>清洁;现在运行测试。

这仅适用于“java.lang.NoClassDefFoundError: org.openqa.selenium.android.AndroidWebDriver”错误

于 2013-04-23T12:47:47.640 回答
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.

于 2013-03-17T04:03:02.200 回答
0

我最近自己遇到了这个问题。我不确定确切的解决方案是什么,但以下步骤为我解决了这个问题。确保库位于项目的子目录中。由于我使用的是 eclipse,我注意到 eclipse 并不总是重建项目,所以我在重建之前从我的 bin/ 文件夹中删除了 apk。在那之后,我没有再遇到这个问题。

编辑:我相信您收到的错误是命名空间问题。应该是:org.openqa.selenium.android。.AndroidWebDriver

我认为这个更改是在通过 Google SDK 发布 AndroidWebDriver 之后对 selenium 树进行的,因此 wiki 中的大部分文档都反映了已发布的 jar,而不是当前 svn 树中的内容。

于 2012-07-24T00:22:49.573 回答
0

假设您使用的是 JUnit。尝试稍微改变一下测试:

@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();
    }

换句话说:如果你不使用@Before,@Test@After注释,你必须以某种方式告诉程序它必须以某种方式启动驱动程序。因此,您可以尝试的其他方法是稍微更新测试方法:

 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
    }
于 2012-05-23T09:48:47.490 回答