samedi 30 janvier 2016

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.

PageObject class - 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:

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://ift.tt/1lEuO3l");
    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. I dont Know why first test is FAIL if thist 2 methods are in the same PageObiect class - 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. How should I do this?

Aucun commentaire:

Enregistrer un commentaire