jimbacker
jimbacker

Reputation: 31

Can not Pass Selenium WebDrivers between two classes using Java on Eclipse

I am new to Selenium Webdriver and I am having issues passing drivers between classes. The driver class uses a switch case to determine which driver the user wants to use, Firefox or Chrome. Once the user picks one, the correct driver loads and the user is asked if they want to run Facebook or Twitter. When I select either the driver returns null instead of going to the correct URL. 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.(social.java:13) at

selenium.driver.(driver.java:23) at

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

Below is the code used:

MAIN CLASS

  package selenium;
import java.util.Scanner;
public class main {
    private static int bflag = 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;}}

DRIVER CLASS

    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 {
    
    private static int socialflag = 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()

  {
    return driver;
  }}

SOCIAL CLASS

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");  
    }}

Upvotes: 0

Views: 161

Answers (1)

jimbacker
jimbacker

Reputation: 31

I figure it out.

'driver = new ChromeDriver();' instead of 'ChromeDriver driver = new ChromeDriver();' works.

Upvotes: 1

Related Questions