尝试读取 CSV 文件时使用 Selenium Web 驱动程序运行自动脚本时出错

Error running an automated script using Selenium Web Driver when trying to read a CSV file

提问人:Rodolfo Trinca 提问时间:9/27/2018 更新时间:11/8/2018 访问量:286

问:

我正在尝试使用 Selenium Web 驱动程序 (ChromeWebDriver) 运行自动测试,在脚本中我需要读取带有一些参数的 CSV 文件,该脚本的目的是使用不同的用户在网站上登录,但是在使用 JUnit 5 运行时出现以下错误:

错误:

java.lang.NoClassDefFoundError: net/sf/cglib/proxy/Callback
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: net.sf.cglib.proxy.Callback
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 17 more

这是我正在尝试运行的代码:

package selenium;

import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths = "C:\\Users\\RODOLFOTRINCA\\eclipse-workspace\\apostila\\src\\test\\java\\resources\\login.csv")
public class LoginCsv {

    static WebDriver driver;

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\RODOLFOTRINCA\\eclipse-workspace\\apostila\\drivers\\chromedriver.exe");
        driver = new ChromeDriver(); //cria obj do tipo WebDriver
        driver.manage().window().maximize(); //maximiza janela
        driver.get("http://automationpractice.com/index.php"); //abre o site desejado
    }

    @After
    public void tearDown() throws Exception {
        Thread.sleep(1500);
        driver.quit();
    }

    @Test
    public void loginComPlanilhaCSV(@Param(name="email")String email,
                                    @Param(name="password")String password,
                                    @Param(name="resultado")String resultado) throws Exception {

        driver.findElement(By.className("login")).click();
        driver.findElement(By.id("email")).sendKeys(email);
        driver.findElement(By.id("passwd")).sendKeys(password);
        driver.findElement(By.id("SubmitLogin")).click();

        Assert.assertEquals(resultado, driver.findElement(By.tagName("h1")).getText());
    }

}

这是包含以下参数的 CSV 文件:

loginComPlanilhaCSV, email, password, resultado
, [email protected], 123456, MY ACCOUNT
, [email protected], 123456, MY ACCOUNT
, [email protected], 123456, MY ACCOUNT

这些是我对pom.xml文件的依赖关系:

 <dependencies>


    <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>3.0.1</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.14.0</version>
    </dependency>

        <!-- https://mvnrepository.com/artifact/org.easetech/easytest-core -->
    <dependency>
        <groupId>org.easetech</groupId>
        <artifactId>easytest-core</artifactId>
        <version>1.4.0</version>
    </dependency>


  </dependencies>

注意:我已经尝试在依赖项上添加 cglib,但它不起作用。

注2:我是Java和自动化的初学者,这是我学习阅读CVS文件的方式,我知道还有其他方法,但如果可能的话,我想继续使用相同的方法,如果没有,也可以学习一种新的方法。

提前致谢!

java selenium-webdriver junit selenium-chromedriver chrome-web-driver

评论

0赞 PJAutomator 9/28/2018
我能够运行您的代码而不会出错。
0赞 Rodolfo Trinca 9/29/2018
我认为这是我的计算机的问题,一位同事也能够毫无错误地运行代码,但另一位同事遇到了同样的问题,我们找不到问题所在。我已经重新安装了Eclipse,但是我一直遇到同样的问题。
0赞 Dhru 'soni 10/1/2018
stackoverflow.com/questions/42698425/......
0赞 Rodolfo Trinca 10/2/2018
我尝试了另一篇文章中提出的解决方案,但仍然遇到同样的错误。

答:

0赞 Killer 11/8/2018 #1

看起来这个依赖项缺少“com.springsource.net.sf.cglib-2.2.0.jar”。 将这个添加到 pom 中并尝试。

<dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib-nodep</artifactId>
  <version>2.2</version>
</dependency>