获取" java.lang.NullPointerException"当使用泛型方法时

时间:2017-06-10 10:09:09

标签: java selenium selenium-webdriver nullpointerexception

所以它一直在给我" java.lang.NullPointerException"每当我在我的代码中使用泛型方法。 我似乎无法找到为什么它返回null。请任何帮助:)

控制台跟踪:

java.lang.NullPointerException
    at SignIn.FlockSignIn.windowTitle(FlockSignIn.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

通用方法:     包SignIn;

import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

public class GenericMethods {
    WebDriver driver;
    private static final Logger log = LogManager.getLogger(GenericMethods.class.getName());

    public GenericMethods(WebDriver driver){
        this.driver = driver;
    }


    public void getTitle(){
        String Title = driver.getTitle();
        log.info("The title of the Page is: "+Title);
        if(driver.getPageSource().contains(Title)){
            log.info("The Title is Correct");
        }
        else{
            log.error("The Title is InCorrect");
        }
    }
}

代码:

package SignIn;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import SignIn.GenericMethods;

public class FlockSignIn {
    static WebDriver driver;
    private static final Logger log = LogManager.getLogger(FlockSignIn.class.getName());
    static String hydraURL = "http://google.com";
    static GenericMethods gm;

    @Parameters("browserType")
    @BeforeClass
    public void beforeClass(String browserType) {
        if (browserType.equalsIgnoreCase("firefox")) {
            System.getProperty("webdriver.gecko.driver",
                    "C:\\Program Files (x86)\\geckodriver-v0.16.0-win64\\geckodriver.exe");
            driver = new FirefoxDriver();
        } else if (browserType.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
        }

        gm.maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(hydraURL);
        log.info("Opening the webclient");
    }

    @Test(priority = 0)
    public void windowTitle(){
        gm.getTitle();
    }

    @AfterClass
    public void afterClass() throws InterruptedException {
        Thread.sleep(10000);
        driver.quit();
    }

}

0 个答案:

没有答案