Tuesday, 16 June 2015

Working with more than two windows, identify the windows based on name not by number.

Here is code to work with more than two windows, identify the windows based on name not by number.
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://irctc.co.in/");
Thread.sleep(400);
//click on Hotels @ Lounge
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[6]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[7]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[8]/a")).click();
Thread.sleep(400);
Set<String> cWinHndls = driver.getWindowHandles();
for(String cWinHndl:cWinHndls){
String cWinHndlTitle = driver.switchTo().window(cWinHndl).getTitle();
System.out.println("Window Title: "+cWinHndlTitle);


if(cWinHndlTitle.equalsIgnoreCase("IRCTC Hotel India, Book Confirm Hotels, Retiring Room Indian Railways")){
driver.findElement(By.xpath("//section[@id='mainpannel']/div[2]/div[1]/div[1]/table/tbody/tr/td[2]/input")).sendKeys("2223334445");
}
if(cWinHndlTitle.equalsIgnoreCase("LTC Tour Packages By IRCTC, Confirm Ticket, Tourist Charters India")){
driver.findElement(By.xpath("//ul[@id='main-menu']/li[1]/a")).click();
}
if(cWinHndlTitle.equalsIgnoreCase("Heritage Monuments of India")){
driver.findElement(By.xpath("//div[@id='intro']/div[2]/div[2]/div/form/input")).click();
}
}
driver.switchTo().window(mWinHndl);
}

To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

Wednesday, 29 October 2014

Multi Selection

Suppose if we want to select multiple selections in the combo box. To achieve this sample code is given below.


Example: 


List<WebElement> listItems = driver.findElements("find your select by id or xpath or etc");
Actions builder = new Actions(driver);
builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click();
Action selectMultiple = builder.build();
selectMultiple.perform();


To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

Saturday, 4 October 2014

Auto Complete Suggestion View

In an application We will search for an item in the Search box. We will try to select the third suggestion in the auto complete suggestion view. To get this sample code is given below.


package Logs;

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

public class Auto {

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

 WebDriver driver=new FirefoxDriver();

 driver.manage().window().maximize();

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

// Type something on Search textbox
 driver.findElement(By.name("q")).sendKeys("mobil");

// Create object on Actions class
 Actions builder=new Actions(driver);

// find the element which we want to Select from auto suggestion

 WebElement ele=driver.findElement(By.xpath(".//*[@id='list_?']/li[3]/ac_odd"));

// use Mouse hover action for that element
 builder.moveToElement(ele).build().perform();

// Give wait for 2 seconds
 Thread.sleep(200);

// finally click on that element
 builder.click(ele).build().perform();

}
}

To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

Tuesday, 23 September 2014

Most Common Java Programs for Testers in Interviews

1.How can you show the duplicate elements from an array by avoiding distinct elements.

     ArrayList<String>list = new ArrayList<String>();
    //list of numbers added in from 0-9
   
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        //another set of numbers from 0-5
        for (int i = 0; i < 5; i++) {
            list.add(String.valueOf(i));
        }

        System.out.println("My List : " + list);
        System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list));

      For the full code Please ClickHere

2.How can you reverse a number 

        int reverse = 0;
        while(number != 0){
            reverse = (reverse*10)+(number%10);
            number = number/10;
        }
        return reverse;
   
For the full code Please ClickHere

3.How can you sort an collection of array of strings

String[] strArray = { "Abhishek", "selenium", "Automation" };

  displayArray(strArray);

   Arrays.sort(strArray);

   displayArray(strArray);

Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  displayArray(strArray);

  System.out.println("---------------");

  List<String> strList = new ArrayList<String>();
  strList.add("Abhishek");
  strList.add("Webdriver");
  strList.add("Automation");
  displayList(strList);

 For the full code Please ClickHere

4.Can you reverse a String with and Without using StringBuffer

   With String Buffer
     StringBuffer buffer = new StringBuffer(str);
      buffer.reverse();
     return buffer.toString();

   Without String Buffer
    int length = str.length();
    String original = str;
    String reverse = "";
    for(int i = length-1; i>=0; i--){
    reverse = reverse + original.charAt(i);
    }
   return reverse;

For Full Code Please ClickHere

5.How can you say a number is Prime or not According to user input

    if (isPrime(n)) {
            System.out.println(n + " is a prime number");
        } else {
            System.out.println(n + " is not a prime number");
        }

    if (n <= 1) {
            return false;
        }
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;

For Full Code Please ClickHere

6.How can you a number is Palindrome or not According to user input

for (int i=0; i<=num; i++){
   int r=num%10;
   num=num/10;
   rev=rev*10+r;
   i=0;
   }

For Full Code Please ClickHere

7.How can you show the maximum numbers from the given array

  int maxOne = 0;
  int maxTwo = 0;
  for(int n:nums){
   if(maxOne<n){
    maxTwo = maxOne;
    maxOne = n;
   }else if(maxTwo<n){
    maxTwo = n;
   }
 
  }

For the Full Code Please ClickHere

8.Can you the Longest Substring Without Repetition of Characters in the string

  char[] inputCharArr = input.toCharArray();
for (int i = 0; i < inputCharArr.length; i++) {
char c = inputCharArr[i];
if (flag[c]) {
extractSubString(inputCharArr,j,i);
for (int k = j; k < i; k++) {
if (inputCharArr[k] == c) {
j = k + 1;
break;
}
flag[inputCharArr[k]] = false;
}
} else {
flag[c] = true;
}
}
extractSubString(inputCharArr,j,inputCharArr.length);
return subStrList;

For Full Code Please ClickHere

9. Swapping of Two Numbers Without having a temp Variable

       x = x+y;
        y=x-y;
        x=x-y;
For the Full Code Please ClickHere

10.How can you remove the multiple spaces of String

package Logs;
import java.util.StringTokenizer;

public class Mutiplespaces {
    public static void main(String a[]){
        String str = "Selenium    Webdriver Automation      Testing";
        StringTokenizer st = new StringTokenizer(str, " ");
        StringBuffer sb = new StringBuffer();
        while(st.hasMoreElements()){
            sb.append(st.nextElement()).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

Output
Selenium Webdriver Automation Testing

To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

Find the Longest Substring in the given String Without Repetition of characters

Given a string, find the longest substrings without repeating characters. Iterate through the given string, find the longest maximum substrings.
package Logs;
import java.util.HashSet;
import java.util.Set;

public class longestsubstring {

private Set<String> subStrList = new HashSet<String>();
private int finalSubStrSize = 0;

public Set<String> getLongestSubstr(String input){
//reset instance variables
subStrList.clear();
finalSubStrSize = 0;
// have a boolean flag on each character ascii value
boolean[] flag = new boolean[256];
int j = 0;
char[] inputCharArr = input.toCharArray();
for (int i = 0; i < inputCharArr.length; i++) {
char c = inputCharArr[i];
if (flag[c]) {
extractSubString(inputCharArr,j,i);
for (int k = j; k < i; k++) {
if (inputCharArr[k] == c) {
j = k + 1;
break;
}
flag[inputCharArr[k]] = false;
}
} else {
flag[c] = true;
}
}
extractSubString(inputCharArr,j,inputCharArr.length);
return subStrList;
}

private String extractSubString(char[] inputArr, int start, int end){

StringBuilder sb = new StringBuilder();
for(int i=start;i<end;i++){
sb.append(inputArr[i]);
}
String subStr = sb.toString();
if(subStr.length() > finalSubStrSize){
finalSubStrSize = subStr.length();
subStrList.clear();
subStrList.add(subStr);
} else if(subStr.length() == finalSubStrSize){
subStrList.add(subStr);
}

return sb.toString();
}

public static void main(String a[]){
longestsubstring mls = new longestsubstring();
System.out.println(mls.getLongestSubstr("automation_tester"));
System.out.println(mls.getLongestSubstr("Selenium_Webdriver_is_AutomationTesting"));
System.out.println(mls.getLongestSubstr("sdsjk_uippj_agjhs_7890"));
System.out.println(mls.getLongestSubstr("abcabcbb"));
}
}


OutPut:
[ion_tes, mation_]
[nium_Webdr, um_Webdriv]
[agjhs_7890]
[cab, abc, bca]

To know FAQ's in Interviews on Selenium  ClickHere
To know more on SQL Queries Required For Testers  ClickHere

Duplicate elements in ArrayList by avoiding Distinct elements

In an Array list I mean to show is the elements which are repeating and by avoiding the distinct elements .To show this we require a piece of code.
Example:
package Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DuplicateNumbers {

    public static void main(String[] args) {
   
    ArrayList<String>list = new ArrayList<String>();

    //list of numbers added in from 0-9
   
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        //another set of numbers from 0-5
        for (int i = 0; i < 5; i++) {
            list.add(String.valueOf(i));
        }

        System.out.println("My List : " + list);
        System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list));
    }

    public static Set<String> findDuplicates(List<String> listContainingDuplicates) {

        final Set<String> setToReturn = new HashSet<String>();
        final Set<String> set1 = new HashSet<String>();

        for (String yourInt : listContainingDuplicates) {
            if (!set1.add(yourInt)) {
                setToReturn.add(yourInt);
            }
        }
        return setToReturn;
    }
}



Output:

My List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]

Here are the duplicate elements from list : [3, 2, 1, 0, 4]

To know FAQ's in Interviews on Selenium Please  ClickHere
To know more on SQL Queries Required For Testers Please ClickHere

Thursday, 18 September 2014

How to Write a Effective Test Cases in Manual or Automation

  • Writing effective test cases is a skill and that can be achieved by some experience and in-depth study of the application on which test cases are being written.
  • Here I will share some tips on how to write test cases, test case procedures and some basic test case definitions.

What is a test case?
“A test case has a component that describes an input, action or event and an expected response, to determine if a feature of an application is working correctly.”

There are levels in which each test case will fall in order to avoid duplication efforts

Level 1: In this level you will write the basic test cases from the available specification and user documentation.

Level 2: This is the practical stage in which writing test cases depend on actual functional and system flow of the application.

Level 3: This is the stage in which you will group some test cases and write a test procedure. Test procedure is nothing but a group of small test cases maximum of 10.

Level 4: Automation of the project. This will minimize human interaction with system and thus QA can focus on current updated functionalities to test rather than remaining busy with regression testing.
So you can observe a systematic growth from no testable item to a Automation suit.

Why we write test cases?
The basic objective of writing test cases is to validate the testing coverage of the application. If you are working in any CMMI company then you will strictly follow test cases standards. So writing test cases brings some sort of standardization and minimizes the ad-hoc approach in testing.

How to write test cases?
Here is a simple test case format
Fields in test cases:

  • Test case id:
  • Unit to test: What to be verified?
  • Assumptions:
  • Test data: Variables and their values
  • Steps to be executed:
  • Expected result:
  • Actual result:
  • Pass/Fail:
  • Comments:
So here is a basic format of test case statement:
Verify
Using [tool name, tag name, dialog, etc]
With [conditions]
To [what is returned, shown, demonstrated]
Verify: Used as the first word of the test case statement.
Using: To identify what is being tested. You can use ‘entering’ or ‘selecting’ here instead of using depending on the situation.
  • For any application basically you will cover all the types of test cases including functional, negative and boundary value test cases.
  • Keep in mind while writing test cases that all your test cases should be simple and easy to understand. 
  • Don’t write explanations like essays. Be to the point.
  • Try writing the simple test cases as mentioned in above test case format.
  •  Generally I use Excel sheets to write the basic test cases.
  •  Use any tool like ‘Test Director’ when you are going to automate those test cases.




To know FAQ's on Selenium ClickHere
To know SQL Queries Required For Testers ClickHere