I have created 2 classes in 2 packages under one project and calling one method of class A into class B by creating object of class but it is giving me null pointer exception below is the code can any one help on this:
package Login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Repository.Objects;
// import Repository.Objects;
public class Loginasa {
public static WebDriver driver;
String baseUrl;
@BeforeSuite
public void setUp() throws Exception
{
System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
driver =new ChromeDriver();
baseUrl= "http://10.7.30.135:9090/xGLinear/login.html";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void Newlogin() throws Exception
{
// driver.get(baseUrl);
driver.findElement(By.id(Objects.Username)).clear();
driver.findElement(By.id(Objects.Username)).sendKeys("ASA");
driver.findElement(By.id(Objects.Password)).clear();
driver.findElement(By.id(Objects.Password)).sendKeys("Abcd1234");
driver.findElement(By.id(Objects.loginbtn)).click();
Thread.sleep(20000);
}
@AfterTest
public void tearDown() throws Exception
{
driver.quit();
System.out.println("Browser Launch");
}
}
Another is:
package Commonmethods;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Login.Loginasa;
public class CreateOrderline {
public static WebDriver driver;
String baseUrl;
@BeforeTest
public void setUp() throws Exception
{
System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
driver =new ChromeDriver();
baseUrl= "http://10.7.30.135:9090/xGLinear/login.html";
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void Orderline () throws Exception
{
driver.get(baseUrl);
Loginasa ASA= new Loginasa();
System.out.println("Browser Launch");
ASA.Newlogin();
}
@AfterTest
public void tearDown() throws Exception
{
driver.quit();
System.out.println("Browser Launch");
}
}
Giving Error: NULL Pointer Exception............
-
Paste your error alsoQAMember– QAMember2017年02月23日 06:12:19 +00:00Commented Feb 23, 2017 at 6:12
-
Please provide your error log or stacktrace so we can identify error line.Sagar007– Sagar0072017年02月23日 06:58:50 +00:00Commented Feb 23, 2017 at 6:58
-
Unclear what you are asking, Please add some more details about your question and queries.Michael Roy– Michael Roy2017年02月23日 07:18:13 +00:00Commented Feb 23, 2017 at 7:18
-
@Rashmi Could you please provide the code which you write in test-ng file. because as per my understanding your webdriver session get closed that why you get the null pointer executionAnkit jain– Ankit jain2017年02月23日 21:50:31 +00:00Commented Feb 23, 2017 at 21:50
-
What IDE are you using to build your tests? I would like to suggest better ways to debug nullpointer exceptions like this.Niels van Reijmersdal– Niels van Reijmersdal2017年02月27日 13:06:20 +00:00Commented Feb 27, 2017 at 13:06
3 Answers 3
I'm not sure it's good design to call one test from another fixture. Had problems with that in the past. You might consider using Page Objects
Anyhow, in your case it looks like you have a NullPointerException because ASA.setUp() isn't called before calling ASA.NewLogin() when the setup method of the referring class is called.
Though it's C# you can find a working proposal of how to organize your test code here.
-
2The problem is that the setup method from Loginasa won't be called automatically from the CreateOrderOnline instance as it's another test suite Maybe if CreateOrderOnline inherited from Loginasa the setup method would be visible to the test runner from a CreateOrderOnline instance.user1130– user11302017年02月23日 07:21:58 +00:00Commented Feb 23, 2017 at 7:21
-
1@BharatMane user1130 is right, the setUp() is not called for instantiated class in your tests. So when you use the "new" keyword the setUp() is not called.Niels van Reijmersdal– Niels van Reijmersdal2017年02月27日 13:03:27 +00:00Commented Feb 27, 2017 at 13:03
No need to use two instances of browser. Just specify one instance and you can use the same instance in a project. You can call the browser object by specifying the classname.objectname. ie,
CreateOrderline.driver.findElement(By.id(Objects.Username)).clear();
CreateOrderline.driver.findElement(By.id(Objects.Username)).sendKeys("ASA");
You could pass the driver when creating the Loginasa
instance:
Loginasa ASA= new Loginasa(driver);
Then you also need to add a constructor to the Loginasa class to pass the driver:
public class Loginasa {
public static WebDriver driver;
public Loginasa(passedDriver) {
driver = passedDriver;
}
}
Now can you remove the setUp()
function from the Loginasa
class, this one is useless anyways cause it is not called by TestNG for that test. TestNG only calls the setUp()
of classes for which it runs the test, not classes used by the test. This is also the reason you get a NullPointer exception, the class has an empty driver
property when you call ASA.Newlogin()
. Also remove all the other annotations from Loginasa
. You shouldn't be using a test-class as a class you are going to instantiate from within another test.
I would have a look at the Page Object Model for TestNG: http://www.ontestautomation.com/using-the-page-object-model-pattern-in-selenium-testng-tests/
Also have a look at how you can start debugging NullPointer exceptions yourself:
Explore related questions
See similar questions with these tags.