TESTEVERYTHING

Saturday 25 July 2015

How to know app package and activities information without having apk | android app | Appium

How to know app package and activities information without having apk | android app | Appium

In Android app mobile automation,we need apk file of app but sometimes we have to perform action on inbuild app(pre installed) which come with device like call,sms etc.. for this we do not have apk file. Basically in android app automation we need app package information  and app activities. In case of apk it is easy to get above information with appium. But if we do not have apk and need the app package/activities information, we can use following command:

Precondition: 
1. Android mobile must have developer option set to true.
2. ADB (Android debug bridge) must be configure
3. Android SDK installed.

Step1. Open then command terminal type adb devices and press enter

It will confirm that your device is connected and able to recognize by android sdk.






Step2: Now in mobile, open the app of which you want to get app pkg info/activities. In your mobile, it should be current activated app with focus. Means current window of your mobile should be app like calling/msg/your app window. 

Step3: Now type adb shell and press enter to open the adb shell

Step4: type dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'  and press enter







Above command will show the current activated app info of  your mobile. Now check the value of mCurrentFocus.

First highlighted text is pkg info



Second highlighted text is app activities info


Here you can see that we can use this:





 Thanks



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

Which one is right ?

Translate







Tweet