简体   繁体   English

如何减少 java 方法中的重复?

[英]How to reduce duplication in java methods?

I have to run my test cases in different environments and generate reports.我必须在不同的环境中运行我的测试用例并生成报告。 I have separate config for each environment to store the urls, username and password and the path for my xl sheet where I put my test results along with screenshots.我对每个环境都有单独的配置来存储 url、用户名和密码以及我的 xl 表的路径,我将测试结果与屏幕截图一起放在其中。 The problem is that for each task like loading the config files, taking screen shots, generating xl reports I have to the same method for each environment (QA, DEV, Production etc) with a different name Bellow is a few methods environment for the two of my environments — I have at least three more and I am basically copying and pasting the same code Just changing the names of the methods.问题在于,对于每个任务,例如加载配置文件、截屏、生成 xl 报告,我必须为每个环境(QA、DEV、生产等)使用相同的方法,但名称不同 Bellow 是两者的一些方法环境我的环境 - 我至少还有三个,我基本上是在复制和粘贴相同的代码只是更改方法的名称。 How would you write these in a better way to be able to reuse the methods — I want to be able to have just one screenshot method and writeToExcel method and be able to use it across my framework.您将如何以更好的方式编写这些以便能够重用这些方法——我希望能够只有一个屏幕截图方法和 writeToExcel 方法,并且能够在我的框架中使用它。 I am working on a cucumber-junit framework.我正在研究一个黄瓜-junit 框架。

public class MainLibrary {

public static Webdriver driver;

public String loadQA_Properties(String property) throws IOException {

Properties p = new Properties();
InputStream input = new FileInputStream(“QA-property-file-path-goes-here”);
pr.load(input);
String propVal = p.getProperty(property);
return propVal;
}

  public String loadDev_Properties(String property) throws IOException {
      Properties p = new Properties();
      InputStream input = new FileInputStream(“Dev-property-file-path-goes-here”);
      pr.load(input);
      String propVal = p.getProperty(property);
      return propVal;
     }
 }

I have 3 more methods of the same type for different environments -- These methods gets called in my utility classes as following:对于不同的环境,我还有 3 个相同类型的方法——这些方法在我的实用程序类中被调用,如下所示:

 public String screenshotQA(WebDriver driver, String ss_name) throws IOException {
      MainLibrary   obj = new  MainLibrary (driver);
      String path = obj.loadQA_Properties("screenshotPath") + ss_name + ".png";
      TakesScreenshot ts = (TakesScreenshot) driver;
      File src = ts.getScreenshotAs(OutputType.FILE);
      File destination = new File(path);
      FileHandler.copy(src, destination);
      return path;
    }
   public String screenshotDev(WebDriver driver, String ss_name) throws IOException {
       MainLibrary   obj = new  MainLibrary (driver);
       String path = obj.loadQA_Properties("screenshotPath") + ss_name + ".png";
       TakesScreenshot ts = (TakesScreenshot) driver;
       File src = ts.getScreenshotAs(OutputType.FILE);
       File destination = new File(path);
       FileHandler.copy(src, destination);
       return path;
     }
   //I am create the same method for all my environment, because of the loadproperty Methods

  public void writeToExcelCellQA(int row, int cell, String data) throws IOException {
        MainLibrary  obj = new MainLibrary (driver);
        XSSFCell cell1;
        FileInputStream fi = new FileInputStream(obj.loadQA_Properties("excelPath"));
        XSSFWorkbook workbook = new XSSFWorkbook(fi);
        XSSFSheet sheet = workbook.getSheet("TestCases");
        cell1 = sheet.getRow(row).createCell(cell);
        cell1.setCellValue(data);
        FileOutputStream fo = new FileOutputStream(obj.loadQA_Properties("excelPath"));
        workbook.write(fo);
        fo.close();
    }
  public void writeToExcelCellDev(int row, int cell, String data) throws IOException {
        MainLibrary  obj = new MainLibrary (driver);
        XSSFCell cell1;
        FileInputStream fi = new FileInputStream(obj.loadDev_Properties("excelPath"));
        XSSFWorkbook workbook = new XSSFWorkbook(fi);
        XSSFSheet sheet = workbook.getSheet("TestCases");
        cell1 = sheet.getRow(row).createCell(cell);
        cell1.setCellValue(data);
        FileOutputStream fo = new FileOutputStream(obj.loadDev_Properties("excelPath"));
        workbook.write(fo);
        fo.close();
     }

All that differs between your methods is the environment.您的方法之间的所有不同之处在于环境。 If we pull that out of the specific code and pass it into a generic method, we can deduplicate all of this.如果我们将其从特定代码中提取出来并将其传递给通用方法,我们就可以对所有这些进行重复数据删除。

Load Properties加载属性

  1. Make a single loadProperties method that you pass the environment into as a string (OR you could store the environment as a global var OR read from some other place).制作一个loadProperties方法,将环境作为字符串传递(或者您可以将环境存储为全局变量或从其他地方读取)。 For the sake of example, I'm pretending it'll be passed in as a string myEnv .例如,我假装它将作为字符串myEnv
  2. InputStream input = new FileInputStream(“QA-property-file-path-goes-here”); becomes InputStream input = new FileInputStream(myEnv + “-property-file-path-goes-here”);变成InputStream input = new FileInputStream(myEnv + “-property-file-path-goes-here”);

Screenshot截屏

  1. Make a single screenshot method that you pass the environment into as a string (OR you could store the environment as a global var OR read from some other place).制作一个screenshot方法,将环境作为字符串传递(或者您可以将环境存储为全局变量或从其他地方读取)。 For the sake of example, I'm pretending it'll be passed in as a string myEnv .例如,我假装它将作为字符串myEnv
  2. String path = obj.loadQA_Properties("screenshotPath") + ss_name + ".png"; becomes String path = obj.loadProperties("screenshotPath", myEnv) + ss_name + ".png";变为String path = obj.loadProperties("screenshotPath", myEnv) + ss_name + ".png";

Write to Excel写入 Excel

  1. Make a single writeToExcel method that you pass the environment into as a string (OR you could store the environment as a global var OR read from some other place).制作一个writeToExcel方法,将环境作为字符串传递(或者您可以将环境存储为全局变量或从其他地方读取)。 For the sake of example, I'm pretending it'll be passed in as a string myEnv .例如,我假装它将作为字符串myEnv
  2. FileInputStream fi = new FileInputStream(obj.loadDev_Properties("excelPath")); becomes FileInputStream fi = new FileInputStream(obj.loadProperties("excelPath", myEnv));变为FileInputStream fi = new FileInputStream(obj.loadProperties("excelPath", myEnv));
  3. FileOutputStream fo = new FileOutputStream(obj.loadDev_Properties("excelPath")); becomes FileOutputStream fo = new FileOutputStream(obj.loadProperties("excelPath", myEnv));变为FileOutputStream fo = new FileOutputStream(obj.loadProperties("excelPath", myEnv));

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

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