【问题标题】:Cucumber fails during @before hook : NoClassDefFoundError@before 钩子期间黄瓜失败:NoClassDefFoundError
【发布时间】:2019-06-01 23:44:54
【问题描述】:

Chrome 浏览器打开后,自动化框架将不会继续执行 .feature 文件中的步骤(然后关闭)

GenericStepImplementation.java 代码

@Before
public void setUp() throws ConfigurationException, org.apache.commons.configuration.ConfigurationException
{
    System.setProperty("webdriver.chrome.ChromeDriver","C:\\Automation\\Webdrivers\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    config = new XMLConfiguration("sample-object-config.xml");
}

RunAutoTest.java 代码

package test_runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
glue={"code_bindings"},
features="src/test/resources/features", 
plugin = {"pretty", "html:target/cucumber-html-report"})

public class RunAutoTest {

}

Sample.feature

Feature: Automation Test

@Login-Successful
Scenario: Login (Successful)
Given I go to "www.yahoo.com" URL
    Then I enter "m@yahoo.com" into "login.username" field and click tab
    Then I enter "1234567890" into "login.password" text field
    Then I clicked on "login.loginlink" login button
    Then I wait for "15" seconds
    And I will capture the page named "Login-Successful"

作为 JUnit Test 运行后,Chrome 浏览器已打开,但未执行 Sample.feature 文件中的步骤。控制台也显示:

*Feature: Automation Test
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 11795
Only local connections are allowed.
Jan 07, 2019 6:00:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[31mFailure in before hook:[0m[31mGenericStepImplementation.setUp()[0m
[31mMessage: [0m[31mjava.lang.NoClassDefFoundError: org/apache/commons/collections/CollectionUtils
    at org.apache.commons.configuration.XMLConfiguration.constructHierarchy(XMLConfiguration.java:640)
    at org.apache.commons.configuration.XMLConfiguration.constructHierarchy(XMLConfiguration.java:635)
    at org.apache.commons.configuration.XMLConfiguration.initProperties(XMLConfiguration.java:596)
    at org.apache.commons.configuration.XMLConfiguration.load(XMLConfiguration.java:1009)
    at org.apache.commons.configuration.XMLConfiguration.load(XMLConfiguration.java:972)

【问题讨论】:

  • 您似乎缺少 apache commons 集合库。您可以尝试将其添加到您的构建中。
  • 您可以从mvnrepository.com/artifact/commons-collections/… 获取库,将其添加到您的构建中,具体取决于您使用的内容(maven、gradle 等)
  • @CalvinIyer 我已经下载了 commons-collections4-4.2 jar 并添加到 java 构建路径。它仍然无法正常工作。还有其他建议吗?
  • 您需要commons-collection 而不是commons-collection4 库,因为上面的错误。
  • @CalvinIyer 等等.. 我还会下载 commons-collection jar 文件来添加到 java 构建路径吗?

标签: java selenium automation cucumber chrome-web-driver


【解决方案1】:

您需要将System.setProperty 行中的关键字从ChromeDriver 更改为driver,即更改:

System.setProperty("webdriver.chrome.ChromeDriver","C:\\Automation\\Webdrivers\\chromedriver.exe");

收件人:

System.setProperty("webdriver.chrome.driver","C:\\Automation\\Webdrivers\\chromedriver.exe");
                             only ^^^driver^^^

【讨论】:

  • 它没有用。只是相同的结果。 :( 还有其他建议吗?
【解决方案2】:

https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html

当类在编译期间存在但在运行时不存在时会发生此异常。

请确保您的类路径包含org/apache/commons/collections/CollectionUtils。类路径中可能不存在库commons-collections

【讨论】:

  • 我已下载 commons-collections4-4.2 jar 并添加到 java 构建路径。它仍然无法正常工作。还有其他建议吗?
  • 我看到你已经找到了上面的答案。为你高兴! ?
【解决方案3】:

只需更改 System.setProperty("webdriver.chrome.ChromeDriver","C:\\Automation\\Webdrivers\\chromedriver.exe");

【讨论】:

  • 改成什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多