Passing Drivers between classes in Selenium WebDriver using Java on Eclipse

Hello,

I am new to Selenium Webdriver and I am having issues passing drivers between classes. It is returning null every time I pass. I was wondering if anyone could help.  Below is the error I’m getting. I am running this on Eclipse using Java.

 

Exception in thread “main” java.lang.NullPointerException

at selenium.social.facebook(social.java:32)

at selenium.social.<init>(social.java:13)

at selenium.driver.<init>(driver.java:23)

at selenium.main.main(main.java:19)

 

Here is the code:

 

package selenium;

import java.util.Scanner;

public class main {

privatestaticintbflag = 0;

public static void main(String[] args) {

// TODO Auto-generated method stub

  System.out.println("Which browser would you like to run");

  System.out.println("1. Chrome ");

  System.out.println("2. Firefox");

    Scanner input = new Scanner(System.in);

    bflag = input.nextInt();

    driver frame =new driver();

}

  public static int getFlagValue()

  {

    return bflag;

  }

}

 

package selenium;

import java.util.Scanner;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class driver {

privatestaticintsocialflag = 0;

public static WebDriver driver;

public driver() {

    switch (main.getFlagValue()) {

      case 1:

        chrome();

        socialsite();

        social frame = new social();

        break;

      case 2:

        firefoxdriver();

        socialsite();

        social frame1 = new social();

        break;

      default:

        break;

    }

    }

public void chrome()

{

System.setProperty("webdriver.chrome.driver", "/Users/lcren1026/Drivers/chromedriver");

ChromeDriver driver = new ChromeDriver();

}

public  void firefoxdriver()

{

      //Points to the browser driver

System.setProperty("webdriver.gecko.driver", "/Users/lcren1026/Drivers/geckodriver");  

FirefoxDriver driver = new FirefoxDriver();

}

public void socialsite()

{

System.out.println("Which social site would you like to run with the browser selected?");

  System.out.println("1. Facebook ");

  System.out.println("2. Twitter");

    Scanner input = new Scanner(System.in);

    socialflag = input.nextInt();

}

  public static int getSFlagValue()

  {

    return socialflag;

  }

  public static WebDriver getDriverFlagValue()

  {

returndriver;

  }

}

 

package selenium;

import org.openqa.selenium.WebDriver;

public class social {

public social() {

    switch (driver.getSFlagValue()) {

      case 1:

        facebook();

        break;

      case 2:

        twitter();

        break;

      default:

        break;

    }

    }

public void facebook()

{

//WebDriver drive = driver.getDriverFlagValue();

driver.getDriverFlagValue().navigate().to( "www.facebook.com");

}

public void twitter()

{

//WebDriver drive = driver.getDriverFlagValue();

driver.getDriverFlagValue().navigate().to( "www.twitter.com");

}

}

Default Asked on August 24, 2018 in Selenium WebDriver.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.