簡體   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