无法调用使其静态或添加no-args错误消息不理解逻辑

时间:2015-05-26 19:12:44

标签: java selenium-webdriver

所以我试图使用@Test Annotations调用测试并为我需要的变量调用属性文件。我想尽可能具体,所以你们可以关注最新情况,但问题是我不明白我收到的错误是如何影响我正在测试的。我会尝试解释并说清楚。

package dataSheet;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.annotations.Parameters;

    import toolsBS.Login;

    public class ReadFileData extends Login {
        WebDriver driver;
        Login objLogin; // Declare a variable of type Login

        public ReadFileData(WebDriver driver) {
            super(driver);
            // TODO Auto-generated constructor stub
        }

        File file = new File(
                "C:\\Users\\andy.williams\\BlueSource\\src\\main\\java\\dataSheet\\dataFile.properties");

        FileInputStream fileInput;
        {
            try {
                fileInput = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        Properties prop = new Properties();
        {
            // load properties file
            try {
                prop.load(fileInput);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void Setup() {
            String browser = prop.getProperty("dataFile.browser");
            String getURL = prop.getProperty("dataFile.URL");
            String strUsername = prop.getProperty("dataFile.Username");
            String strPassword = prop.getProperty("dataFile.Password");
            if (browser.equalsIgnoreCase("firefox")) { // Compares this String to
                // another String, ignoring case considerations. Two
                // strings are considered equal ignoring case if
                // they are of the same length and corresponding characters in the
                // two strings are equal ignoring case.
                driver = new FirefoxDriver(); // Construct a new object and store a
                // reference to it in the variable.

            }

            else if (browser.equalsIgnoreCase("chrome")) { // Compares this String
                // to another String, ignoring case considerations. Two
                // strings are considered equal ignoring case if they are of the
                // same length and corresponding characters in the two strings are
                // equal ignoring case.
                System.setProperty("webdriver.chrome.driver",
                        "C:\\Users\\andy.williams\\Desktop\\chromedriver.exe"); // Set
                // to
                // specified
                // Browser
                driver = new ChromeDriver(); // Construct a new object and store a
                // reference to it in the variable.
            }

            else if (browser.equalsIgnoreCase("InternetExplorer")) {
                // Compares this String to another String, ignoring case
                // considerations. Two strings are considered equal
                // ignoring case if they are of the same length and
                // corresponding characters in the two strings are equal ignoring
                // case.

                System.setProperty("webdriver.ie.driver",
                        "C:\\Users\\andy.williams\\Desktop\\IEDriverServer.exe"); // Set
                // to
                // specified
                // Browser
                driver = new InternetExplorerDriver(); // Construct a new object and
                // store a reference to it
                // in the variable.

            }
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Set
                                                                                // Specified
                                                                                // Time
                                                                                // Before
                                                                                // Timeout

            driver.get(getURL); // Call desired parameter to initiate the URL
            // Selection
            driver.manage().window().maximize(); // Maximize WebDriver window

            objLogin = new Login(driver); // Construct a new object and store a
            // reference to it in the variable.
            objLogin.loginToBlueSource(strUsername, strPassword); // Calling method
                                                                    // from login
                                                                    // class
                                                                    // assigning
                                                                    // desired
                                                                    // Username and
                                                                    // Password

        }

    }

这是我的数据文件

 # This file is used to store URL,browser and Login Credentials

    browser=chrome
    URL=http://bluesourcestaging.herokuapp.com/login
    Username=adam.thomas
    Password=asdf 

现在我将告诉你我正在打电话的考试。

package calendarPage;

    import org.openqa.selenium.WebDriver;
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    import toolsBS.Login;
    import calendar.Calendar;
    import dataSheet.ReadFileData;

    public class Practice extends ReadFileData {
        WebDriver driver;
        Login objLogin; // Declare a variable of type Login
        Calendar objCal; // Declare a variable of type Calendar

        public Practice(WebDriver driver) {
            super(driver);
            // TODO Auto-generated constructor stub
        }

        @Test
        // Call the Current Test
        public void Practice() { // Call Method
            Setup(); // Call Setup from ReadFileData Class
            objCal = new Calendar(driver); // Construct a new object and store a
                                            // reference to it in the variable.
            Assert.assertTrue(objCal.CalendarPageLoaded(), // Assert Method
                                                            // called returns value
                                                            // of True, otherwise
                                                            // state the page didn't
                                                            // load
                    "Calendar Page didn't Load");
            Reporter.log("Success, the Calendar Page Loaded<br>"); // Reports
                                                                    // success of
                                                                    // the desired
                                                                    // Test/Method

        }

        // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        @AfterTest
        // The annotated method will be run after all the test methods belonging to
        // the classes inside the <test> tag have run.
        public void closeBrowser() {

            driver.getCurrentUrl();
            driver.quit(); // close browser after each test
        }
    }

最后,这是我收到的错误。

[TestNG] Running:
      C:\Users\andy.williams\AppData\Local\Temp\testng-eclipse--300217712\testng-customsuite.xml

    FAILED CONFIGURATION: @AfterTest closeBrowser
    java.lang.NullPointerException
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:178)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.TestRunner.afterRun(TestRunner.java:1014)
        at org.testng.TestRunner.run(TestRunner.java:621)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        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)

    FAILED: Practice
    org.testng.TestNGException: 
    Can't invoke public void calendarPage.Practice.Practice(): either make it static or add a no-args constructor to your class
        at org.testng.internal.Utils.checkInstanceOrStatic(Utils.java:795)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:40)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        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)


    ===============================================
        Default test
        Tests run: 1, Failures: 1, Skips: 0
        Configuration Failures: 1, Skips: 0
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
    ===============================================



    I am not sure what I am doing wrong as I thought my code logic was correct.

1 个答案:

答案 0 :(得分:2)

您尚未定义无参数构造函数:

public Practice(){
 ...
}

您已经定义了一个名为Practice的公共void方法,IDE确实会向您发出警告。