【问题标题】:NullPointerException when using an object to call a method that has Selenium WebDriver code [duplicate]使用对象调用具有 Selenium WebDriver 代码的方法时出现 NullPointerException [重复]
【发布时间】:2016-11-23 19:45:17
【问题描述】:

我是 JAVA 和 Selenium 的新手,我真的很想了解为什么我的代码不起作用并且会抛出 NullPointerException。

基本上,我想要做的是将具有不同类的 WebDriver 实现的方法调用为将作为 JUnit 测试执行的“主测试”类。

但是每次我执行我的主测试时都会抛出 NullPointerException。

这是我将要执行的主测试:

package common_methods;

import org.junit.*;

public class Master_Test extends Configurations {

    @Before
    public void setUp(){
        try{
        testConfiguration();
        driver.get("http://only-testing-blog.blogspot.ro/");
        } catch (Exception e){
            System.out.println("setUp Exception: "+e);
        }
    }

    @After
    public void tearDown(){
        driver.close();
    }

    @Test
    public void masterTest(){
        try{
        TestCase1 testy1 = new TestCase1();
        testy1.test1();
        }catch (Exception master_e){
            System.out.println("Test Exception: "+master_e);
        }
    }
}

现在为了更好地理解这里是正在扩展的配置类:

package common_methods;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Configurations {

    public WebDriver driver;

    public void testConfiguration() throws Exception{
        System.setProperty("webdriver.chrome.driver", "D:\\Browser_drivers\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
}

这是我从中获取方法的 TestCase1 类:

package common_methods;

import org.openqa.selenium.*;

public class TestCase1 extends Configurations{

    public void test1() throws Exception{
        driver.findElement(By.id("tooltip-1")).sendKeys("Test Case 1 action");
        Thread.sleep(5000);
    }
}

为什么会出现 NullPointerException?

【问题讨论】:

    标签: java selenium junit nullpointerexception


    【解决方案1】:

    在您调用的 Master_Test 中

    TestCase1 testy1 = new TestCase1();
    

    您需要传递驱动程序引用,以便它不会给出 NullPointerException。此外,您需要在 TestCase1 类中添加一个构造函数来处理驱动程序。

    检查下面的代码。我已将它与 FirefoxDriver 和 Google 一起使用 -

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Configurations {
    
        public WebDriver driver;
    
        public void testConfiguration() {
            driver = new FirefoxDriver();
        }
    }
    

    Master_Test

    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.WebDriver;
    
    
    
    public class Master_Test extends Configurations {
    
        @Before
        public void setUP() {
            testConfiguration();
            driver.get("http://www.google.com");
        }
    
        @Test
        public void masterTest() {
            TestCase1 test = new TestCase1(driver);
            test.test1();
        }
    }
    

    测试用例1

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    
    public class TestCase1 extends Configurations {
    
        public TestCase1(WebDriver driver) {
            this.driver = driver;
        }
    
        public void test1() {
            driver.findElement(By.id("lst-ib")).sendKeys("test");
        }
    }
    

    【讨论】:

    • 谢谢阿尼什!它有效,我明白为什么它不起作用:)
    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多