我在使用pojo时会遇到异常吗?

时间:2019-12-16 04:41:28

标签: selenium nullpointerexception pojo

大家好,请检查我的代码

public class Sample1 extends Sample {
        public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\Balaji-PC\\cucumber\\SamplePro\\driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.facebook.com/");
        driver.manage().window().maximize();

        Sample s= new Sample1();
        WebElement d = s.getUsername();

        d.sendKeys("lsmanikandan");
        s.getPassword().sendKeys("manikandan");
    }
}

请在我的pojo课上检查

public class Sample {
    WebDriver driver;
    public Sample() {
        PageFactory.initElements(driver, this);
    }

    @FindBy(id = "email")
    private WebElement username;

    @FindBy(id = "pass")
    private WebElement password;

    public WebElement getUsername() {
        return username;
    }

    public WebElement getPassword() {
        return password;
    }

}

请在例外下面找到

  

线程“ main”中的异常java.lang.NullPointerException在   org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)     在   org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)     在com.sun.proxy。$ Proxy4.sendKeys(未知来源)处   org.test.Sample1.main(Sample1.java:18)

1 个答案:

答案 0 :(得分:0)

代码中的问题是您没有将驱动程序状态从子类Sample 1传递到基类Sample。

这三点是魔术

  1. 在Child类中添加参数化的构造函数-Sample1

    public Sample1(WebDriver驱动程序){super(driver); }

  2. 在父类中添加参数化的构造函数-示例

    公共样本(WebDriver驱动程序){   PageFactory.initElements(driver,this);   System.out.println(“页面工厂已启动”); }

  3. 通过传递驱动程序来创建父类中被调用页面工厂的对象。

    Sample s = new Sample1(driver);

    public class Sample {
    WebDriver driver;
    public Sample(WebDriver driver) {
        PageFactory.initElements(driver, this);
        System.out.println("Page Factory started");
    }
    
    @FindBy(id = "email")
    private WebElement username;
    
    @FindBy(id = "pass")
    private WebElement password;
    
    public WebElement getUsername() {
        System.out.println(username.getAttribute("data-testid"));
        return username;
    }
    
    public WebElement getPassword() {
        System.out.println(username.getAttribute("data-testid"));
        return password;
    }  }
    
    public class Sample {
        WebDriver driver;
    public Sample(WebDriver driver) {
        PageFactory.initElements(driver, this);
        System.out.println("Page Factory started");
    }
    
    @FindBy(id = "email")
    private WebElement username;
    
    @FindBy(id = "pass")
    private WebElement password;
    
    public WebElement getUsername() {
        System.out.println(username.getAttribute("data-testid"));
        return username;
    }
    
    public WebElement getPassword() {
        System.out.println(username.getAttribute("data-testid"));
        return password;
    }    }
    

enter image description here