简体   繁体   English

将驱动程序/对象传递给其他页面/类-java.lang.NullPointerException

[英]Passing driver/object to other page/class - java.lang.NullPointerException

I cant pass the driver/object to next class/page and have same NullPointerException in first/beginning class. 我无法将驱动程序/对象传递给下一个类/页面,并且在第一个/开始类中具有相同的NullPointerException。

PageObject class - SearchResultsPage: PageObject类-SearchResultsPage:

public class SearchResultsPage extends BasePage{

    @FindBy(xpath = "//*[@data-original-title=\"Compare this Product\"]")
    List <WebElement> compareButton;

    @FindBy(partialLinkText = "Product Compare")
    WebElement urlComparePage;

    public SearchResultsPage(WebDriver driver) {
        super(driver);
        PageFactory.initElements(driver, this);
    }   
    public void compareItems(){
        for(WebElement compareButtons: compareButton){
            compareButtons.click();
        }
    }

    public void goToComparePage(){
        urlComparePage.click();

    }
}

PageObject class HomePage: PageObject类的HomePage:

public class HomePage extends BasePage{

    public HomePage(WebDriver driver) {
        super(driver);
        PageFactory.initElements(driver, this);
    }
    public String PAGE_TITLE = "Your Store";
    WebDriver driver;   
    @FindBy(className = "input-lg")
    WebElement inputSearch; 
    @FindBy(className = "btn-lg")
    WebElement searchButton;        

    public void isHomePage(){
        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, PAGE_TITLE);
    }

    public void inputIntoSearch(){
        String itemName = "ipod";
        inputSearch.sendKeys(itemName);
    }

    public  SearchResultsPage clickSearchButton(){
        searchButton.click();
        return PageFactory.initElements(driver, SearchResultsPage.class);
    }
}

Test class: 测试类别:

public class MainPage {
    HomePage hp;
    TopNavigation topNav;
    ComparePage cp;
    SearchResultsPage srp;

    @BeforeTest
    public void setUp(){
    WebDriver driver = new FirefoxDriver();
    driver.get("http://demo.opencart.com/");
    driver.manage().window().maximize();
    hp =  PageFactory.initElements(driver, HomePage.class);
    topNav = PageFactory.initElements(driver, TopNavigation.class);
    cp = PageFactory.initElements(driver, ComparePage.class);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

    @Test(priority = 0)
    public void checkIsHomePage(){
        hp.isHomePage();
    }

    @Test
    public void changeCurrency(){
        topNav.clickButtonChangeCurrency();
        topNav.setCurrency();
    }
    @Test
    public void searchProducts(){
        hp.inputIntoSearch();
        hp.clickSearchButton();
    }
    @Test
    public void addToCompare(){
        srp.compareItems();
    }
}

And I have 2 problem: 我有两个问题:

1.When I run test checkIsHomePage() - FAILS (NullPointerException) and changeCurrency() PASS. 1.当我运行测试checkIsHomePage()-FAILS(NullPointerException)和changeCurrency()PASS。 I dont Know why first test is FAIL if thist 2 methods are in the same PageObiect class - HomePage. 我不知道为什么如果这2个方法在同一PageObiect类-HomePage中,则第一个测试失败。 What is wrong? 怎么了?

2.When searchProduct method Pass I want to compare product using addToCompare(), but I have no idea how use PageFactory.initelements to make test on - page with search results. 2.当searchProduct方法通过时,我想使用addToCompare()比较产品,但是我不知道如何使用PageFactory.initelements在带有搜索结果的页面上进行测试。 How should I do this? 我应该怎么做?

---------------------------------------UPDATED---------------------------- Ok, i foud the reason why it doesnt work. - - - - - - - - - - - - - - - - - - - -更新 - - - - - ------------------好的,我怀疑它为什么不起作用的原因。 Its becouse of base class (BasePage). 因为它是基类(BasePage)。 I made it and extended all classes with PageObjest, and with constructor and super(driver). 我做到了,并使用PageObjest以及构造函数和super(driver)扩展了所有类。 When I deleted "extends" and "super" in PageObject classes and used (this.driver = driver) it is working now. 当我删除PageObject类中的“ extends”和“ super”并使用(this.driver = driver)时,它现在正在工作。 But what i made with this base class that it didint work?? 但是我用这个基类做了些什么呢?

---------------------------------------------UPDATED------------------------------- My BasePage is poor now: - - - - - - - - - - - - - - - - - - - - - - -更新 - - ---------------------------我的BasePage现在很差:

public class BasePage {
 WebDriver driver;
 public BasePage(WebDriver driver){
        this.driver=driver;
 }

} }

The fundamental problem is that HomePage is clearly intended to defer to BasePage (though you also call it MainPage in the snippet): its constructor passes the WebDriver instance to super(), and yet it has its own WebDriver driver member variable that never gets set, and that will be null when isHomePage() is called. 根本的问题是HomePage显然是要BasePage (尽管您在代码段中也将其称为MainPage ):其构造函数将WebDriver实例传递给super(),但是它具有自己的WebDriver driver成员变量,从未设置,并且在isHomePage()时为null。 You should instead have used the WebDriver instance from the parent class and dropped the 'masking' declaration from the child class. 相反,您应该使用父类中的WebDriver实例,并从子类中删除“ masking”声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM