0

I am getting this error in those 2 classes:

at pages.actions.HomePageActions.searchProduct(HomePageActions.java:30)
at testcases.StandardProductPricingTest.standardProductPricingTest(StandardProductPricingTest.java:23)

The code for them I am providing below:

Page.java

public class Page {

    public static WebDriver driver;
  public static void initConfigurationAndLogin() {

        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//src//test//resources//executables//chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();

   driver.get(Constants.websiteUrl);
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@id='userName']")).sendKeys(Constants.username);
        driver.findElement(By.xpath("//input[@id='passwd']")).sendKeys(Constants.password);
        driver.findElement(By.xpath(" //input[@name='login']")).click();
        driver.manage().timeouts().implicitlyWait(Constants.implicitWait, TimeUnit.SECONDS);

}

HomePageActions.java

public class HomePageActions extends Page {
    
        public HomePageLocators homePagelocators;
    
        public HomePageActions() {
    
            this.homePagelocators = new HomePageLocators();
            AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 20);
            PageFactory.initElements(factory, homePagelocators);
        }
    
        public void goToQuickOrder() {
            homePagelocators.ordersTab.click();
            homePagelocators.quickOrder.click();
    
        }
    
        public void searchProduct(String productID) throws InterruptedException {
            Thread.sleep(10000);
            homePagelocators.searchText.sendKeys(productID);
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.elementToBeClickable(homePagelocators.searchBtn));
            homePagelocators.searchBtn.click();
        }
    }



   

StandardProductPricingTest.java

 public class StandardProductPricingTest{
    
        public static String productID = "7100113022";
        HomePageActions home = new HomePageActions();
        GalleryPageActions galleryPage = new GalleryPageActions();
    
        @BeforeTest
        public void setUp() {
            Page.initConfigurationAndLogin();
        }
    
        @Test
        public void standardProductPricingTest() throws InterruptedException {
    
            home.searchProduct(productID);
            galleryPage.clickOnThePicture();
    
        }
    }

HomePageLocators .java

public class HomePageLocators {

    @FindBy(xpath = "//a[@class='hasMMMMenu MMM--order']")
    public WebElement ordersTab;
    @FindBy(linkText = "Quick Order")
    public WebElement quickOrder;
    @FindBy(id = "global-search-btn")
    public WebElement searchBtn;
    @FindBy(id = "global-search-txt")
    public WebElement searchText;

}
  • Show your HomePageLocators class – Alexey R. Jul 7 at 9:33
  • I have added the HomePageLocators. – Beti Jul 7 at 9:44
1

Please try with below changes in StandardProductPricingTest

public class StandardProductPricingTest extends Page {
    
        public static String productID = "7100113022";
        HomePageActions home ;
        GalleryPageActions galleryPage;
    
        @BeforeTest
        public void setUp() {
            initConfigurationAndLogin();
            home = new HomePageActions();
            galleryPage = new GalleryPageActions()
        }
    
        @Test
        public void standardProductPricingTest() throws InterruptedException {
    
            home.searchProduct(productID);
            galleryPage.clickOnThePicture();
    
        }
    }

Explanation

driver variable in class Page.java is not initialized in a constructor instead it happens inside a method initConfigurationAndLogin because of this driver variable is null in the class HomePageActions.java that's the reason exception is thrown

So to prevent this in the test class i have called the initConfigurationAndLogin() method before intializing the class HomePageActions so the driver variable is intialized and exception is prevented

I hope this will fix the issue

| improve this answer | |
  • Can you add couple of words about why this should work and what was wrong with OP's code? – Alexey R. Jul 7 at 14:22
  • Refer to the update answer – Mohamed Sulaimaan Sheriff Jul 7 at 14:59
  • 1
    Thank you. It helped :) – Beti Jul 8 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.