Sunday, November 29, 2015

Selenium WebDriver : How to handle frame in webpage using selenium automation.

/*
 * @author : P programs
 * Selenium WebDriver : How to handle frame in webpage using selenium automation. 
 *
 */

An iframe is nothing but a webpage within a webpage.
Multiple iframe can be present in a webpage . To work or extract data from frame is not straight forward.We can't directly access iframe using code i.e we need to switch to frame before extracting any data or perform any operation.
We can use driver.switchTo().frame("pass_frame_index or frame_name") function to jump to particular frame in webpage.
Here is a self explanatory example in Selenium web driver.



import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FrameHandlingInSelenium {

 public static void main(String[] args) {

  
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_height_width");
  
// First of all we need to get the list of frames present in page. we can use iframe tagname as a locator. 
  List<WebElement> pageframeList =driver.findElements(By.tagName("iframe"));

// For debugging purpose , print the size of available frames.  
  System.out.println("Total list of frames ina page. size = " + pageframeList.size()); 

// Once you identified the desired frame where you want to retrieve data, switch to that frame   
  driver.switchTo().frame("iframeResult");

// In this example, we are again going to inner frame to retrieve data.
// Get the list of all frames inside the outer frame.  
  List<WebElement> frameIn =driver.findElements(By.tagName("iframe"));
  System.out.println("Inner frame size :-" +frameIn.size());

// Either you can switch to frame using frame name or frame index.Since in this example only one frame is present
// inside.so we are directly switching using index 0.   
  driver.switchTo().frame(0);

// Now print the text inside the frame 0 using valid xpath.  
  System.out.println(driver.findElement(By.xpath("html/body/h1")).getText());
  
  driver.quit();  
 }

}

Selenium WebDriver : How to take screenshot and save it to particular location with timestamp.

/*

Selenium WebDriver : How to take screenshot and save it to particular location with timestamp.

 *
 */

This is a simple utility program which helps to capture a screenshot in Web automation using Selenium WebDriver.

User can enhance this program to save output file with their own requirement of timestamp and the file format in which they want to save.



import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Screenshot_Demo_Selenium {

 /**
  * @param args<!-- ?xml version="1.0" encoding="iso-8859-1"? -->
  */
 public static void main(String[] args) throws IOException {

// Create browser instance, to know how to set and invoke different browser click here:
  WebDriver driver = new FirefoxDriver();  
  
  driver.get("https://twitter.com");   // provide the URL to browse
  driver.manage().window().maximize();  // maximize the browser
  
  //Java class which formats date and time in a given manner
  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
  Date date = new Date();
  
  String fileName = (dateFormat.format(date)).toString();
  System.out.println(fileName);
  
  
  //Using below function TakesScreenshot which is type-casted to driver whose output type is File.
  File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
  
  //Using copyFile function which take 2 parameters , sourceFile and destination file( where you can provide the path with the file extension )
// System.getProperty("user.dir") - provide the current directory path.
  FileUtils.copyFile(sourceFile, new File(System.getProperty("user.dir")+"\\screenshots\\"+fileName+".jpg"));
  
  //quit() is a selenium function which close and quits the driver.
  driver.quit();
  
 }

}

Java : Program to check if a given integer number is power of two.

/*
 * @author : P programs
 * Check if a given integer number is power of 2 or not.
 * Exp :8 is a power of 2
 *     :10 is not a power of 2
 *     :32 is a power of 2.
 *
 * To find whether a number is power of 2 or not, we can do a AND operation between number and number-1 ,
 * if AND operation results in 0 then it's a valid power of 2 else its not.
*/

import java.util.*;

public class FindIntegerPowOf2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter valid Integer number : - ");
int validIntNumber = sc.nextInt();
int result = pow2(validIntNumber);

System.out.println(result);

if (result ==0){
System.out.println("Number is a valid power of 2 ");
}else
{
System.out.println("Number is not a valid power of 2 ");
}


}

public static int pow2(int number){

number = number & (number-1);
return number;

}

}

Sunday, November 15, 2015

Android : How to change language of device using commandLine

How to change language of device using commandLine


Changing language in Android device on-the fly is needed when we are running or executing automation script in various languages.

To achieve language change of Android device, we have two ways:


You can watch a Youtube video OR follow the step by step procedure below





1> Using adb command 
 Below command can be run from command line , to change the language of device.
   
adb shell "setprop persist.sys.language fr; setprop persist.sys.country CA; 
setprop ctl.restart zygote"

** But this procedure is having one issue, your phone should be ROOTED to make adb command into effect.  

2> Using ADB Change Language APP installed on your device (link)

Steps: 
-
Install this APP on to your device.
-
Setup adb connection to your device (How to do - follow Link )
-
Android OS 4.2 onwards (tip: you can copy the command here and paste it to your command console):
adb shell pm grant net.sanapeli.adbchangelanguage android.permission.CHANGE_CONFIGURATION
- Language change example 1, Brazilian Portuguese:
adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language pt-rBR
- Language change example 2, Canadien French:
adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language fr -e country CA

To make your work easy , create a .bat file

@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@           Create .bat file                  @@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@

You can put the change language command in .bat file and can trigger it from your code.
Create a .bat file lets say : "changelang.bat" and paste below 3 line and save it.

cd /D 
adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language %1

exit




That's it.

Sunday, August 16, 2015

Java : Given two unsorted arrays ,write a program to remove duplicates and merge it into sorted order


package com.qahumor;

import java.util.Iterator;
import java.util.TreeSet;

/*@author : qahumor 
 * Given two Unsorted Arrays a1 and a2, 
 * Remove duplicates and merged both into sorted order  
 * 
 */

public class TwoUnsortedArrayMergedAndSorted {

 public static void main(String[] args) {
  
  int[] a1 = {1,2,3,49,6,77,8};
  int[] a2 = {3,67,77,3,10,54};
  int[] a3;
  
  TreeSet<Integer> ts = new TreeSet<Integer>();

  for(int i = 0 ; i < a1.length;i++){
   ts.add(a1[i]);
  }
  
  for(int j = 0 ; j < a2.length ; j++){
   ts.add(a2[j]);
  }
  
  Iterator<Integer> it = ts.iterator();
  a3 = new int[ts.size()];
  int k =0;
  while(it.hasNext()){
   a3[k] = it.next();
   k++;
  }
  
  
  for(int p = 0 ; p < a3.length ; p++){
   System.out.print(a3[p]+",");
  }
 }

}

JAVA : Program to check a given string, if rotated is a palindrome or not

/* * @author : P programs
    * Check if a given string becomes palindrome after rotation or not.
    * Exp : mmo , aaplaaeep , travel
    *
    *We can check for the count of each character in a string , If the total count of 2 chars are equal to                
        1 then the string can't be palindrome.
 */

package pprograms;

import java.util.HashMap;
import java.util.Scanner;


public class CheckStringRotationPalindrome {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter a String ");
 
                String str = sc.next();
  HashMap<Character , Integer> hs = new HashMap<Character, Integer>();
int len = str.length();
int count = 0;

for(int i = 0 ; i < len ; i++ ){

if(hs.get(str.charAt(i))!=null){

hs.put(str.charAt(i), hs.get(str.charAt(i))+1);

}else{

hs.put(str.charAt(i), 1);

}
}

for(int i = 0 ; i < len ; i++ ){

if(hs.get(str.charAt(i))==1){
count ++;
if(count == 2){
break;
}
}
}

if(count ==1 ){
System.out.println("String can become palindrome after rotation");
}else{
System.out.println("String can't become palindrome even after rotation");
}
}
}

Sunday, August 2, 2015

Android : How to download .apk file from playstore


Here is the simple procedure to download the .apk file from playstore or from mobile device.

Procedure 1: Downloading from google playstore


Step1 : Browse URL https://play.google.com/store?hl=en
Step2 : Search for your app which you want to download, for example facebook.
Step3 : Select and click on the appropriate facebook app from list available.
Step4 : Copy the URL from address bar as it contains the app package name , see the pic below

Step5: Browse URL http://apps.evozi.com/apk-downloader/
Step6: Paste the copied URL in step4 to textbox as shown below.
Step7: Click on Generate Download link and click on download button as shown with highlighted button in red.

Testing : Key information gathering before you start testing/involving in a new product



I have been testing product from more than 7+ years and seen people specially freshers or test-leads who recently started handling projects facing problem initially in information gathering of product . In IT industry as we all know that teams are spread across the globe who are working on similar project. And getting same/all information's about product at once is a big headache. Most the time meeting happens at a time which does not suits to your timing and you may miss some critical updates /schedule or information or didn't follow your mails because of some or the other reasons. Ultimately you won't be having all the required information's of product at one place as it spreads across your email. So based on my experience and the issue faced , I jotted down some key questions which we can ask to Project Managers or Stake holders or leads(in case of fresher) before we start testing/involving in a product.

1- Ask for product functional specification.

2- What are the new features added to product?(applicable for product having incremental release)

3- Availability of reading stuffs includes any product document ,admin guide ,videos, hands-on docs etc

4- Ask for project milestone which includes CC/FF/SI/RC/RTM date etc.

5- Supported language of the product(if your product supports MUI)

6- Supported platforms of the product i.e. OS(32 or 64 bit )/Browser/Versions/Mobile devices etc

7- Ask for build location , from which branch build should be picked like main branch, staging etc.

8- Is the build available via Testfairy, Jenkins or ask for share location where builds are kept.

9- Which tool is used to maintain the stories of new features like JIRA, AtTask etc , if you are following agile methodologies .

10- What is the target version of product

11- Ask for Upgrade scenario. For example

              11.1- build to build upgrade is supported or not.

              11.2- Upgrade from previous version is supported or not.

              11.3- downgrade to previous version is supported or not.

12- Previous version compatibility is supported or not. If your server version is new , can older version client connects to new server and vice-versa.

13- Area which needs more testing efforts.

14- Ask for bug reporting guidelines if any and you can narrow down to few things/parameters

            14.1- Found in release

            14.2- Target Release

            14.3- Developer contacts

            14.4- Severity guidelines of bug

15- Any 3rd parties software integration required etc

16- Automation scripts is available for this product or can this be automated etc.




These are some points which helps you to get started but I believe there will be many more such questions are present. I would to request you to share your experience in comment sections.

Linux: How to change OS language via terminal?

  In this post , I will be talking about how to change language of Linux OS from Terminal . If you are not a frequent user of Linux and str...