Automation Using Selenium Webdriver

Wednesday 23 November 2016

Capture and Navigate All The Links on Webpage Using Selenium Webdriver

In case of Sanity Check we can use this Code to check whether all links are functional or not.
Its so simple to capture links and navigate
public static void navigateToAllLinks() throws InterruptedException {
List<WebElement> linksize = driver.findElements(By.tagName(“a”));
int linksCount = linksize.size();
System.out.println(“Total no of links Available: ” + linksCount);
String[] links = new String[linksCount];
System.out.println(“List of links Available: “);
// print all the links from webpage
for (int i = 0; i < linksCount; i++) {
links[i] = linksize.get(i).getAttribute(“href”);
System.out.println(linksize.get(i).getAttribute(“href”));
}
// navigate to each Link on the webpage
for (int i = 0; i < linksCount; i++) {
driver.navigate().to(links[i]);
System.out.println(driver.getTitle());


}
}

Java program To Count the number of words in a String

Java program To Count the number of words in a String::



  1. package com.javatutorial;
  2.  
  3. public class CountNumberofWords {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7. String s="";
  8. int count=0;
  9.  
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Please enter a String");
  12.  s=in.nextLine();
  13.  
  14. char ch[]= new char[s.length()];    
  15.  
  16. for(int i=0;i<s.length();i++)
  17. {
  18.  
  19.     ch[i]= s.charAt(i);
  20.  
  21.     if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
  22.         count++;
  23.  
  24. }
  25. System.out.println("Number of words in given String: "+count);
  26.  
  27. }

  28. }




Output:
  1. Please enter a String
  2. Java Tutorial
  3. Number of words in given String: 2

Remove Character from string in java



#1: Java Program to Remove all occurrences of a string  

  1. package com.instanceofjava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.     str = str.replace("a", "");
  8.     System.out.println(str);
  9. }
  10. }
Output:
  1. Jv

#2: Java Program to Replace First occurance of Specific index char in a String



  1. package com.injava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.  
  8. //String result = str.substring(0, index) + str.substring(index+1);
  9.  
  10.   String result = str.substring(0, 1) + str.substring(1+1);
  11.   System.out.println(result);
  12.  
  13. }
  14. }


Output:
  1. Jva



#3: Java Program to Remove all Numbers from a string.



  1. package com.instanceofjava;
  2.  
  3. class RemoveNumberString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance12ofjava143";
  7.   str = str.replaceAll("[0-9]","")
  8.   System.out.println(str);
  9.  
  10. }
  11. }
Output:
  1. Instanceofjava