Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

selenium-webdriver - Selenium-webdriver给我黑屏的谷歌浏览器和Java空指针异常。 有人可以建议吗?(Selenium-webdriver is giving me blank screen of google chrome and a Java null pointer exception. Can anyone suggest?)

TestBase which contains the intialization method which is creating problem :

(TestBase包含正在创建问题的初始化方法:)

package TestBase;

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

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;

    public class testBasesetup {
        public static Properties prop;
        public static WebDriver driver;
        public static testBasesetup testBaseObj;
        public  static EventFiringWebDriver e_driver;

        public testBasesetup() throws Exception 
        {   
            try {
                prop = new Properties();
                File src = new File("C:\Users\LENOVO\eclipse-workspace\Demon\src\main\java\Config\config.properties");
                FileInputStream ip = new FileInputStream(src) ;

                prop.load(ip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void initialization() throws Exception 
        {
            String browsername = prop.getProperty("browser");
            if(browsername.equals("chrome")) 
            {
                System.setProperty("webdriver.chrome.driver", "F:\Eclipse\chromedriver.exe");
                WebDriver driver = new ChromeDriver();

            }
            else if(browsername.equals("firefox"))
            {
                System.setProperty("webdriver.gecko.driver", "F:\Eclipse\browser\geckodriver.exe");
                WebDriver driver = new FirefoxDriver();
            }   

            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

            String URLtoTest = prop.getProperty("URL");
            Thread.sleep(1000);
        }

    }

Test Class which I am running and this is creating issue.

(我正在运行的测试类,这正在产生问题。)

I guess there is some issue with driver variable but don't know the error:

(我猜驱动程序变量存在一些问题,但不知道错误:)

    package PageTest;

    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;

    import Pages.LoginPage;
    import TestBase.testBasesetup;

    public class LoginPageTest extends testBasesetup{
        LoginPage LoginPageObj;
        testBasesetup testBaseObj;
        public LoginPageTest() throws Exception 
        {
            super();
        }

        @BeforeMethod
        public void setup() throws Exception 
        {
            initialization();
            LoginPageObj = new LoginPage();
            Thread.sleep(2000);
        }

        @Test()
        public void LoginCheck() throws Exception 
        {
            LoginPageObj.login("[email protected]","Potatok");
        }


        @AfterMethod
        public void tearDown() 
        {
            driver.quit();
        }
    }

Image of error:

(错误图片:)

错误图片 Code

()

  ask by Ishmeet Singh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your static instance of WebDriver remain uninitialized.

(您的WebDriver静态实例保持未初始化状态。)

You need to initialize static instance instead of local driver instance in initialization() method.

(您需要在initialize()方法中初始化静态实例,而不是本地驱动程序实例。)

Like Below:

(如下所示:)

public class testBasesetup {
        public static Properties prop;
        public static WebDriver driver;
        public static testBasesetup testBaseObj;
        public  static EventFiringWebDriver e_driver;

        public testBasesetup() throws Exception 
        {   
            try {
                prop = new Properties();
                File src = new File("C:\Users\LENOVO\eclipse-workspace\Demon\src\main\java\Config\config.properties");
                FileInputStream ip = new FileInputStream(src) ;

                prop.load(ip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void initialization() throws Exception 
        {
            String browsername = prop.getProperty("browser");
            if(browsername.equals("chrome")) 
            {
                System.setProperty("webdriver.chrome.driver", "F:\Eclipse\chromedriver.exe");
                **driver = new ChromeDriver();**

            }
            else if(browsername.equals("firefox"))
            {
                System.setProperty("webdriver.gecko.driver", "F:\Eclipse\browser\geckodriver.exe");
                **driver = new FirefoxDriver();**
            }   

            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

            String URLtoTest = prop.getProperty("URL");
            Thread.sleep(1000);
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...