Getting 'java.lang.NullPointerException' on executing test Cases



  •  import org.openqa.selenium.WebDriver;
        import org.testng.Assert;
        import org.testng.annotations.AfterMethod;
        import org.testng.annotations.BeforeMethod;
        import org.testng.annotations.Test;
    
        import com.ebay.qa.base.TestBase1;
        import com.ebay.qa.pages.HomePage;
        import com.ebay.qa.pages.SigninPage;
    
        public class SigninPageTest extends TestBase1 {
    
            SigninPage signinPage;
            HomePage homePage;
            public WebDriver driver;
            public SigninPageTest() {
                super();
            }
    
    
            @BeforeMethod
            public void setup() {
                initialization();
                SigninPage signinPage = new SigninPage();
            }
    
            @Test
    
            public void SigninPageTitleTest() throws InterruptedException {
            String title =  signinPage.valiadateSigninPagetitle();
            Thread.sleep(3000);
            Assert.assertEquals(title, "Sign in or Register");
            }
            @Test
                public void EbaylogoimgTest() {
                boolean logo = signinPage.ebayimage();
                Assert.assertTrue(logo);
                }
            @Test
            public void SigninTest() {
           homePage =   signinPage.login(prop.getProperty("username"), prop.getProperty("password"));
    
            }
    
    
    
    
            @AfterMethod
            public void teardown() {
                if (driver != null) {
                    //You might want to debug this though
                    driver.quit();
                    }
    
            }
    
    
            }
    

    ========================================================================== SIGN IN PAGE

    package com.ebay.qa.pages;
    
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    import com.ebay.qa.base.TestBase1;
    
    public class SigninPage extends TestBase1{
    
        //define page factory or Object repository
        @FindBy(xpath = "//input[@id='userid']")
        WebElement username;
        @FindBy(xpath = "//input[@id='pass']")
        WebElement password;
        @FindBy(xpath="//button[@id='sgnBt']")
        WebElement submitBtn;
    
    
        @FindBy (xpath = "//a[@id='InLineCreateAnAccount']")
        WebElement createanAccount;
    
        @FindBy(xpath ="//img[conatains(@id='gh-logo')]")
        WebElement ebaylogo;
    
        public SigninPage() {
            PageFactory.initElements(driver, this);
            //this means current class objects
    
        }
    
        public String valiadateSigninPagetitle() 
        {
            return driver.getTitle();
        }
        public boolean ebayimage()
        {
            return ebaylogo.isDisplayed();
        }
        public HomePage login(String un, String pwd)
        {
            username.sendKeys(un);
            password.sendKeys(pwd);
            submitBtn.click();
            return new HomePage();
        }
    
    
    
    
    
    
    }
    

    ========================================================================== TESTBASE1

    package com.ebay.qa.base;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;
    
    import com.ebay.qa.util.TestUtil;
    import com.ebay.qa.util.WebEventListener;
    
    public class TestBase1 {
    
        public static WebDriver driver;
        public static Properties prop;
        public  static EventFiringWebDriver e_driver;
        public static WebEventListener eventListener;
    
        public TestBase1(){
            try {
                prop = new Properties();
                FileInputStream ip = new FileInputStream(System.getProperty("user.dir")+ "/src/main/java/com/ebay"
                        + "/qa/config/config.properties");
                prop.load(ip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public static void initialization(){
            String browserName = prop.getProperty("browser");
    
            if(browserName.equals("chrome")){
                System.setProperty("webdriver.chrome.driver","C:\\Users\\littin.jomon\\Documents\\chromedriver_win32\\chromedriver.exe" );  
                driver = new ChromeDriver(); 
            }
            /*else if(browserName.equals("FF")){
                System.setProperty("webdriver.gecko.driver", "/Users/naveenkhunteta/Documents/SeleniumServer/geckodriver"); 
                driver = new FirefoxDriver(); 
            }*/
    
    
            e_driver = new EventFiringWebDriver(driver);
            // Now create object of EventListerHandler to register it with EventFiringWebDriver
            eventListener = new WebEventListener();
            e_driver.register(eventListener);
            driver = e_driver;
    
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
    
            driver.get(prop.getProperty("url"));
    
        }
    
    
    
    
    
    
    
    
    
    }
    

    ========================================================================== console results:

    [RemoteTestNG] detected TestNG version 6.11.0
    Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 22142
    Only local connections are allowed.
    Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    May 02, 2019 2:40:49 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    Before navigating to: 'https://www.ebay.com/signin/'
    Navigated to:'https://www.ebay.com/signin/'
    Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 25701
    Only local connections are allowed.
    Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    May 02, 2019 2:40:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    Before navigating to: 'https://www.ebay.com/signin/'
    Navigated to:'https://www.ebay.com/signin/'
    Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 6048
    Only local connections are allowed.
    Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    May 02, 2019 2:41:00 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    Before navigating to: 'https://www.ebay.com/signin/'
    Navigated to:'https://www.ebay.com/signin/'
    FAILED: EbaylogoimgTest
    java.lang.NullPointerException
        at SigninPageTest.EbaylogoimgTest(SigninPageTest.java:36)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
    
    FAILED: SigninPageTitleTest
    java.lang.NullPointerException
        at SigninPageTest.SigninPageTitleTest(SigninPageTest.java:30)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    

    Got this exception. Have 3 test cases, each one opens the browser but does not execute TC's. Can someone help me on this?



  •     public void setup() {
            initialization();
            SigninPage signinPage = new SigninPage();
        }
    

    Here, you are assigning a SiginPage object to a singinPage local variable. This object is eligible to be destroyed as soon as the method ends.

    What you probably want to do is to assign this object to an attribute.

    this.signinPage = new SigninPage();
    

    Attributes last as long as the object which holds them exists.

    TestNG will instantiate a SigninPageTest object, call each one of the @Test methods, along with the hooks @Before/@After and only after the tests destroy the SigninPageTest object.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2