无法实例化类错误 - Selenium WebDriver

时间:2015-09-11 14:24:39

标签: java selenium-webdriver testng pageobjects

我在使用java在selenium webdriver中运行我的一个测试用例时遇到了“无法实现类”错误。

以下是测试功能的类,

package Pages;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import Lib.lib;

public class KnowledgeBase extends lib{

    By sortBylink = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/div");
    By featuredOption = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]");
    By mostRcnt = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]");

    String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();

    String mostRecent = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]")).getText();

    public void initSBy() throws Exception
    {
        driver.findElement(sortBylink).click();
        Thread.sleep(1500);
    }

    public void selectfO() throws Exception
    {
        driver.findElement(featuredOption).click();
        Thread.sleep(5000);
    }

    public void selectMr() throws Exception
    {
        driver.findElement(mostRcnt).click();
        Thread.sleep(5000);
    }

    public void sortBy(String sProp) throws Exception
    {
        this.initSBy();

        if (sProp == featOption) {

            this.selectfO();

        }

        else if (sProp == mostRecent){

            this.selectMr();
        }

        else {

            System.out.println("Incorrect option. Test failed.");
        }
    }


}

以下是我的测试用例类

package TestCases;

import org.testng.annotations.Test;

import Lib.lib;
import Pages.KnowledgeBase;
import Pages.LoginPage;

public class sortingTextKB extends lib {

    LoginPage uLogin = new LoginPage();
    KnowledgeBase sortObj = new KnowledgeBase();

    //Logging In
    @Test (priority = 1)
    public void loggingIn() throws Exception
    {
        uLogin.loginToKB("uzii@test.com", "uziiiii");
    }

    //Sorting
    @Test (priority = 2)
    public void sortIn() throws Exception
    {
        sortObj.sortBy("Most Recent");
    }

}

下面是我的Lib类,它包含chrome驱动程序配置

package Lib;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class lib {

    protected static WebDriver driver = null;

    @BeforeTest
    public void chrome_extension()
    {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
        driver.get("http://www.testsite.com");
    }


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

}

当我运行我的测试用例类时,我收到以下错误,

org.testng.TestNGException: 
Cannot instantiate class TestCases.sortingTextKB
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    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)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.NullPointerException
    at Pages.KnowledgeBase.<init>(KnowledgeBase.java:22)
    at TestCases.sortingTextKB.<init>(sortingTextKB.java:12)
    ... 26 more

以下是知识库的第22行,

String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();

请让我知道为什么我面临这个'无法实例化类'的错误。感谢

4 个答案:

答案 0 :(得分:0)

您应该尝试实例化您的对象。这应该可以消除呈现的错误。

答案 1 :(得分:0)

将代码更改为这样,可能会有效。

@Test (priority = 2)
    public void sortIn() throws Exception
    { `enter code here
        sortObj = new KnowledgeBase();
        sortObj.sortBy("Most Recent");
    }

答案 2 :(得分:0)

尝试以下内容。 我在@Test注释之外创建了WebDriver对象,并在@Test中实例化为。

public class TestNGClass
{
    WebDriver driver;
    public String baseUrl = "http://newtours.demoaut.com/"; 
    @Test
    public void verifyHomepageTitle()
    {
        driver = new ChromeDriver();
        driver.get(baseUrl);
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle);
        driver.quit();
    }
}

答案 3 :(得分:0)

您可以下载&#34; chromedriver.exe&#34;然后将其添加到您的项目中。您无需使用此System.setProperty("webdriver.chrome.driver", "chromedriver.exe");。你可以ChromeDriver driver = new ChromeDriver(); 如果仍然出现错误,请提供文件&#34; chromedrive.exe&#34;。

的权限