我如何从另一个类访问其他类的void方法的变量

时间:2018-05-09 17:25:27

标签: java selenium selenium-webdriver

  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. 此处驱动程序显示null值,而我需要访问Browserselection.java getDriver方法值的值。在println Browserselection.java时,我MyMainClass.java bs.getDriver()具有适当的价值;显示null

  2. 有没有办法从myMainClass访问actionPerformed nethod' s的d1变量?

  3. 我是Java新手,学习selenium。

2 个答案:

答案 0 :(得分:1)

使用此方法:

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

请注意,您必须在类中声明driver2,如:private WebDriver driver2;

你的全班看起来像这样:

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

}

答案 1 :(得分:1)

问题不在于选择。它是在您的代码的架构中。 看下面的代码可能对你有帮助。

@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方法中创建Browserselection类的对象,而是创建实际需要的类对象。

希望这会对你有所帮助。