繁体   English   中英

Java:如何将方法重构为类,然后在其他类中使用其参数?

[英]Java: How can I refactor a method to a class and then use its parameters in other classes?

这里有3周的Java经验。 我有这两个类 - AppTest和AppTest2,我在这两个类中都有相同的代码:

这是我的代码:

public class Apptest/AppTest2 {
     public WebDriver driver;
     public WebDriverWait wait;

     @DataProvider(name = "dataProvider")
     public Object[][] setUp() throws Exception {
     File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
     FirefoxBinary ffox = new FirefoxBinary(firefoxPath);
     ffox.setEnvironmentProperty("DISPLAY", ":20");
     driver = new FirefoxDriver(ffox, null);
     wait = new WebDriverWait(driver, timeoutInSeconds );
     Object[][] data = new Object[1][2];
     data[0][0] = driver;
     data[0][1] = wait;
     return data;
 }

  @Parameters({ "driver", "wait" })
  @Test(dataProvider = "dataProvider")
  public void twoUsersSignUp(WebDriver driver, WebDriverWait wait) throws InterruptedException{

       //test here

     }
}

我如何取出这个代码(setUp()),使它成为一个类,然后将这些变量传递给下一个void“twoUsersSignUp”

编辑:我不是在寻找自动解决方案,我只是想重构这个,所以我在这两个类中都没有相同的代码

编辑2:在我实现了接受的答案的解决方案后,我现在遇到了将变量“driver”传递给第一个类中的下一个方法的问题:

     @AfterClass
     public void quit () {
         driver.quit();
     }

我怎么做?

EDIT3:这是@AfterClass解决方案:

     @SuppressWarnings("deprecation")
     @Configuration 
     @AfterClass
     public static void quit (@Optional WebDriver driver) {
         driver.quit();
     }

EDIT4:实际上EDIT3不起作用,它只是隐藏了Eclipse中的错误。 我仍然无法访问“驱动程序”:(

EDIT5:我决定我不需要在AfterClass TestNG注释中使用它,所以我删除了所有不必要的东西,它现在看起来像这样:

     public static void quit (WebDriver driver) {
         driver.quit();
     }

并且变量已经这样声明:

public static WebDriver driver;

但它仍然没有用

EDIT6:通过实际调用测试代码中的方法来解决这个问题。 之前我没有必要调用它,因为testng.xml已经调用它,但是在我删除了@AfterTest注释之后,它已被排除在那里!

您无法将方法转换为类,但您可以将方法移动到ApptestAppTest2共享的AppTest2 :创建基类,并使ApptestAppTest2扩展它。

public abstract class AbstractAppTest {
    public WebDriver driver;
    public WebDriverWait wait;

    @DataProvider(name = "dataProvider")
    public Object[][] setUp() throws Exception {
        File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
        FirefoxBinary ffox = new FirefoxBinary(firefoxPath);
        ffox.setEnvironmentProperty("DISPLAY", ":20");
        driver = new FirefoxDriver(ffox, null);
        wait = new WebDriverWait(driver, timeoutInSeconds );
        Object[][] data = new Object[1][2];
        data[0][0] = driver;
        data[0][1] = wait;
        twoUsersSignUp(data);
        return data;
    }
    public abstract void twoUsersSignUp(@Optional Object[][] data) throws InterruptedException; 
}
public class Apptest extends AbstractAppTest {
    public void twoUsersSignUp(@Optional Object[][] data) throws InterruptedException {
        ...
    }
}
public class AppTest2 extends AbstractAppTest {
    public void twoUsersSignUp(@Optional Object[][] data) throws InterruptedException {
        ...
    }
}

现在不需要重复setUp方法的代码,它使用AbstractAppTest的两个子类中提供的twoUsersSignUp方法的实现。

您不能只将方法转换为类。

但是,您可以创建新对象或修改现有对象。

像这样初始化你的testData类

public class ApptestData{
 public WebDriver driver;
 public WebDriverWait wait;

 public ApptestData() throws Exception {
 File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
 FirefoxBinary ffox = new FirefoxBinary(firefoxPath);
 ffox.setEnvironmentProperty("DISPLAY", ":20");
 driver = new FirefoxDriver(ffox, null);
 wait = new WebDriverWait(driver, timeoutInSeconds );
 Object[][] data = new Object[1][2];
 data[0][0] = driver;
 data[0][1] = wait;
 twoUsersSignUp(data);
 return data;
 }
}

然后在测试类中使用该对象

    public class Apptest/AppTest2 {

     @Test
     public void twoUsersSignUp() throws InterruptedException{

       AppTestData data = new AppTestData();
       //test here

     }

    }

您正在寻找的重构类型尚不存在 ,至少在Eclipse上是这样。

这里解释了手动执行此操作的解决方法

顺便说一句,在Eclipse中按下ALT SHIFT T,你会发现通过提取方法,类等来重构现有代码的所有当前可能性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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