This is Parent Class to run the ChromeDriver.
package logInCredit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LogInPage {
public WebDriver driver ;
public void LogInCredit() {
//Open ChromeDriver
WebDriver driver= new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
System.out.println(driver);
}
}
This Is Child Class which takes the url from Parent Class
package afterLogIn;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import logInCredit.LogInPage;
public class NextLogIn extends LogInPage {
@Test
public void DashboardPage() {
System.out.println("driver is " driver);
//Admin UserName Enter
WebElement AdminUserName =
driver.findElement(By.xpath("//input[@name='txtUsername']"));
AdminUserName.sendKeys("Admin");
//Admin Password Enter
WebElement AdminPassword =
driver.findElement(By.xpath("//input[@name='txtPassword']"));
AdminPassword.sendKeys("admin123");
//click Login
WebElement LogInButton = driver.findElement(By.xpath("//input[@id='btnLogin']"));
LogInButton.click();
WebElement ClickAdmin = driver.findElement(By.xpath("//*[text()='Admin']"));
ClickAdmin.click();
WebElement ClickPIM = driver.findElement(By.xpath("//*[text()='PIM']"));
ClickPIM.click();
}
}
This is my testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="After Login Test">
<classes>
<class name="afterLogIn.NextLogIn"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
While running the program I get the Exception
[RemoteTestNG] detected TestNG version 7.4.0
driver is null
FAILED: DashboardPage
java.lang.NullPointerException
at afterLogIn.NextLogIn.DashboardPage(NextLogIn.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
The Webdriver url from the parent class is not passing to the child class. Is their any Issues with my code. When I run the parent class with main method it navigate the browser to the url but when I call from the child class it not giving the same url.
CodePudding user response:
the class member driver is not instantiated, in the method logInCreated() in the parent class you are using another object reference ( not the class member ).
in the LoginPage add a no arg constructor in wich you instntialize driver
public LoginPage(){ driver = new ChromeDriver(); }
and in the method LogInCredit() delete the line WebDriver driver= new ChromeDriver();