无法实例化类packageName.pageName

时间:2016-08-04 10:03:44

标签: java selenium-webdriver testng

我得到"无法实例化类packageName.pageName"将尝试在selenium中执行测试用例。我尝试了很多,但没有找到我犯错误的地方。

以下是我的登录页面

package detailsPkg_user;

public class Login {
    public static WebDriver driver;
    String username = "userName";
    String password = "passWord";
    String baseurl = "http://mybusiness.com/login.aspx";
    WebDriverWait wait = new WebDriverWait(driver, 10); // 10 seconds


public class Details {  
    @Test(priority = 0)
    public void loginpage() {

        System.setProperty("webdriver.chrome.driver","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");
        options.addArguments("--disable-extensions");
        capabilities.setCapability("chrome.binary","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver(capabilities);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.get(baseurl);


        WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("UserName")));
        username.sendKeys(userName);

        WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("PassWord")));
        password.sendKeys(passWord);

        WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ButtonClick")));
        button.click();

    }

    // Selecting a date from date picker
    @Test(priority = 1)
    public void RecordSearch() {
        WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("CalendarId")));
        calendar.click();


        WebElement month = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        month.click();


        WebElement day = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        day.click();


        WebElement submit = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Submit")));
        submit.click();


    }

        driver.close();
}

以下是detailSearch页面代码

 package detailsPkg_user;


import org.testng.annotations.Test;
import java.util.Arrays;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.JavascriptExecutor;



public class detailSearch extends Login {

    @Test(priority = 2)
    public void loginpageDetail() { 

        System.setProperty("webdriver.chrome.driver","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");
        options.addArguments("--disable-extensions");
        capabilities.setCapability("chrome.binary","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver(capabilities);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.get(baseurl);



        WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("UserName")));
        username.sendKeys(userName);

        WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Password")));
        password.sendKeys(passWord);

        WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ButtonClick")));
        button.click();
    }   

    // Selecting and clicking DetailSearch menu
    @Test(priority = 3) 
    public void detailSelect() {    
        WebElement detailMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        detailMenu.click();



        // Datepicker of "From" calendar
        WebElement datepickerFrom = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("calendarId01")));
        datepickerFrom.click();


        WebElement monthPreviousFrom = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("leftarrow_calendar_NP']/img")));
        monthPreviousFrom.click();


        WebElement dayFirst = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        dayFirst.click();


        // Datepicker of "To" calendar
        WebElement datepickerTo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("calendarId02")));
        datepickerTo.click();


        WebElement monthPreviousTo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("leftarrow_calendar_NP']/img")));
        monthPreviousTo.click();


        WebElement monthLast = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        monthLast.click();

    }

        // Click "Submit"
        WebElement submit = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_contentMiddle_btnSearch")));
        submit.click();


        WebElement logout = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        logout.click();

        driver.close();
    }

}

当我尝试执行项目时。我在控制台中收到以下错误。

    org.testng.TestNGException: 
Cannot instantiate class detailsPkg_user.detailSearch
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:118)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:128)
    at org.testng.TestRunner.initMethods(TestRunner.java:416)
    at org.testng.TestRunner.init(TestRunner.java:242)
    at org.testng.TestRunner.init(TestRunner.java:212)
    at org.testng.TestRunner.<init>(TestRunner.java:166)
    at org.testng.remote.support.RemoteTestNG6_9_7$1.newTestRunner(RemoteTestNG6_9_7.java:26)
    at org.testng.remote.support.RemoteTestNG6_9_7$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_7.java:59)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:587)
    at org.testng.SuiteRunner.init(SuiteRunner.java:161)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1290)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1277)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)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 com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at detailsPkg_user.Login.<init>(Login.java:25)
    at detailsPkg_user.detailSearch.<init>(CDRSearch.java:21)
    ... 26 more

2 个答案:

答案 0 :(得分:1)

那是因为你在方法中实例化了驱动程序。尝试将其从方法中实例化,您可以使用@BeforeSuite

此行WebDriverWait wait = new WebDriverWait(driver, 10);也存在问题。在这里,您已经使用空驱动程序创建了wait实例,因为驱动程序稍后会被实例化。

我不确定,为什么人们编写如此复杂的代码。你可以实现同样的目标而不会让它变得那么复杂。这可以解决你的问题:

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.util.Arrays;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class DetailSearch extends Login {

    @BeforeSuite
    public void instantiateDriver() {
        System.setProperty("webdriver.chrome.driver","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");
        options.addArguments("--disable-extensions");
        capabilities.setCapability("chrome.binary","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver(capabilities);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.get(baseurl);
    }

    @Test(priority = 2)
    public void loginpageDetail() { 



        WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("UserName")));
        username.sendKeys("userName");

        WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Password")));
        password.sendKeys("passWord");

        WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ButtonClick")));
        button.click();
    }   

    // Selecting and clicking DetailSearch menu
    @Test(priority = 3) 
    public void detailSelect() {    
        WebElement detailMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        detailMenu.click();



        // Datepicker of "From" calendar
        WebElement datepickerFrom = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("calendarId01")));
        datepickerFrom.click();


        WebElement monthPreviousFrom = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("leftarrow_calendar_NP']/img")));
        monthPreviousFrom.click();


        WebElement dayFirst = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        dayFirst.click();


        // Datepicker of "To" calendar
        WebElement datepickerTo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("calendarId02")));
        datepickerTo.click();


        WebElement monthPreviousTo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("leftarrow_calendar_NP']/img")));
        monthPreviousTo.click();


        WebElement monthLast = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        monthLast.click();

    }

        // Click "Submit"
        WebElement submit = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_contentMiddle_btnSearch")));



        WebElement logout = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
    }


import java.util.Arrays;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class Login {
    public static WebDriver driver;
    String username = "userName";
    String password = "passWord";
    String baseurl = "http://mybusiness.com/login.aspx";
    WebDriverWait wait;


public class Details {  
    @BeforeSuite
    public void instantiateDriver() {
        System.setProperty("webdriver.chrome.driver","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");
        options.addArguments("--disable-extensions");
        capabilities.setCapability("chrome.binary","D:\\From H\\Selenium Package\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver(capabilities);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();

        wait = new WebDriverWait(driver, 10); // 10 seconds
    }

    @Test(priority = 0)
    public void loginpage() {
        driver.get(baseurl);


        WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("UserName")));
        username.sendKeys("userName");

        WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("PassWord")));
        password.sendKeys("passWord");

        WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ButtonClick")));
        button.click();

    }

    // Selecting a date from date picker
    @Test(priority = 1)
    public void RecordSearch() {
        WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("CalendarId")));
        calendar.click();


        WebElement month = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        month.click();


        WebElement day = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath")));
        day.click();


        WebElement submit = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Submit")));
        submit.click();


    }

}
}

答案 1 :(得分:0)

检查保存驱动程序文件/ JRE文件的文件夹结构是否已更改。即使文件夹名称的微小变化也可能是此问题的原因。 如果更改 - 更新该路径作为环境变量。