Automation Using Selenium Webdriver

Friday, 25 November 2016

How to Check All the Checkboxes Present in a Page Using Selenium

This post tells you how to check all the checkboxes present in the page.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;


public class testng_3 {
@Test
public void checkbox() throws Exception
{
FirefoxDriver fd= new FirefoxDriver();
fd.manage().window().maximize();
fd.get(“http://www.flipkart.com/laptops/lenovo~brand/pr?sid=6bo,b5g&otracker=hp_nmenu_sub_electronics_0_Lenovo”);
Thread.sleep(10000);
List checkBoxes = fd.findElements(By.xpath(“//input[@type=’Checkbox’]”));
System.out.println(checkBoxes.size());
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(checkBoxes.get(i).getText());
checkBoxes.get(i).click();
}
fd.close();
}
}

Java Program to find Max occurred character in a string



  • How to print duplicate characters from string in java?
  • Java program to count number of repeated characters in a string.
  • Java program for printing a repetitive letters in a string
  • count occurrences of each character in string java

  1. package com.javatutorial;
  2.  
  3. public class MaxOccuranceOfChar{
  4.    
  5. public static String MaxOccuredChar(String str) {
  6.  
  7.         char[] array = str.toCharArray();
  8.         int maxCount = 1;
  9.         char maxChar = array[0];
  10.  
  11.   for(int i = 0, j = 0; i < str.length() - 1; i = j){
  12.        int count = 1;
  13.    while (++j < str.length() && array[i] == array[j]) {
  14.           count++;
  15.         }
  16.  
  17.   if (count > maxCount) {
  18.      maxCount = count;
  19.      maxChar = array[i];
  20.  }
  21.  
  22.   }
  23.  
  24.         return (maxChar + " = " + maxCount);
  25.  }

  26.  public static void main(String[] args) {
  27.   
  28.   String str1=MaxOccuredChar("instanceofjava");
  29.   System.out.println(str1);
  30.  
  31.   String str2=MaxOccuredChar("aaaabbbccc");
  32.   System.out.println(str2);
  33.  
  34.   String str3=MaxOccuredChar("ssssiiinnn");
  35.   System.out.println(str3);
  36.  
  37.   String str4=MaxOccuredChar("jaaaavvvvvvvvaaaaaaaaaa");
  38.   System.out.println(str4);
  39.  
  40. }

  41. }


Output:
  1. i = 1
  2. a = 4
  3. s = 4
  4. a = 10

Thursday, 24 November 2016

Pattern Program in java Part-1


#1 Java Program To print Numbers in Below pattern: 

              1 
             2 3 
            4 5 6 
           7 8 9 10 
         11 12 13 14 15


  1. package com.javatutorial;
  2. public class NumbersFormat {
  3.  
  4.  public static void main(String[] args) {
  5.  
  6.  int num=15;
  7.  int temp=1;
  8.      
  9.  for (int i = 1; i <= num; i++)
  10.  {
  11.  
  12.   for (int k = i; k <num; k++)
  13.    System.out.print(" ");
  14.    for (int j =1; j <= i; j++){
  15.  
  16.     System.out.print("" +temp+ " ");
  17.      temp++;
  18.  
  19.  if(temp>15){
  20.        break;
  21.  }
  22.  
  23.  }
  24.  
  25.   System.out.println();
  26.  
  27. if(temp>15){
  28.      break;
  29.   }
  30.  
  31. }
  32.  
  33. }

  34. }
Output:

  1.               1 
                 2 3 
                4 5 6 
               7 8 9 10 
              11 12 13 14 15






# Java Program to print Numbers in Below Format:


1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 


  1. package learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. 1 2 
  2. 1 2 3 
  3. 1 2 3 4 
  4. 1 2 3 4 5 
  5. 1 2 3 4 5 6 
  6. 1 2 3 4 5 6 7 
  7. 1 2 3 4 5 6 7 8 
  8. 1 2 3 4 5 6 7 8 9 
  9. 1 2 3 4 5 6 7 8 9 10

#3 Java Program to print numbers in below format

1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 



  1. package com.learnJava;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= 10-r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }

Output:
  1. 1 2 3 4 5 6 7 8 9 
  2. 1 2 3 4 5 6 7 8 
  3. 1 2 3 4 5 6 7 
  4. 1 2 3 4 5 6 
  5. 1 2 3 4 5 
  6. 1 2 3 4 
  7. 1 2 3 
  8. 1 2 

throw keyword in java with example


Throw keyword in java
  • Throw keyword is used to throw exception manually.
  • Whenever it is required to suspend the execution of the functionality based on the user defined logical error condition we will use this throw keyword to throw exception.
  • So we need to handle these exceptions also using try catch blocks.

 Java simple example Program to explain use of throw keyword in java


  1. package exceptions;
  2.  
  3. public class ThrowKeyword {
  4.  
  5.     
  6. public static void main(String[] args) {
  7.  
  8. try {
  9.             
  10.    throw new ArithmeticException();
  11.             
  12.             
  13. } catch (Exception e) {
  14.  
  15.    System.out.println(e);
  16.    e.printStackTrace();
  17. }
  18.  
  19. }
  20.  
  21. }

Output:



  1. java.lang.ArithmeticException
  2. java.lang.ArithmeticException
  3.     at exceptions.ThrowKeyword.main(ThrowKeyword.java:11)



Rules to use "throw" keyword in java 
  • throw keyword must follow Throwable type of object.
  • It must be used only in method logic.
  • Since it is a transfer statement , we can not place statements after throw statement. It leads to compile time error Unreachable code




  •  We can throw user defined exception using throw keyword.

  1. //Custom exception
  2. package com.instanceofjavaforus;
  3. public class InvalidAgeException extends Exception {
  4.  InvaidAgeException(String msg){
  5.  super(msg);
  6.  }
  7. }
     

  1. package com.exceptions;
  2. public class ThrowDemo {

  3. public boolean isValidForVote(int age){
  4.  
  5.  try{
  6.    if(age<18){
  7.   throw new InvalidAgeException ("Invalid age for voting");
  8. }
  9.  }catch(Exception e){
  10.  System.out.println(e);
  11.  }
  12.   return false;
  13.  }
  14.  
  15. public static void main(String agrs[]){
  16.  
  17.  ThrowDemo obj= new ThrowDemo();
  18.    obj.isValidForVote(17);
  19.  
  20.   }
  21. }


 Output:


  1.  exceptions.InvalidAgeException: Invalid age for voting
  • We can throw predefined exceptions using throw keyword


  1. package com.instanceofjava;
  2.  
  3. public class ThrowKeyword{
  4. public void method(){
  5.  
  6.  try{
  7.   
  8.   throw new NullPointerException("Invalid age for voting");
  9. }
  10.  }catch(Exception e){
  11.  System.out.println(e);
  12.  }
  13.  }
  14.  
  15.   public static void main(String agrs[]){
  16.  
  17.  ThrowKeyword obj= new ThrowKeyword();
  18.  
  19.    obj.method();
  20.  
  21.   }
  22. }

 Output:


  1.  java.lang.NullPointerException: Invalid age for voting