TESTEVERYTHING

Friday 17 July 2015

WebDriver compare data/css between two pages

Hi All,

In Web based project like implemented in CQ5 where we have to check same site for different country. For this we have to rollout/replicate some/all pages for different countries OR I can say downloaded/uploaded the production data in testing environment or vice versa.
After this we have to validate page static data (like text) with production site Data, page wise validation.
E.g.  One page of US site and replicate the same page for another country then verify page content or css
For this we have to check manually, we can automate the same. I assume both pages having same text content (English).
Logic to implement this:
First we have to analyze page objects properties (what are the common page objects (elements) properties are being used) like class, id, type etc and create array of this.
Then we have to get all elements from the base page(expected) using xpath(//*)
For each element get all properties and value using java script and store only those element properties/value which are matching with common page objects (elements) properties stored in array.
Create locators for those elements by generating xpath for them.
Now same locator(xpath) should be exist in actual page
open two driver instances driver1 for page1 , driver2 for another country page2
Using WebDriver find element by xpath(locator) from base page and same for another page, match the text using gettext() method if  same element present in both pages
We can use same for CSS attribute values check for two pages having same element with same type of properties(class, ID etc)

Here is the code to implement this.


public static List<String> getPageLocatorsxPath(WebDriver driverbase,String pageURL )
{
List<String> strLocatorxPath = new ArrayList<>();
/// Common attributes from base site pages
String[] attributes = { "class", "data-class", "data-class-name",
"data-tab", "id", "name", "data-countrycode" };

HashMap<String, String> mapWeblementAttributes = new HashMap<String, String>();

JavascriptExecutor js = (JavascriptExecutor) driverbase;

/// get all elements from page
List<WebElement> childElementsGSAM = driverbase.findElements(By
.xpath("//body//*"));
int counter=0;
for (WebElement webElement : childElementsGSAM) {

// check only those element which are visible and displayed
if (webElement.isDisplayed() & webElement.isEnabled()) {

mapWeblementAttributes.clear();
// get element tag name use for creating Xpath
String strTagName = webElement.getTagName();
String strAttributes = (String) js
.executeScript(
"var str = '', obj = arguments[0].attributes; for (var key in obj){if(obj.hasOwnProperty(key)){str = str + '~' + obj[key].name + '=' + obj[key].value;}}; return str;",
webElement);
// store element properties and value in map
String[] arrAttValues = strAttributes.split("~");

for (String attribute : attributes) {
for (int k = 0; k < arrAttValues.length; k++) {
String[] arrAttrValue = arrAttValues[k].split("=");
if (arrAttrValue.length == 2) {
String strAttr = arrAttValues[k].split("=")[0];
String strAttrValue = arrAttValues[k].split("=")[1];

// store only if element property match with defined properties
if (attribute.equals(strAttr)) {
mapWeblementAttributes.put(strAttr,
strAttrValue);

}
}
}
}
// create element locators only if attribute size >1 like class and id etc..

if (mapWeblementAttributes.size() > 1) {
Set<String> setUIKeys = mapWeblementAttributes.keySet();
String strLocatorStart = "//" + strTagName + "[";
String strLocator = "";
Boolean bAnd = false;
for (String key : setUIKeys) {
if (bAnd) {
strLocator = strLocator + "and@" + key + "='"
+ mapWeblementAttributes.get(key) + "'";
} else {
strLocator = strLocator + "@" + key + "='"
+ mapWeblementAttributes.get(key) + "'";
}
bAnd = true;
}
String finalxpath = strLocatorStart +strLocator + "]";
System.out.println(finalxpath);
strLocatorxPath.add(finalxpath);
counter++;
} else {
System.out.println("not able to create locator for "
+ strTagName
+ mapWeblementAttributes.values().toString());
}


///######################
//// only for class attribute if element has only one class attribute
if (mapWeblementAttributes.size() == 1) {
Set<String> setUIKeys = mapWeblementAttributes.keySet();
String strLocatorStart = "//" + strTagName + "[";
String strLocator = "";
Boolean bAnd = false;
for (String key : setUIKeys) {
if (key.equalsIgnoreCase("class"))
{
if (mapWeblementAttributes.get(key).matches(".*\\d.*"))
{
System.out.println("not able to create locator for "
+ strTagName
+ mapWeblementAttributes.values());


}else
{
if (bAnd) {
strLocator = strLocator + "and@" + key + "='"
+ mapWeblementAttributes.get(key) + "'";
} else {
strLocator = strLocator + "@" + key + "='"
+ mapWeblementAttributes.get(key) + "'";
}
bAnd = true;
String finalxpath = strLocatorStart + strLocator + "]";
System.out.println(finalxpath);
strLocatorxPath.add(finalxpath);
}
}

}

}
///#####################
}
}
        System.out.println("Total xpath locator created from base page " + strLocatorxPath.size());
return strLocatorxPath;
}

------------------------------------------------------#############---------------------

WebDriver driverbase = new FirefoxDriver(profile);
driverbase.manage().window().maximize();
WebDriver driverStatic = new FirefoxDriver(profile);
driverbase.manage().window().maximize();
String page1 = "http://www.google.com/";
String page2 = "http://www.google.com/";
driverbase.get(page2);
driverStatic.get(page1);

///######## Getting elements locator from base page ###################
List<String> strLocatorxPathofBasepage = getPageLocatorsxPath(driverbase, page1);
////##########################
        System.out.println("Total xpath locator created from base page " + strLocatorxPathofBasepage.size());
for (String xpathExpression : strLocatorxPathofBasepage) {
System.out.println("Checking for xpath " + xpathExpression);
        String strLocator = xpathExpression;
try {
                 boolean elementFoundinBasePage = (driverbase.findElements(By.xpath(xpathExpression))
.size() > 0);
                 boolean elementFoundinGivenPage = (driverStatic.findElements(By.xpath(xpathExpression))
.size() > 0);
               
if ((elementFoundinBasePage==true)
& (elementFoundinGivenPage==true)) {
strLocator = xpathExpression;
WebElement childElementGSAM = driverbase.findElement(By
.xpath(xpathExpression));
WebElement childElementStatic = driverStatic.findElement(By
.xpath(xpathExpression));
// call verify css method for css validation
verifyCSSAttributes(WebElement childElementGSAM,WebElement childElementStatic);

System.out.println(driverbase.findElement(
By.xpath(xpathExpression)).getText());
System.out.println(driverStatic.findElement(
By.xpath(xpathExpression)).getText());
/// call verify text method
verifyText(WebElement childElementGSAM,WebElement childElementStatic);
}
}}


##############################################


In this way we can check for multiple pages

No comments:

Post a Comment

Which one is right ?

Translate







Tweet