【问题标题】:"Cannot instantiate class" error while executing tests through Selenium Java TestNG通过 Selenium Java TestNG 执行测试时出现“无法实例化类”错误
【发布时间】:2019-04-03 20:59:01
【问题描述】:

我第一次使用 Selenium 和 TestNG,我一直在尝试通过其 ID 搜索元素,但我不断收到“无法实例化类”错误。这是我的代码:

import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

public class NewTesting {

    WebDriver driver = new FirefoxDriver();

    @BeforeTest
    public void setUp() {
        driver.get("http://book.theautomatedtester.co.uk/chapter1");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testExample() {
        WebElement element = driver.findElement(By.id("verifybutton"));
    }

}

也许我错过了安装某些东西?我为eclipse安装了TestNG插件并添加了WebDriver JAR文件,我需要做更多吗? 我尝试了多个教程,但我不断收到错误,希望有人能提供帮助。提前致谢!

编辑: 我现在有这个:

public class NewTest {
     private WebDriver driver;

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://book.theautomatedtester.co.uk/chapter1");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testExample() {
        WebElement element = driver.findElement(By.id("verifybutton"));
    }

}

它现在确实打开了网站,但我现在得到一个空指针异常:

配置失败:@AfterTest 拆解 java.lang.NullPointerException 在 NewTest.tearDown(NewTest.java:21)

【问题讨论】:

  • 您能否添加更多有关异常的详细信息?最好提供完整的例外来查看它。
  • 处理导入的最佳方法是悬停红色的 suggled 文本并以这种方式修复您的导入。这样你就得到了正确的导入(假设你做出了正确的选择,如果有多个)。
  • 您的问题已被提出并回答。如果您修复了原始问题,请打开一个新问题,其中包含新错误的详细信息……不要继续编辑这个问题,还有其他问题。这使未来的读者无法理解正在发生的事情,并且会混淆哪些答案对当前问题有效。

标签: java selenium selenium-webdriver webdriver testng


【解决方案1】:

如果您使用的是 Windows,this 上一个问题可能对您有所帮助。

上面提到你可以download geckodriver,然后像这样初始化你的FirefoxDriver:

System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

【讨论】:

    【解决方案2】:

    替换这组导入:

    import org.testng.annotations.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.*;
    

    与:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import org.testng.annotations.AfterTest;
    

    另外,您必须从mozilla/geckodriver下载所需格式的GeckoDriver可执行文件,解压二进制文件,然后初始化FirefoxDriver。

    您的有效代码块将是:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import org.testng.annotations.AfterTest;
    
    public class NewTesting {
    
        WebDriver driver;
    
        @BeforeTest
        public void setUp() {
            System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.get("http://book.theautomatedtester.co.uk/chapter1");
        }
    
        @AfterTest
        public void tearDown() {
            driver.quit();
        }
    
        @Test
        public void testExample() {
            WebElement element = driver.findElement(By.id("verifybutton"));
        }
    
    }
    

    【讨论】:

    • 我试过了,但现在它无法识别“AfterTest”和“Test”中的驱动程序。
    • 检查更新的解决方案并告诉我状态。
    • 这正在工作,只是发现了我的错误 :) 虽然仍然有一堆红色文本,但最后它说:默认测试测试运行:1,失败:0,跳过:0默认套件总测试运行:1,失败:0,跳过:0 那么红色文本正常吗?
    • @Rugo 我不确定您看到的...红色文本...。可能这些是警告,我们可以接受那些你得到的默认测试测试运行:1,失败:0,跳过:0
    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多