appium / java-client

Java language binding for writing Appium Tests, conforms to W3C WebDriver Protocol
Apache License 2.0
1.21k stars 755 forks source link

Unable to initialize the WebElement of Object Repository using "PageFactory.initElements(driver, this);" in constructor, Throwing an java.lang.NullPointerException #1348

Open vaibhav01M opened 4 years ago

vaibhav01M commented 4 years ago

Description

Unable to initialize the WebElement of Object Repository using "PageFactory.initElements(driver, this);" in constructor in Page Object class which inherits the driver from the base class, Throwing an java.lang.NullPointerException

Environment

Details

Code throws java.lang.NullPointerException as Web Elements are not initializing using "PageFactory.initElements(driver, this);" method in Page Object class

Code To Reproduce Issue [ Good To Have ]

You can access the code from following link - https://github.com/vaibhav01M/sampleNewDemoTours.git

Exception Stacktraces

[RemoteTestNG] detected TestNG version 6.14.3 Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 8988 Only local connections are allowed. May 17, 2020 2:53:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS PASSED: verifyWelcomePage FAILED: verifyMercuryLogo 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.$Proxy9.isDisplayed(Unknown Source) at pageobjects.WelcomePage.validateMercuryLogo(WelcomePage.java:33) at testscript.WelcomePageTest.verifyMercuryLogo(WelcomePageTest.java:31) 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:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:583) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) 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)

=============================================== Default test Tests run: 2, Failures: 1, Skips: 0

=============================================== Default suite Total tests run: 2, Failures: 1, Skips: 0

ahmadsayeed3 commented 4 years ago
System.setProperty("webdriver.chrome.driver", "F:\\Java Basic\\DemoProjectWorkspace\\.metadata\\sample\\Driver_Executables\\chromedriver.exe");
        driver = new ChromeDriver();    
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://newtours.demoaut.com/");

The code you have written here is to open a browser in the PC/Mac. This is not the way to launch browser in a mobile device.