Tuesday, 24 September 2013

Simple Java Tips | Selenium Uses

1| Handling Exception | Assertion

Multiple Catch Blocks
Multiple catch avoid quit tests from various Exception

try
{
   assertEquals(actual, expected);
}catch(Exception e)
{
   System.out.println("...");
}catch(NullPointerException e)
{
   System.out.println("...");
}catch(IOException e)
{
   System.out.println("...");
}catch(ComparisonFailure e)
{
   System.out.println("...");
}


Finally Keyword
The finally keyword always executes

try
{
   assertEquals(actual, expected);
}catch(Exception e)
{
   System.out.println("...");
}finally
{
   System.out.println("...");
}


2| Java Arrays

Store values as a list of arrays for assertion.

#1
List<WebElement> savedItem = driver.findElement(By.id(Value)).findElements(By.tagName("li"));
ArrayList<String> storelist = new ArrayList<String>();
for(int i=0; i<savedItem.size(); i++) 
{
String d = savedItem.get(i).findElement(By.id(Value)).getText();
storelist.add(d);
System.out.println(storelist.get(i));
}

#2
String content[]={"string1","string2","string3","string4","string5"};
for(int i=0; i<content.length; i++){
if(isElementPresent(By.cssSelector("ul#"+content[i]+" > li")))
{
// Perform your code
}


3| Date Format

Date Format can be asserted using Java DateFormat.

String element = driver.findElement(By.xpath(Value)).getText();    
String Result = element.substring(6);
System.out.println(Result);   
    
DateFormat format = new SimpleDateFormat("MMM dd, yyyy");  // Date Format can be modified link
Date dif = format.parse(Result);
System.out.println(dif);

_
Sep 4, 2013
Wed Sep 04 00:00:00 IST 2013


4| Random Select

Elements can be selected randomly using Random().

List<WebElement> element = driver.findElement(By.id(Value)).findElements(By.tagName("li"));
    
Random r=new Random();
int index = r.nextInt((element.size()-1)-0+1)+0;  // int index = r.nextInt(max - min + 1) + min;    
String id = element.get(index).getAttribute(Value);
System.out.println(id);


5| Sort Arraylist

Elements can be sorted either in ascending order or descending order.

List<WebElement> savedItem = driver.findElement(By.id(Value)).findElements(By.tagName("li"));
ArrayList<String> storelist = new ArrayList<String>();
for(int i=0; i<savedItem.size(); i++) 
{
String d = savedItem.get(i).findElement(By.tagName("a")).getText();
storelist.add(d);
System.out.println(storelist.get(i));
}

Collections.sort(storelist); // Ascending order
System.out.println(storelist);
Collections.reverse(storelist); // Descending order

4 comments:

  1. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
    When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    thanks a lot for taking a time to share a wonderful article.

    Best Selenium Training Institute in Chennai


    Selenium Training in Velachery

    ReplyDelete
  2. Automation testing mainly focus on Java training in chennai and needs java knowledge.So we need to get Java training in chennai to be expert in selenium. All the training were listed in software testing training in chennai

    ReplyDelete
  3. This is really very nice and informative article Thanks for sharing this type of article & keep posting amazing article. keep it up.

    technical updates
    Latest updates






    ReplyDelete