元素框架。简单示例出现 NoClassDefFoundError 异常

Htmlelements framework. NoClassDefFoundError exception appears on simple example

今天我找到了一个很好的例子,说明如何使用 Selenium Webdriver 为 Web 应用程序构建自己的测试自动化框架,并获得很好且易于理解的代码和体系结构。这个例子演示了 Yandex Htmlelements 框架的使用。 但是当我尝试用这个框架启动我的第一个简单示例时,我遇到了一个永久性问题。它的名字 "NoClassDefFoundError".

下一个是堆栈跟踪:

java.lang.NoClassDefFoundError: org/apache/commons/lang/WordUtils
at ru.yandex.qatools.htmlelements.utils.HtmlElementUtils.splitCamelCase(HtmlElementUtils.java:134)
at ru.yandex.qatools.htmlelements.utils.HtmlElementUtils.getElementName(HtmlElementUtils.java:121)
at ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementDecorator.decorate(HtmlElementDecorator.java:60)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:112)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:104)
at calculator.MainPage.<init>(MainPage.java:18)
at calculator.Test1.Test(Test1.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.WordUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

在当前应用程序中,我使用 Selenium 2.46 + Htmlelements 1.14 + 最新的 TestNG。 我还有较早的 Htmlelements 库 (1.11) 和 Selenium 2.48。 我尝试使用不同的库版本组合来启动我的示例。我还使用不同的方式来注释由适当的 类 表示的 html 块,并使用

等方法初始化我的页面对象
HtmlElementLoader.populatePageObject(this, driver);

PageFactory.initElements(new HtmlElementDecorator(driver), this);

官方教程中推荐的。但结果总是一样的:我总是在调用上述方法时得到 NoClassDefFoundError。

您缺少 commons-lang package on your classpath. If you use a dependency manager like maven this will be taken care of for you, otherwise download the jar 并将其添加到您的 lib 文件夹。

NoClassDefFoundError 通常出现在 Maven 项目中,当你的依赖项中有同一个库的几个不同的不兼容版本时。在这种情况下,maven 通常采用最旧的所需版本,这有时会带来此类问题。查找和解决问题的步骤很简单:

  1. 找出丢失的 class 所属的图书馆(只需通过谷歌搜索完整的 class 名称)
  2. 使用maven dependency plugin构建完整的依赖关系树并找出冲突版本的来源:mvn dependency:tree | grep your-problem-lib
  3. 从您的依赖项中排除冲突版本,例如: <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>3.2.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </exclusion> </exclusions> </dependency>