import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Created by Mridul on 1/14/2016.
*/
public class ReportParser {
private WebDriver driver;
private WebDriverWait wait;
ReportParser(){
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
this.driver = new ChromeDriver();
driver.manage().window().maximize();
wait=new WebDriverWait(driver,10);
}
public WebDriver getDriver(){
return driver;
}
public WebDriverWait getWaitDriver(){
return wait;
}
private void printMap(Map<String,List<String>> testMap){
System.out.println("Printing values in map!");
for(Map.Entry entry: testMap.entrySet()){
System.out.println("The Defect is :" + entry.getKey());
System.out.println("The associated tests are :");
for(String value : (List<String>)entry.getValue()){
System.out.println(value);
}
System.out.println("\n");
}
System.out.println("The no of tests are : " + testMap.size());
}
private Map<String,List<String>> storeTestsToMap(List<WebElement> elements, Map<String, List<String>> testMap){
for(WebElement element: elements){
//WebElement anchor=element.findElement(By.tagName("a"));
WebElement anchor=wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("a")));
//retrieve key
String fullKey=anchor.getAttribute("title");
String key=(fullKey.contains("Exception"))? fullKey.substring(0,fullKey.indexOf("Exception")) : fullKey;
//System.out.println(key);
//retrieve value
String fullValue=anchor.getText();
String value=fullValue.substring(fullValue.indexOf("Shopping"), fullValue.length());
//System.out.println(value+"\n");
//store key value into map based on unique key
List<String> values;
if(testMap.containsKey(key)){
values=testMap.get(key);
}else {
values=new ArrayList<String>();
}
values.add(value);
testMap.put(key, values);
}
return testMap;
}
public static void main(String... args){
try {
int displaySize, totalCount, currentCount;
Map<String, List<String>> failureTestsMap=new WeakHashMap<String, List<String>>();
Map<String, List<String>> errorTestsMap=new WeakHashMap<String, List<String>>();
ReportParser reportParser;
WebDriver driver;
WebDriverWait wait;
reportParser = new ReportParser();
driver = reportParser.getDriver();
wait=reportParser.getWaitDriver();
driver.get("http://localhost:63342/ais-parent/ais-dotcom/target/site/thucydides/index.html");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
do {
//WebElement testTable = driver.findElement(By.id("test-results-table"));
//List<WebElement> elems_failureTest = testTable.findElements(By.className("FAILURE-text"));
WebElement testTable = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test-results-table")));
//List<WebElement> elems_failureTest = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("FAILURE-text")));
List<WebElement> elems_failureTest = testTable.findElements(By.className("FAILURE-text"));
//System.out.println("Failure tests size : " + elems_failureTest.size());
failureTestsMap = reportParser.storeTestsToMap(elems_failureTest,failureTestsMap);
//reportParser.printMap(failureTestsMap);
List<WebElement> elems_errorTest = testTable.findElements(By.className("ERROR-text"));
//System.out.println("Error tests size : " + elems_errorTest.size());
errorTestsMap = reportParser.storeTestsToMap(elems_errorTest, errorTestsMap);
//reportParser.printMap(errorTestsMap);
//navigate to the drop down element
//Select sel_ResultSize = new Select(driver.findElement(By.name("test-results-table_length")));
Select sel_ResultSize = new Select(wait.until(ExpectedConditions.presenceOfElementLocated(By.name("test-results-table_length"))));
displaySize = Integer.parseInt(sel_ResultSize.getFirstSelectedOption().getText());
//System.out.println("Size : " + displaySize);
//retrieve the total tests
//String totalTests = driver.findElement(By.id("test-results-table_info")).getText();
String totalTests = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test-results-table_info"))).getText();
String count = totalTests.substring(totalTests.indexOf("to"), totalTests.indexOf("of"));
currentCount = Integer.parseInt(count.substring(count.length() - 3, count.length() - 1));
//System.out.println("currentCount : " + currentCount);
String finalCount = totalTests.substring(totalTests.indexOf("of"), totalTests.indexOf("entries"));
totalCount = Integer.parseInt(finalCount.substring(finalCount.length() - 3, finalCount.length() - 1));
//System.out.println("totalCount : " + totalCount);
//click on the next button
//WebElement elem_NextButton=driver.findElement(By.id("test-results-table_next"));
WebElement elem_NextButton=wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test-results-table_next")));
elem_NextButton.click();
} while (currentCount < totalCount);
reportParser.printMap(failureTestsMap);
System.out.println("The no of failure tests are : " + failureTestsMap.size());
reportParser.printMap(errorTestsMap);
System.out.println("The no of error tests are : "+errorTestsMap.size());
}finally {
//driver.quit();
}
}
}
Wednesday, March 23, 2016
Webdriver Parser to retrieve information from an HTML report - direct access was not available to use listeners
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment