I'm new to selenium framework, i am trying to create a framework and started my steps towards learning framework. I have written a code for login page but here i'm getting "nullPointerException". Anyone's help would be better. Thanks in advance.

- 83
- 1
- 2
- 13
-
Use `Inheritance` concept inside login page class. Because you have set up your browsers in another class file, so you have to class that file from login page. – Jainish Kapadia Mar 17 '17 at 05:08
-
can you suggest me, what i have to do in login page class. What changes make in login page will be useful ? – user3782636 Mar 17 '17 at 05:16
-
you are not calling the startBrowser method at all. – shank087 Mar 17 '17 at 05:17
-
`public class LoginPage extends Utility` Before Going with selenium first you should learn basic core concepts, then only you will be able to create effective test automation script. Take this comment as an advice :) it will help you. – Jainish Kapadia Mar 17 '17 at 05:37
-
Yes Sure, Thank you. – user3782636 Mar 17 '17 at 06:12
4 Answers
Your driver is not initialised ,for which the code you have written in Utility Class, you can extend UtlitlityClass in your LoginPage and use @BeforeMefthod in LoginPage where you can create an instance of driver and use it in your test method and @AfterMethod to destroy the same.

- 1,358
- 14
- 21
-
I have updated my code like this.still getting the same exception. – user3782636 Mar 17 '17 at 06:40
-
This is because of Declaring the WebDriver driver; in both parent class and child class.
Example : 1.created one base class and in base class declaring the Webdrievr; 2.Created child class and declare the webdriver in child class. 3.child class extends base class. this issue comes.
So declare the WebDriver driver in either Base class or Child Class.

- 439
- 4
- 10
Better place the editable code so that it can get rectified; 1. Remove static from static webdriver driver in Utility 2. Remove webdriver declared in Login page
Question: Does base url launches homepag or signin page
If home page then
1. Create a home page class
2. In utility- make this asignment --
Homepage homepgobj = driver.get(url) ;
return homepgobj;
and replace void with text Homepage.
3. Now in Homepage Create method to navigate to Login page using pagefactory In
public LogInPage navigateToLoginPage(){
driver.navigate().to("Loginpage url");
LoginPage logInPageObj =
PageFactory.initElements(driver, LoginPage.class);
return loginPageObj;
}
4. On homepage create const. to driverobj just like done in login page but do not place pagefactory in it
5. Also remove page factory element from const. of Signin page
6. Create login page test class to write tests
. Now while writin test navigate to signin page like this
Homepage homepgObj = startBrowser(browser, url) --
--this will navigate to homepage
Login loginpgObj =
homepgObj.callmethodcreatedinhomepage] --
--
will navigate to signin page.
loginpgObj.[Call methods of loginpg]

- 11
- 1
- 4
I was also facing the same problem. As I am using BaseClass (parent class) for my tests where I declared WebDrive driver and mistakenly I also declared Webdriver in my test class (child class). It was making a duplicated declaration of the driver. Just removing, WebDriver driver; from child class, this issue resolved

- 143
- 2
- 5