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;

}

}

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.

Android : How to connect your Android device over Wifi using ADB command for App debugging

How to connect your Android device over Wi-Fi using ADB command Sometimes it requires to connect your Android dev...