2016-12-06 39 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())); 

}