0

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)
James Z
  • 12,209
  • 10
  • 24
  • 44
Tawheed
  • 31
  • 4
  • 1
    Possible duplicate of [UnreachableBrowserException: Could not start a new session when trying to open web page on firefox through Java Selenium](https://stackoverflow.com/questions/45374301/unreachablebrowserexception-could-not-start-a-new-session-when-trying-to-open-w) – undetected Selenium Feb 05 '18 at 16:42

1 Answers1

2

That is because of the mistake you have done in your code.

I didn't understand why you have declared driver in 3 places in your code. Whenever you declare same variable again and again its previous value get override every time and that is the reason you are getting NullPointerException.

I have modified your code and verified the working also.

   public class Browserfactory {

    public static WebDriver startBrowser(WebDriver driver,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;
    }
}

public class Guru99 {
    // creating a guru99 bank page login verify.
    public WebDriver driver = null;
    private static final String URL = "https://demo.guru99.com/v4";

    @BeforeTest
    // pre-requisition
    public void launchpage() {
        driver = Browserfactory.startBrowser(driver,"firefox");
        driver.manage().window().maximize();
        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();
    }

}

Hope this will help you.

Pradeep hebbar
  • 2,147
  • 1
  • 9
  • 14
  • Yes , solved i didnt notice actually , i thought it will work, and surprise i wasted 2 hour to get resolve but i couldnt. – Tawheed Feb 05 '18 at 18:16