调用静态WebDriver方法以使用TestNG调用浏览器时出错

时间:2016-06-15 19:30:50

标签: java selenium testng

这是ERROR MSG:

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

FAILED: LoginTest
java.lang.NullPointerException
    at Pro1.Driver.callBrowser(Driver.java:18)
    at OR.Login.LoginTest(Login.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    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:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

这是WebDriver方法:

    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.Test;

    public class Driver {

        static WebDriver driver;

        @Test
        public static WebDriver callBrowser() throws Exception{

            String browserName = XMLData.getXMLtagValue("Browser");

            switch (browserName){

            case "Chrome" :
                System.setProperty("webdriver.chrome.driver", "C:\\MYFW\\chromedriver.exe");
                driver = new ChromeDriver();
                break;

            case "IE" :
                System.setProperty("webdriver.ie.driver", "C:\\MYFW\\IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                break;

            case "Mozilla":
                driver = new FirefoxDriver();
                break;

            default :
                System.out.println("As code for given browser is not defined, hence, using Mozilla as default option");
                driver = new FirefoxDriver();
            }
            return driver;
        }
    }

这是呼叫计划:

import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;
    import PageFactory.*;

public class Login {

@Test
public void LoginTest() throws Exception{

String url = XMLData.getXMLtagValue("URL");
String uid = XMLData.getXMLtagValue("Uid");
String pwd = XMLData.getXMLtagValue("Pwd");

WebDriver driver = Driver.callBrowser();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(url);

LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
loginPage.username.sendKeys(uid);
loginPage.pwd.sendKeys(pwd);
loginPage.lbtn.click(); 

}
}

任何想法为什么会抛出这个错误/ NPE? 应该没有任何问题地调用Driver类。

1 个答案:

答案 0 :(得分:0)

  1. switchbytechar数据类型使用正确的int案例。

    Character browserName = <get a character from your input xml>
    
        switch (browserName){
        case 'C' :
            System.setProperty("webdriver.chrome.driver", "C:\\MYFW\\chromedriver.exe");
            driver = new ChromeDriver();
            break;
    
        case 'I' :
            System.setProperty("webdriver.ie.driver", "C:\\MYFW\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            break;
    
        default :
            System.out.println("As code for given browser is not defined, hence, using Mozilla as default option");
            driver = new FirefoxDriver();
        }
        return driver;
    
  2. 在尝试调用方法时,使用先创建一个对象 -

    WebDriver driver = new Driver().callBrowser();