简体   繁体   English

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

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

3 weeks of experience with Java here. 这里有3周的Java经验。 I have these two classes - AppTest and AppTest2 and I have the same code in both of them: 我有这两个类 - AppTest和AppTest2,我在这两个类中都有相同的代码:

Here is my code: 这是我的代码:

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

     }
}

How can I take this code out (setUp()), make it a class and then pass those variables to the next void "twoUsersSignUp" 我如何取出这个代码(setUp()),使它成为一个类,然后将这些变量传递给下一个void“twoUsersSignUp”

EDIT: Im not looking for automatic solution, I just want to refactor this, so I dont have the same code in both classes 编辑:我不是在寻找自动解决方案,我只是想重构这个,所以我在这两个类中都没有相同的代码

EDIT2: After I implemented the accepted answer's solution, I now have a problem with passing the variable "driver" to the next method in the first class: 编辑2:在我实现了接受的答案的解决方案后,我现在遇到了将变量“driver”传递给第一个类中的下一个方法的问题:

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

How do I do that? 我怎么做?

EDIT3: This is the @AfterClass solution: EDIT3:这是@AfterClass解决方案:

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

EDIT4: actually EDIT3 doesnt work, it just hides the errors from Eclipse. EDIT4:实际上EDIT3不起作用,它只是隐藏了Eclipse中的错误。 I still can't access "driver" :( 我仍然无法访问“驱动程序”:(

EDIT5: I decided that I dont need to have it in an AfterClass TestNG annotation, so I removed all the unnecessary stuff and it now looks like this: EDIT5:我决定我不需要在AfterClass TestNG注释中使用它,所以我删除了所有不必要的东西,它现在看起来像这样:

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

and the variable has been declared this way: 并且变量已经这样声明:

public static WebDriver driver;

but still it doesnt work 但它仍然没有用

EDIT6: fixed this by actually calling the method in the test code. EDIT6:通过实际调用测试代码中的方法来解决这个问题。 Beforehand I didnt have to call it, because testng.xml had it called, but after I removed the @AfterTest annotation, it had been excluded from there! 之前我没有必要调用它,因为testng.xml已经调用它,但是在我删除了@AfterTest注释之后,它已被排除在那里!

You cannot convert a method to a class, but you can move a method to a place from which it would be shared by both Apptest and AppTest2 : create a base class, and make the Apptest and AppTest2 classes extend it. 您无法将方法转换为类,但您可以将方法移动到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 {
        ...
    }
}

Now the code of the setUp method does not need to be repeated, and it uses the implementation of the twoUsersSignUp method provided in each of the two subclasses of AbstractAppTest . 现在不需要重复setUp方法的代码,它使用AbstractAppTest的两个子类中提供的twoUsersSignUp方法的实现。

You can't just convert a method to a class. 您不能只将方法转换为类。

However, you can create new objects or modify existing objects. 但是,您可以创建新对象或修改现有对象。

Initialize your testData class like this 像这样初始化你的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;
 }
}

And then use that object in your test classes 然后在测试类中使用该对象

    public class Apptest/AppTest2 {

     @Test
     public void twoUsersSignUp() throws InterruptedException{

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

     }

    }

The kind of refactoring you are looking for does not exists yet , at least on Eclipse. 您正在寻找的重构类型尚不存在 ,至少在Eclipse上是这样。

A workaround to do it manually is explained here 这里解释了手动执行此操作的解决方法

By the way, in Eclipse by pressing ALT SHIFT T you will find all the current available possibilities to refactor your existent code, by extracting methods, classes etc. 顺便说一句,在Eclipse中按下ALT SHIFT T,你会发现通过提取方法,类等来重构现有代码的所有当前可能性。

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

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