vendredi 21 janvier 2022

NullPointerException with java and Selenium, TestNG and POM design pattern [duplicate]

Can you please tell my How I can fix this Null Pointer Exception?

import org.openqa.selenium.WebDriver;
            import org.openqa.selenium.chrome.ChromeDriver;
            import org.openqa.selenium.support.PageFactory;
            import org.testng.Assert;
            
            
            import java.util.concurrent.TimeUnit;
            
            
            public class BasePage {
            
            
                public WebDriver driver;
            
            
                public HomePage openBrowser() {
                    System.setProperty("webdriver.chrome.driver", "C://Users//Cata//Desktop//ChromeDriver1//chromedriver.exe");
                    driver = new ChromeDriver();
                    driver.manage().window().maximize();
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                    driver.get("https://www.emag.ro/homepage?ref=emag_CMP-60801_stock-busters-martie-2020");
                    return PageFactory.initElements(driver, HomePage.class);
                }
        
        
        
        import org.openqa.selenium.Keys;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.support.FindBy;
        import org.openqa.selenium.support.PageFactory;
        
        
        public class HomePage extends BasePage {
        
        
            @FindBy(id = "searchboxTrigger")
            public WebElement searchBox;
        
        
            public SearchPage clickSearchPage() {
        
                searchBox.sendKeys("Rasnita de cafea");
                searchBox.sendKeys(Keys.ENTER);
                return PageFactory.initElements(driver, SearchPage.class);
            }
        
        }
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    public class SearchPage extends BasePage {
    
    
    
          @FindBy(css = "input.form-control.js-custom-price-min")
          public  WebElement minimPrice;
    
          @FindBy(css = "input.form-control.js-custom-price-max")
          public WebElement maxPrice;
    
          @FindBy(css = "button.btn.btn-alt.js-custom-price-trigger")
          public WebElement filterButton;
    
          @FindBy(xpath = "//*[@id=\\\"main-container\\\"]/section[1]/div/div[2]/div[2]/div/div/div[2]/form/div[3]/button[1]")
          public WebElement caffeMachine;
    
        public SearchPage(WebDriver driver) {
            this.driver = driver;
    
        }
    
    
    
        public SearchPage filterPrice(){
               minimPrice.clear();
               minimPrice.sendKeys("200");
               maxPrice.clear();
               maxPrice.sendKeys("800");
               filterButton.click();
               return PageFactory.initElements(driver, SearchPage.class);
    
        }
    
        public ProductPage getCaffeMachine() {
            caffeMachine.click();
            System.out.println("Product is in cart");
    
    
              return PageFactory.initElements(driver, ProductPage.class );
        }
    }

    import org.openqa.selenium.WebDriver;
    
    public class ProductPage extends BasePage {
    
    
    
        public ProductPage(WebDriver driver) {
            this.driver = driver;
        }
    
        public void addToCart(){
    
        }
    
    }
    
    
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

public class EmagTest {


    HomePage homePage;
    SearchPage searchPage;
    ProductPage productPage;


    @Test
    public void writeEmagTest() {

        BasePage basePage = new BasePage();

        homePage = basePage.openBrowser();

        searchPage = homePage.clickSearchPage();
        searchPage.filterPrice();
        productPage = searchPage.getCaffeMachine();

        productPage.addToCart();
    }
}
    
    

The error message is: java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy6.clear(Unknown Source) at SearchPage.filterPrice(SearchPage.java:30) at EmagTest.writeEmagTest(EmagTest.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.util.ArrayList.forEach(ArrayList.java:1255) at org.testng.TestRunner.privateRun(TestRunner.java:766) at org.testng.TestRunner.run(TestRunner.java:587) at org.testng.SuiteRunner.runTest(SuiteRunner.java:384) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337) at org.testng.SuiteRunner.run(SuiteRunner.java:286) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187) at org.testng.TestNG.runSuitesLocally(TestNG.java:1109) at org.testng.TestNG.runSuites(TestNG.java:1039) at org.testng.TestNG.run(TestNG.java:1007) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

Aucun commentaire:

Enregistrer un commentaire