Reputation: 11
In below LogOutfunc class, I have created object for the class LoginPage and when I run LogOutfunc class program, Launch() method is passed. However, the LogOut() method fails with java.lang.NullPointerException.
After login(), the url navigates to a page which contains the objects present in the LogOut() method but it doesn't identify the object.Suggest me a solution of how to use the driver instance in the current class.
1. LogOutfunc Class
package TestNG;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class LogOutfunc {
public WebDriver driver;
@Test()
public void Launch() throws NoAlertPresentException, InterruptedException {
LoginPage tc1 = new LoginPage();
tc1.setUp();
tc1.Login("test_Sharmila","Welcome123","Sharmila");
}
@Test()
public void LogOut() {
System.out.println("Logout Functionality");
driver.findElement(By.xpath("//img[@id=\"usrmenu\"]]")).click();
driver.findElement(By.xpath("//span[@class='logoutx']//a[text()='Logout']")).click();
System.out.println("Logged out from the page");
2. LoginPage class
package TestNG;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class LoginPage {
String driverPath = "C:\\Selenium\\Driver\\chromedriver.exe";
public WebDriver driver;
String baseUrl = "https://www.myguruavatar.com/demo";
String expectedtUrl = "https://www.myguruavatar.com/demo/index.php?option=com_avatarprofile&view=avatarprofileform";
@BeforeTest
public void setUp() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
}
@Test
public void Login(String userName, String password, String Name)
throws NoAlertPresentException, InterruptedException {
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id=\"modlgn-username\"]")).sendKeys(userName);
driver.findElement(By.xpath("//input[@id=\"modlgn-passwd\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"login-form\"]/fieldset/div/p[2]/input")).click();
driver.findElement(By.xpath("//input[@id=\"hmelogin\"]")).click();
if (driver.getCurrentUrl().contains(expectedtUrl)) {
System.out.println("Login Success");
} else {
System.out.println("Login Failure");
please let me know how to use the driver instance in the current class.
Upvotes: 1
Views: 924
Reputation: 3927
Its because driver is null. See here
public class LogOutfunc {
public WebDriver driver;
You are driver is null in this class. Launch method will work as there is valid driver in that class.
So you need to change the way of implementation.
simple way is LogOutfunc extends LoginPage and delete this line public WebDriver driver; in LogoutFun class.
Upvotes: 1