Tuesday, 24 July 2018

Durga Soft- Testing Notes FAQ


























Random Testing Interview Questions [Manual Testing &Selenium & Core Java ]

Below are the some of the testing interview questions frequently asked. I might have listed the solution for some and others are pretty straight forward.Google can help you!!


Ø  If a button is not present at the time of testing but it will come in future how will you test?
Ø  How to make finally block not to execute
Ø  Program to reverse a string
Ø  Check 2 strings eg: str1|A|B|C| & str2|C|A|B|
Ø  how to check str2 contains some alphabets as string1
Ø  Program for Bubble sort
Ø  What is Static variable? What is use of it?
Ø  What is the difference between overloading and overriding?
Ø  How do you stress test on windows media player? Give out two scenarios
Ø  explain junit
Ø  How to schedule jobs in Jenkins?
Ø  Explain Architecture of testng.
Ø  Explain implicit wait vs explicit wait
Ø  In which method you will define implicit wait in testng?
Ø  How to handle dynamic web tables in selenium?
Ø  How to capture a screenshot?
Sol: 
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in d drive with name "screenshot.png"
FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"))

Ø  How to load/fetch keys value from properties file?
Ø  Find the second highest salary from employees table
Ø  How to connect DB to selenium
Ø  How to Handle alert box
Ø  How to handle window based pop ups
Ø  Program to reverse a string
Ø  Program to count the repeated words in a string
Ø  How to return a string from method?
Ø  Difference b/w absolute class and absolute methods
Ø  Program to print Duplicate numbers from array
Ø  Program to check whether string is palindrome or not
Ø  Difference b/w throw and throws
Ø  Define Abstraction
Ø  How to Connect to DB
Ø  Annotation in Testng and their order of execution
Ø  Difference between HSSf and XSSf
Ø  How to execute parallel test cases
Ø  Which locator is fastest in selenium Id?
Sol: 
1st css-2nd, xpath

Ø  What are different locators?
Ø  Define Properties of css
Ø  What is property file in java?
Ø  How to check whether text is underlined or not?
Ø  Can static method be overloaded and overridden?
Ø  What is final?
Ø  Can final method be overloaded and overridden?
Ø  In automation I am not able to click on element how do u handle which works fine when done manually?
Ø  How do you identify different exception in try catch?
Ø  Difference b/w string, string buffer, string builder
Ø  Program to print unique characters
Ø  What is stale exception?
Ø  How do you handle certificates?
Ø  What happens if we don’t have main in public static void main?


RETRY TEST CASE LOGIC WITH MAX LIMIT USING TESTNG



We can implement retry logic in testNg using two ways



i. At Class level
ii. At Suite level(Recommended)

Class Level Implementation


> Create a class(Dynamic RetryAnalyzer) which implements IRetryAnalyzer
Define the max count how many times you need to run



public class dynamic RetryAnalyzer implements IRetryAnalyzer{
int count=1;
int maxRetryCount=3;
@OVerride
public boolean retry(ITestResult result){
if(result.getStatus()==ITestResult.FAILURE && count <=maxRetryCount)
count++;
return true;
}
return false;
}
}

> In the test which we are running define retryAnalyzer annotation

public class dummytest{

@Test(retryAnalyzer=DynamicRetryAnalyzer.class)
public void testOne{
system.out.println(" ")
Assert.assertTrue("false")
}}




Suite Level Implementation



> Create a class(Retry) which implements IAnnotationTransformer

public class Retry implement IAnnotationTransformer{
@OVerride
public void transform(ITestAnnotation annotation, Class areg1, Contstructor arg2, Method arg3){
IRetryAnalyzer retry=annotation.getRetryAnalyzer();
if(retry==null)
annotation.setRetryAnalyzer(CustomRetryListener.class)



> In Suite xml define Listeners and class(retry ) where it is defined

<suite name="suite" parallel="false>
<listeners>
<listener class=name="com.listener.Retry"></listener>
</listeners>

FREQUENTLY ASKED JAVA PROGRAMMATIC QUESTIONS FOR TESTERS


TO PRINT DUPLICATE CHARACTERS FROM STRING

String str="MMIMAA";
char[] chars= str.toCharArray();
Hashtable<Character,Integer> charcount = new Hashtable<Character,Integer>();
                               
                  for(int i=0;i<chars.length;i++)
                  {
                        if(charcount.containsKey(chars[i]))
                              charcount.put(chars[i], charcount.get(chars[i])+1);
                                      else
                            charcount.put(chars[i], 1);
                                }
               
        Set<Character> count= charcount.keySet();
      
                 for (Character ch : count)
        {
               // if(charcount.get(ch) > 1)
            {
    
                System.out.println(ch +" : "+ charcount.get(ch));
                               
                }}}}

        
     TO COUNT THE TOTAL NUMBER OF OCCURANCES OF A GIVEN CHARACTER IN AS STRING WIHTOUT USING ANY LOOP

String str = "This is an Example Of The Character";
System.out.println("Length Of String:" + str.length());
System.out.println("Length Of String Without a :" + str.replace("a", "").length());
int charcount = str.length() - str.replaceAll("a", "").length();
System.out.println("Occurrence Of A Char In String: " + charcount);


TO REVERSE A STRING

String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);


       TO REMOVE ALL WHITE SPACES FROM A STRING

      String str = "India     Is My    Country";  
 //1st way  
 String noSpaceStr = str.replaceAll("\\s", ""); // using built in method  
 System.out.println(noSpaceStr);  

//2nd way  
char[] strArray = str.toCharArray();  
StringBuffer stringBuffer = new StringBuffer();  
for (int i = 0; i < strArray.length; i++) {  
if ((strArray[i] != ' ') && (strArray[i] != '\t')) {  
stringBuffer.append(strArray[i]);  
}  }
String noSpaceStr2 = stringBuffer.toString();  
 System.out.println(noSpaceStr2);  


TO PRINT FIRST NON REPEATED CHARACTER FROM STRING

HashMap<Character,Integer> scoreboard = new HashMap<>();
// build table [char -> count]
 for (int i = 0; i < word.length(); i++) {
 char c = word.charAt(i);
  if (scoreboard.containsKey(c)) {
  scoreboard.put(c, scoreboard.get(c) + 1);
  } else {
    scoreboard.put(c, 1);
 }}
// since HashMap doesn't maintain order, going through string again
   for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
  if (scoreboard.get(c) == 1) {
 return c;
  }


     TO CHECK IF AS STRING CONTAINS ONLY DIGITS

public boolean containsOnlyNumbers(String str) {
 //It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
  return false;
 for (int i = 0; i < str.length(); i++) {
 //If we find a non-digit character we return false.
  if (!Character.isDigit(str.charAt(i)))
                return false;
  }
               return true;
    }


TO COUNT NUMBER OF VOWELS AND CONSTANTS IN A STRING

     String line = "This website is aw3som3.";
        int vowels = 0, consonants = 0, digits = 0, spaces = 0;

        line = line.toLowerCase();
        for(int i = 0; i < line.length(); ++i)
        {
            char ch = line.charAt(i);
            if(ch == 'a' || ch == 'e' || ch == 'i'
                || ch == 'o' || ch == 'u') {
                ++vowels;
            }
            else if((ch >= 'a'&& ch <= 'z')) {
                ++consonants;
            }
            else if( ch >= '0' && ch <= '9')
            {
                ++digits;
            }
            else if (ch ==' ')
            {
                ++spaces;
            }
        }
        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);
        System.out.println("Digits: " + digits);
        System.out.println("White spaces: " + spaces);



   TO FIND ALL PERMUTATIONS OF A STRING

public static Set<String> permutationFinder(String str) {
        Set<String> perm = new HashSet<String>();
        //Handling error scenarios
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            perm.add("");
            return perm;
        }
        char initial = str.charAt(0); // first character
        String rem = str.substring(1); // Full string without first character
        Set<String> words = permutationFinder(rem);
        for (String strNew : words) {
            for (int i = 0;i<=strNew.length();i++){
                perm.add(charInsert(strNew, initial, i));
            }
        }
        return perm;
    }



 TO CHECK IF STRING IS PALINDROME

String str, rev = ""; Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
int length = str.length(); f
or ( int i = length - 1; i >= 0; i-- ) rev = rev + str.charAt(i);
if (str.equals(rev)) System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");



TO REMOVE DUPLICATE CHARACTERS FROM STRING

public void duplicate_occurance(String str)
        {
 int len = str.Length;
char[] c= str.ToCharArray();
           StringBuilder sb = new StringBuilder();
           HashSet<char> set = new HashSet<char>();
            for(int i=0;i<str.Length;i++){
                if(!(set.Contains(c[i])))
                {
                set.Add(c[i]);
              sb.Append(c[i]);
                }}
           System.out.println sb);
}


  TO CHECK IF STRING CONTAINS ANOTHER STRING

  String haystack = "this is a test";
  String needle1 = "Java";
  int index1 = haystack.indexOf(needle1);
    if (index1 != -1)
      System.out.println("The string contains the substring " + needle1);
    else
      System.out.println("The string does not contain the substring " + needle1);


      TO SORT STRING

String original = "edcba";
int j=0;
char temp=0;
char[] chars = original.toCharArray();
for (int i = 0; i <chars.length; i++) {
for ( j = 0; j < chars.length; j++) {
if(chars[j]>chars[i]){
          temp=chars[i];
           chars[i]=chars[j];
           chars[j]=temp;
       }}}
for(int k=0;k<chars.length;k++){
System.out.println(chars[k]);
}}}


TO PRINT FIBONACCI SERIES

 int num1 = 0;
 int num2 = 1;
 int res=num1+num2;
  System.out.println (num1);
  System.out.println e(num2);
  System.out.println (res);
  for (int i = 4; i <= limit; i++){
             num1 = num2;
              num2 = res;
              res = num1 + num2;
         System.out.println (res);
}

TO CHECK GIVEN NUMBER IS PRIME NUMBER

        public void prime_number(int num)
        {
            int flag=1;
            for(int i=2;i<=num/2;i++) {
             int res=num%i;
                if (res == 0)
                {
                    flag = 0;
                    break;
                }
                else
                 flag = 1;
                }
            if (flag == 0)
System.out.println (" not prime number");
      else
System.out.println (" prime number");
}


    SWAP TWO NUMBERS WITHOUT USING TEMP

int a = 10;
int b = 20;
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);
//swapping value of two numbers without using temp variable
a = a+ b; //now a is 30 and b is 20
b = a -b; //now a is 30 but b is 10 (original value of a)
a = a -b; //now a is 20 and b is 10, numbers are swapped
System.out.println("value of a and b after swapping, a: " + a +" b: " + b)