Sunday, June 7, 2015

Selenium WebDriver : How to find the highlighed text on webpage.





/*
* How to find the highlighed text on webpage.
* Programming language : JAVA
*
* We can achieve this with the help og JavaScript indow.getSelection().toString() function
*
*/
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class HighlightedTextOnWebPage {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = null;

driver.get("https://google.com");
Thread.sleep(5000);
// Try to highlight any text on webpage using the mouse.


if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}

String hldText = (String)js.executeScript("return window.getSelection().toString();");
System.out.println("Highlighted Text on WebPage is : " + hldText.trim());

}
}

Selenium WebDriver : How to handle PopUp and close it


/*
* Pop-up handling in Selenium WebDriver
* Programming language : JAVA
*
* Here we will browse Rediff site and close the pop-up .
* We will print the pop-up window URL and the main window URL
*/

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PopUpHandling {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

// for chrome browser setting
//System.setProperty("webdriver.chrome.driver", "D:/setup/chromedriver.exe");
//WebDriver driver = new ChromeDriver();

driver.get("http://www.rediff.com/");

// Since the Window Handles are unique , we will use java.util.Set to store as it does allow the duplicate values.
// Windows Handles is of type String, so we store the value of window handle as String in Set
Set<String> winHandle = driver.getWindowHandles();

// just to check the number of total windows , in this case its 2
System.out.println("Total number of Windows : "+ winHandle.size());

// Iterator is use to iterator through Set , we can use for loop also
Iterator<String> it = winHandle.iterator();

String parentWindow = it.next();


String childWindow = it.next();

// switch to child window / pop-up window
driver.switchTo().window(childWindow);

System.out.println("child URL/ Title : " + driver.getCurrentUrl() );
driver.close(); // close the child /popUp window

driver.switchTo().window(parentWindow);
System.out.println("parent URL/ Title : " + driver.getCurrentUrl() );

}

}

Selenium WebDriver : Example to set and invoke different browsers through WebDriver


/*
* Example to set and invoke different browser in Selenium WebDriver
* Programming Language : JAVA
*/
import java.util.Scanner;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class SettingDifferentBrowserinSelenium {

static WebDriver wd = null;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter browser which you want to invoke : ");
String browserName = sc.next();
openBrowser(browserName);

}


public static void openBrowser(String browserName){

if(browserName.equalsIgnoreCase("Mozilla")){
wd = new FirefoxDriver();
System.out.println("Browser invoked : " +browserName);

}else if(browserName.equalsIgnoreCase("Chrome")){

//for chrome browser you have to set the path
System.setProperty("webdriver.chrome.driver", "/path_to_chormedriver/chromedriver.exe"); // D:/setup/chromedriver.exe
wd = new ChromeDriver();
System.out.println("Browser invoked : " +browserName);

}else if(browserName.equalsIgnoreCase("Chrome")){

//for ie browser you have to set the path , use the IEDriverServer.exe based on your OS architecture x64bit or x86bit
System.setProperty("webdriver.ie.driver", "/path_to_iedriver/IEDriverServer.exe"); // D:/setup/IEDriverServer.exe
wd = new InternetExplorerDriver();
System.out.println("Browser invoked : " +browserName);

}else{

System.out.println("You can either invoke the default browser or display"
+ " a message to provide valid broswer name ");
}

}

}

Java : Program to find the minimum and maximum number in a given array.


/**
* Find the minimum and maximum number in a given array.
*
* Approach: we can pick an element from array and compare with the min and max value
*
*
*/

public class MinMaxNumber {

public static void main(String[] args) {

int arr[] = {1100,10,3,5,8,1,5,9,23,56,0,90,54};
int min,max;
min=max= arr[0];
for(int i =1 ; i<arr.length;i++){

if (min > arr[i]){ // comparing the number for min value
min= arr[i];
}

if(max < arr[i]){ // comparing the number for max value
max=arr[i];
}
}
System.out.println("Min value = " + min);
System.out.println("Max value = " + max);

}

}

/* output
Min value = 0
Max value = 1100

Tuesday, June 2, 2015

Java : Program to swap two number without using any arithmetic operator


import java.util.Scanner;

/*
*
* Swap two number without using any arithmetic operator like + , - etc
 * we can achieve the solution using the XOR operator
*/


public class SwapTwoNumberWithoutUsingOperator {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter number A : ");
int a = sc.nextInt();
System.out.println("Enter number B : ");
int b = sc.nextInt();


System.out.println("Number before swapped A and B respectively --> " + a +" " + b);

a = a^b;
b = a^b;
a = a^b;

System.out.println("Number after swapped A and B respectively --> " + a +" " + b);


}

}

/* output
Enter number A :
10
Enter number B :
30
Number before swapped A and B respectively --> 10 30
Number after swapped A and B respectively --> 30 10

*/

Java : Program to find the sum of each row and each column of n x m of 2D Matrix


package jun2;

/*
* Find the sum of each row and each column of n x m of 2D Matrix
*
*/

public class SumOf2DMatrixRowsAndColumn {


public static void main(String[] args) {

int [][] twoDMatrix = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }
};
outputArray(twoDMatrix);
}

public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;

int[] colSum =new int[array[0].length];
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
sum += array[i][j];
colSum[j] += array[i][j];
}
System.out.println("sum of rows "+ i +" = " + sum);
}
System.out.println(" ");
for(int k=0;k<colSum.length;k++){
System.out.println("sum of columns "+ k +" = " + colSum[k]);
}


}

}

/*output:

sum of rows 0 = 97
sum of rows 1 = 206
sum of rows 2 = 334
sum of rows 3 = 454

sum of columns 0 = 91
sum of columns 1 = 81
sum of columns 2 = 79
sum of columns 3 = 118
sum of columns 4 = 85

*/

Thursday, May 14, 2015

Java : Program to find a occurrence/count of a particular pattern in a given string


/*
* Find occurrence/count of a particular pattern in a given string .
* For example : you have to search for a pattern ca#e or ca$e or ca&e or ca1e
* where 3 character could be any thing but first 2 chars and last
* should contain the c ,a and e respectively.
*
* Input = "carehappyca#eforyouca$ecccaseeeeca@ca$e"
* output = 5
*
* Condition : You don't have to use any inbuilt function like split() or regex() function etc.
*
*/

public class CountPatternInStringSequence {


public int countPatternInStringSequence(String str){

char[] ch = str.toCharArray(); // convert the given string to character array;
int i = 0;
int count = 0;

while(i < ch.length){

if(ch[i] == 'c'){
i++;
if(ch[i] == 'a'){
i= i+2;
if(ch[i] == 'e'){
count++;
}else{
i = i-2;
}
}else{

i = i-1;
}
}

i = i +1;

}
return count;
}

public static void main(String[] args) {

CountPatternInStringSequence fs = new CountPatternInStringSequence();
String str = "carehappyca#eforyouca$ecccaseeeeca@ca$e";
int totalCount = fs.countPatternInStringSequence(str);
System.out.println("Given String : " + str);
System.out.println("Total occurence of particular pattern found : " + totalCount);

}

}

/*
output:

Given String : carehappyca#eforyouca$ecccaseeeeca@ca$e
Total occurence of particular pattern found : 5

*/

Thursday, January 15, 2015

Java : A string consists of parenthesis and letters. Write a program to validate all the parenthesis.


import java.util.*;
/*
*
* A string consists of parenthesis and letters. Write a program to validate all the parenthesis.
* Ignore the letters.eg.
* ((alf)ls) – valid
* )(dkk)() – invalid
*/

public class StringI_1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter string :- ");
String str = sc.nextLine();

System.out.println("Entered string is :- " + str);

int countOpen = 0 ;
int countClose =0 ;
int sizeOfString = str.length();

if(str == null || sizeOfString == 0){
System.out.println("String is Empty");
System.exit(0);
}

if (str.charAt(0) != '(' && str.charAt(sizeOfString - 1) != ')' ){
System.out.println("First : Invalid String");
System.exit(0);

}
/*else
System.out.println("valid");*/

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

if(str.charAt(i) == '(' && i!=(sizeOfString-1) ){
if( str.charAt(i+1) == ')' ){
System.out.println("Second :Invalid String ");
System.exit(0);
}else{
countOpen++;
System.out.println("countOpen :- " + countOpen +" "+ str.charAt(i));
}
}else {
if(str.charAt(i) == ')' ){ //&& str.charAt(i+1) !=')'
countClose++;
System.out.println("countClose :- " + countClose +" "+ str.charAt(i));
}
}
}
if(countOpen == countClose){
System.out.println("Valid string");
}else{
System.out.println("Third : Invalid String");
}
}

}

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...