2016-12-06 108 views
0

我正在我的电子商务网站上进行数据迁移测试。比较二维数组并写入一个单元格在excel中的java

情景:需要比较旧网站和新网站中每个父级产品的简单产品列表。

我有一个网页列出了与父产品(可配置产品)相关的所有简单产品skus和价格。

每行显示一个产品SKU,其价格

我需要从新旧页面阵列来获得数据,然后进行比较。 然后在Excel工作表中添加额外\缺少的skus和价格到单元格。

我用硒+ java的这个(Java1.8,塞莱2.52) 我所做的是如下:

1)从2个动态表table1的(旧)和表2(新) 了数据2)采取了更大的阵列,并写了大循环比较大与较小

有没有人建议一个更好的办法吗?

回答

1

我会做这样的事情...

for (each product) 
{ 
    // navigate to old site 
    // verify product name 
    // scrape all product SKUs and create a comma delimited string 
    String oldSkus = "sku1,sku2,sku3"; 
    // scrape all prices and create a comma delimited string 
    String oldPrices = "$1.00,$2.00,$3.00"; 

    // navigate to new site 
    // verify product name 
    // scrape all product SKUs and create a comma delimited string 
    String newSkus = "sku1,sku2,sku3"; 
    // scrape all prices and create a comma delimited string 
    String newPrices = "$1.00,$2.00,$4.00"; 

    // compare old and new SKUs and write to CSV 
    // compare old and new prices and write to CSV 
} 

我会写一行每次比较使用CSV文件|作为分隔符。实际是从新的网站和预期是从旧的。然后您可以将该文件导入到Excel中。一些示例行在下面(带标题)。

Product Name|Validation|Result|Expected|Actual 
Product1|Compare SKUs|PASS|sku1,sku2,sku3|sku1,sku2,sku3 
Product1|Compare prices|FAIL|$1.00,$2.00,$3.00|$1.00,$2.00,$4.00 
Product2|Compare SKUs|PASS|sku1,sku2,sku3|sku1,sku2,sku3 
Product2|Compare prices|FAIL|$1.00,$2.00,$3.00|$1.00,$2.00,$4.00 

将此导入到Excel后,您可以快速筛选以仅显示失败的测试并开始调查。我定期使用非常类似的方法来验证产品是否正确加载到我们的eComm网站上。

0
public static void GetContentFromURL1(String url1) throws IOException { 
    System.out.println(url1); 
    // Open Url1 
    driver.get(url1); 

    // Get the no of rows of data 
    List<WebElement> rows = driver 
      .findElements(By.xpath("html/body/div[2]/div[3]/div[2]/div[3]/div/form/table/tbody/tr")); 
    // print no of rows of data 
    System.out.println("URL 1 : Total number of rows [ Items in the table ] :" + rows.size()); 

    // Get Skus , ItemName and Price row wise and add to 
    // ArrayList->contentOnSite1 
    for (int dataRowOnWebsiteTble = 2; dataRowOnWebsiteTble < rows.size(); dataRowOnWebsiteTble++) { 
     // get no of skus listed 
     int noOfSkusListed = rows.size() - 2;// one row for header and one 
               // for footer 

     // Get Sku value from table - display in console - add it to Array 
     // List 
     String Sku = driver.findElement(By.xpath("html/body/div[2]/div[3]/div[2]/div[3]/div/form/table/tbody/tr[" 
       + dataRowOnWebsiteTble + "]/td[3]")).getText(); 
     // System.out.println(Sku); 
     contentOnSite1.add(Sku); 

     arrayRow++; 
    } 

    // Display the arraylist contents to console 
    System.out.println(Arrays.toString(contentOnSite1.toArray())); 

    // write to excel - write contents of arraylist to a single cell in 
    // excel against corresponding url 
    writeToExcel(2, UrlIndex, Arrays.toString(contentOnSite1.toArray())); 

} 

我写了类似的代码来获取第二个网址的内容。

最后以下方法以除去所有常见的物品和写入到Excel表单

private static void CompareTwoListsandPrintResulttoExcelSheet(List contentOnSite1, List contentOnSite2) { 
    if (contentOnSite1.size() > contentOnSite2.size()) { 
     ListDiff = new ArrayList(contentOnSite1); 
     ListDiff.removeAll(contentOnSite2); 
    } else { 
     ListDiff = new ArrayList(contentOnSite2); 
     ListDiff.removeAll(contentOnSite1); 

    } 
    if (ListDiff.isEmpty()) { 
     result = "Pass : Data and no of items match in two urls"; 
    } else { 
     result = "Fail : Data and no of items does not match in two urls"; 
    } 
    // write to excel - write contents of arraylist to a single cell in 
    // excel against corresponding url 
    writeToExcel(6, UrlIndex, Arrays.toString(ListDiff.toArray())); 

}