org.openqa.selenium.interactions.Actions上的java.lang.NullPointerException。<init>

时间:2018-04-30 07:48:55

标签: selenium

当我在POM中使用Actions类时,获取Null指针异常。

public class Homepage_purchase {

    WebDriver driver;

    @FindBy(xpath="//*[@id=\"menu-item-2822\"]") WebElement Purc_Form;
    @FindBy(xpath="//*[@id=\"menu-item-2824\"]") WebElement Purc_Form2;
    @FindBy(xpath="//*[@id=\"menu-item-2843\"]") WebElement Purc_Form6;

    public Homepage_purchase(WebDriver driver) {
    PageFactory.initElements(driver, this);
    this.driver=driver;
    }

    public void Purchaseform_1() {

    Actions act=new Actions(driver);
    act.moveToElement(Purc_Form).build().perform();
    act.moveToElement(Purc_Form2).build().perform();
    act.moveToElement(Purc_Form6).build().perform();
    Purc_Form6.click();
}
}

public class baseTest {

    public WebDriver driver;

    public void browserLaunch(String Browser_is) {

        if(Browser_is.equalsIgnoreCase("Chrome")){
            System.setProperty("webdriver.chrome.driver","C:\\Users\\veerendra\\Downloads\\python software\\chromedriver.exe");
            driver=new ChromeDriver();
        }
        else if(Browser_is.equalsIgnoreCase("firefox")){
            System.setProperty("webdriver.chrome.driver","C:\\Users\\veerendra\\Downloads\\python software\\chromedriver.exe");
            driver=new ChromeDriver();
        }
        }
    public void Url_selection(String URL,int waitime) {

        driver.get(URL);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(waitime, TimeUnit.SECONDS);}


public class Homepage_mouseover extends baseTest {

    Homepage_purchase HomePurchase;

    @BeforeClass
    public void test() {
        Homepage_mouseover ob=new Homepage_mouseover();
        ob.browserLaunch("chrome");
        ob.Url_selection("http://www.globalsqa.com/", 60);  
    }

    @Test
    public void Test1() throws Exception  {
        HomePurchase=new Homepage_purchase(driver);
        System.out.println("1111");
        HomePurchase.Purchaseform_1();
        System.out.println("2222");

    }
  

失败:Test1 java.lang.NullPointerException at   org.openqa.selenium.interactions.Actions。(Actions.java:44)at   UI_Webelements_Homescreen.Homepage_purchase.Purchaseform_1(Homepage_purchase.java:28)     在TestPackages.Homepage_mouseover.Test1(Homepage_mouseover.java:25)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

2 个答案:

答案 0 :(得分:0)

您正尝试在驱动程序之前实例化页面对象:

    Homepage_mouseover ob=new Homepage_mouseover();
    ob.browserLaunch("chrome");
    ob.Url_selection("http://www.globalsqa.com/", 60);  

将第一行移动到第三行,并将驱动程序作为参数传递。

    ob.browserLaunch("chrome");
    ob.Url_selection("http://www.globalsqa.com/", 60);  
    Homepage_mouseover ob=new Homepage_mouseover(driver);

我不确定为什么编译器没有发现你的页面对象调用缺少驱动程序参数。

另外,提醒警告:如果您目前使用的是Selenium和Chrome版本,则最大化将失败 - 搜索正确的方法以最大化使用选项。

答案 1 :(得分:0)

我试着像下面直接调用基类方法而不创建对象......现在工作正常。

// returns a promise 
function existsUsername(username){
   return this.db.one('SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)', username);
}