简体   繁体   中英

How do i access variable of void method of other class from another class

  Code of Broweserselection.java 


    @SuppressWarnings("serial")
        class Browserselection extends JFrame implements ActionListener
        { 
         public void BrowserSelection1()
         { Some code...}

         @Override
         public void actionPerformed(ActionEvent e)
         {
           WebDriver d1 = null;

           if(FireFox.isSelected())
            {
             dispose();
             System.setProperty("webdriver.gecko.driver","driverPath");
             d1 = new  FirefoxDriver();
            }

          this.setDriver(d1);
         }

        public WebDriver setDriver(WebDriver driver)
        {
          this.driver2 = driver;
          this.getDriver();

          return this.driver2;
        }

        public WebDriver getDriver()
        {
          return this.driver2;          
        }

===========================================================================

Code of MyMainClass.java

    public class MyMainClass{

        public static void main(String[] args) throws InterruptedException, IOException {

            Browserselection bs= new Browserselection();
            bs.BrowserSelection1();
            WebDriver driver=bs.getDriver();
            System.out.println(driver + "Hello from main");
  1. here driver shows null value while i need to access of value of Browserselection.java 's getDriver method's value. I do println Browserselection.java has proper value while in MyMainClass.java bs.getDriver() ; shows null .

  2. is there any way to access d1 variable of actionPerformed nethod's if condition from MyMainClass?

I am new to Java and learning selenium.

Use this method :

public WebDriver getDriver()
    {
      driver2 = new ChromeDriver();
      return this.driver2;          
    }

Note that you have to declare the driver2 in your class like : private WebDriver driver2;

Your whole class would look like this :

public class Browserselection {

   private WebDriver driver2;

    public void BrowserSelection1()
     {
        //some code 
     }

     @Override
     public void actionPerformed(ActionEvent e)
     {
       WebDriver d1 = null;

       if(FireFox.isSelected())
        {
         dispose();
         System.setProperty("webdriver.gecko.driver","driverPath");
         d1 = new  FirefoxDriver();
        }

      this.setDriver(d1);
     }

    public WebDriver setDriver(WebDriver driver)
    {
      this.driver2 = driver;
      this.getDriver();

      return this.driver2;
    }

    public WebDriver getDriver()
    {
      driver2 = new ChromeDriver();
      return this.driver2;          
    }

}

Issue is not in drived selection. it is in architecture of your code. see below code might help you.

@SuppressWarnings("serial")
    class Browserselection extends JFrame implements ActionListener
    { 
     public void BrowserSelection1(String selectedBrower)
     { 
         WebDriver d1 = null;

        if(selectedBrower.equals("firefox")){
            d1 = new  FirefoxDriver();  
        }else if(selectedBrower.equals("firefox")){
            d1 = new  ChromeDriver();   
        }
     }
     /*  
      As per this code , your input from user will be received in this method
     */
     @Override
     public void actionPerformed(ActionEvent e)
     {
       String driverSelected = "";
        if(FireFox.isSelected()){
            driverSelected =  "firefox";
        }else if(Chrome.isSelected()){
            driverSelected =  "chrome";
        }
        Browserselection bs= new Browserselection();
        bs.BrowserSelection1(driverSelected);

     }
}

Main method will be used for just start your project, so no need to create object of Browserselection class in main method, rather create that class object where it actually require.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM