Cucumber 在挂钩期间失败@before:NoClassDefFoundError

Cucumber fails during @before hook : NoClassDefFoundError

提问人:Automation Engineer 提问时间:1/7/2019 最后编辑:Loïc Le DoyenAutomation Engineer 更新时间:1/8/2019 访问量:1733

问:

打开 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 {

}

示例功能

Feature: Automation Test

@Login-Successful
Scenario: Login (Successful)
Given I go to "www.yahoo.com" URL
    Then I enter "[email protected]" 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)
Java Selenium 自动化 cucumber chrome-web-driver

评论

1赞 Calvin Iyer 1/7/2019
您似乎缺少 apache commons 集合库。您可以尝试将其添加到您的构建中。
0赞 Calvin Iyer 1/8/2019
你可以从 mvnrepository.com/artifact/commons-collections/ 获取库,至于把它添加到你的构建中,这取决于你使用什么(maven、gradle等)
0赞 Automation Engineer 1/8/2019
@CalvinIyer我已经下载了 commons-collections4-4.2 jar 并添加到 java 构建路径。它仍然不起作用。还有其他建议吗?
0赞 Calvin Iyer 1/8/2019
您需要 而不是上面的错误中的库。commons-collectioncommons-collection4
0赞 Automation Engineer 1/8/2019
@CalvinIyer等等。.我还会下载 Commons-Collection jar 文件只是为了添加到 Java 构建路径吗?

答:

-1赞 Jainik Bakaraniya 1/7/2019 #1

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

评论

0赞 Automation Engineer 1/8/2019
换成什么?
0赞 undetected Selenium 1/7/2019 #2

您需要将行中的关键字从 to driver 更改为更改:System.setPropertyChromeDriver

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

自:

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

评论

0赞 Automation Engineer 1/8/2019
它没有用。只是相同的结果。:(还有其他建议吗?
0赞 Mikhail Kopylov 1/8/2019 #3

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

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

请确保您的类路径包含 .类路径中可能缺少库。org/apache/commons/collections/CollectionUtilscommons-collections

评论

0赞 Automation Engineer 1/8/2019
我已经下载了 commons-collections4-4.2 jar 并添加到 java 构建路径中。它仍然不起作用。还有其他建议吗?
0赞 Mikhail Kopylov 1/8/2019
我看到你已经在上面找到了答案。为你感到高兴!😊