How to resolve below exception.
Here is code
package com;`
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Browserfactory {
static WebDriver driver;
public static WebDriver startBrowser(String browserName)
{
if(browserName.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.firefox.marionette","geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
driver = new ChromeDriver();
}
return driver;
}
}
Accessing from another class. below code is written in different class and accessing this as a Base class.
i wanted to make Logout method as utility, when ever required to logout just call it and get done.
package com;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Guru99 {
//creating a guru99 bank page login verify.
public WebDriver driver=null;
private static final String URL="http://demo.guru99.com/v4/";
@BeforeTest
//pre-requisition
public void launchpage()
{
WebDriver driver = Browserfactory.startBrowser("firefox");
driver.get(URL);
driver.findElement(By.name("uid")).sendKeys("mngr117051");
driver.findElement(By.name("password")).sendKeys("EhYtErY");
driver.findElement(By.name("btnLogin")).click();
}
@Test
public void logout()
{
driver.findElement(By.xpath("html/body/div[2]/div/ul/li[15]/a")).click();
}
}
//Running code through TestNG.
Getting Error
FAILED: logout
java.lang.NullPointerException
at com.Guru99.logout(Guru99.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)