2017-10-18 50 views
0

我正在尝试为网页创建一个自动测试。网页有一个过滤器表格和一个表格。表格有5-6种不同的表示数据的方式。在硒测试中普及获取操作

我已经为每种数据展现方式创建了PageObjects(所有表格都只有不同的列)。现在我有了这个通用的Pageobject,它可以找到表并询问它的行。

但我无法得到代码的pece来获取表的所有行。

public <T> List<T> getAllRows(){ 
     List<AbstractTableRow> allRows = table.findElements(By.xpath("//tr[@role='row']").className("jqgrow")).stream() 
      .map(AbstractTableRow::new).collect(Collectors.toList()); 
     if(allRows != null) { 
      return (List<T>)allRows; 
     } 

     return null; 
    } 

AbstractTableRow是所有其他行的父项。但它并不是真正的抽象类(我尝试过这种方式,最终导致了泛型的混乱业务+没有最终工作的反射)。所以,现在我需要某种方式来贬低父母对孩子的看法,或者有人可以举一个例子来说明可能会起作用的反思和泛型(然后我可以再次抽象AbstratTableRow抽象)。所有tablerows进来类型WebElement和所有rowobject有

public SomeRow(WebElement element) 

的构造。方法需要返回List,其中SomeRow是5-6行类型之一。

回答

0

听起来就像你需要一个工厂类,它产生实现TableRow接口(或扩展抽象AbstractTableRow类)的对象。

这通常是这样做:

public interface TableRow { 
    /*methods..*/ 
} 

public class NiceRow implements TableRow { 
    public NiceRow(WebElement element) { 
    //implementation 
    } 
    /*Implementation..*/ 
} 

public class CuteRow implements TableRow { 
    public CuteRow(WebElement element) { 
    //implementation 
    } 
    /*Implementation..*/ 
} 

public class TableRowFactory { 
    public static TableRow getRow(WebElement element) { 
    switch(element.getAttribute("class")) { 
     case "nice": 
     return new NiceRow(element); 
     /*...*/ 
    } 
    } 
} 

现在,你可以得到一个工厂对象,并用它制作行:

TableRowFactory factory = new TableRowFactory(); 
List<TableRow> allRows = 
    table.findElements(By.xpath("//tr[@role='row']")).stream() 
     .map(element -> factory.getRow(element)) 
     .collect(Collectors.toList());  

编辑: 请注意的getAttribute( “class”)很可能会产生比单个类更多的东西,并且仅在我的代码中用于区分类之间的示例。

0

您可以通过concrete SomeRow type的等级。此外,您可以将RowTable切换为抽象,其中将包含带有WebElement的构造函数作为将在子类型中重载的单个参数。 (我认为这已经存在)。

public static <T extends RowTable> List<T> getTableData(Class<T> clz) throws Exception { 

      List<WebElement> elems = .....Your query....; 
      Constructor<T> cont = clz.getConstructor(WebElement.class); 

      List<T> data = elems.stream().map(e -> { 
       T type = null; 
       try { 
        type = cont.newInstance(e); 
       } catch (Exception e1) { 
        throw new RuntimeException(e1); 
       }   
       return type; 
      }).collect(Collectors.toList()); 

      return data; 
     } 

这样称呼它 - getTableData(ConcreteRow.class)

+0

似乎是同样的结果可以只是做可以实现“地图(ConcreteRow ::新)”从问题的代码。有些混淆了所有这些问题(不是很通用的通用+反射)逻辑解决了哪些问题? – Kudin