Saturday 8 March 2014

TestNG Parameterized Test using @DataProvider

One of the bright feature of TestNG is that to allows pass parameter to test. It can be achieve in two ways...

1. using @DataProvider annotation and
2. with testng.xml

Here is a demonstration how to parameterized test using @DataProvider annotation.

The Data Provider method returns an array of Objects(Objects[][])  where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method.

----------------------------------------------------------------------------------

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGparameter {

WebDriver driver;
@BeforeTest
public void atBefore() throws Exception{
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test(dataProvider = "testData")          //Calling Data Provider
public void atTest(String url, String userID_locator, String userID, String passwd_locator, String passwd, String signIN_locator) throws Exception{
String expected_PageTitle = "Inbox";   //Expected Result
driver.get(url);
Thread.sleep(1000);
System.out.println(driver.getTitle());   
driver.findElement(By.id(userID_locator)).sendKeys(userID);
driver.findElement(By.id(passwd_locator)).sendKeys(passwd);
driver.findElement(By.id(signIN_locator)).click();
Thread.sleep(1000);
String actual_PageTitle = driver.getTitle();  //Actual Result
Assert.assertEquals(actual_PageTitle, expected_PageTitle);  //Compare actual with expected result
}
@DataProvider(name = "testData")         //Declare Data Provider
public Object[][] methodDataProvider() throws Exception{
Object [][]xData = new Object[1][6];
xData[0][0] = "http://www.gmail.com";     //url
xData[0][1] = "Email";                            // userID_locator 
xData[0][2] = "Gmail_User";                    // userID 
xData[0][3] = "Passwd";                          // passwd_locator
xData[0][4] = "Password";                       // passwd
xData[0][5] = "signIn";                           //signIN_locator
return xData;
}
@AfterTest
public void atAfter() throws Exception{
driver.quit();
}
}



Tuesday 4 March 2014

How to check broken links on a web page


import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebPageLinks {
    WebDriver driver;
    String url;
  
    @Before
    public void atBefore() throws Exception{
      
        driver = new FirefoxDriver();
        url = "http://www.google.com";
        driver.manage().window().maximize();
    }
     
    @Test
    public void atTest() throws Exception{
      
        driver.get(url);
      
        driver.findElement(By.id("gbqfq")).sendKeys("JAVA");    //Write 'JAVA' keyword in search text box
      
        driver.findElement(By.id("gbqfb")).click();
        Thread.sleep(4000);

         List<String> Linkarray = new ArrayList<String>();
       
          List<WebElement> Linklist = driver.findElements(By.tagName("a"));    //Finding all element on the page with 'a' tag.
       
          for (WebElement link : Linklist) {
          
           String links = link.getText();
                
           if(links.isEmpty()){
               System.out.println("No Text.");    //Checking if any link doesn't have text
           }
          else{
                 Linkarray.add(links );
                System.out.println(links);
           }
         
          }
         System.out.println("=============================================");
        
          for(int i=0; i<Linkarray.size(); i++)
        
          {
             System.out.println("Element at " + i  + " is : "+ Linkarray.get(i));
          }
              
          for (String linkToTest : Linkarray){
              try{
                  driver.findElement(By.linkText(linkToTest)).click();    //Clicking on the link
              }
              catch(Exception e){
                
                  System.out.println("The link "  + linkToTest.substring(0) + " is not found in the page.");  //Print if any link not present on the page. Some links might be change in this case when navigate on the main page
              }
            
              Thread.sleep(15000L);
            
              if(driver.getTitle().contains("Problem")) {          //Checking if page title having 'Problem' word
                  
                          System.out.println("Fail");
                    }
                 
              else{
                     System.out.println("pass");
                   }
            
                    driver.navigate().back();        //Navigate on main page
                    Thread.sleep(5000L);
                   }
    }
  
}

-----------------------------------------------------------------------------------------------------------
How to handle iFrames in WebDriver

An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page.

An <iframe> is used for containing (X)HTML documents in other (X)HTML documents. This enables updates of parts of a website while the user browses, without making them reload the whole thing.

If name of iFrame is given then it is easy to deal with
    driver.switchTo().frame("name of iframe");

But in case if name is not given then follow these steps...

First get all elements starting with 'iframe' tag in a list
    List<WebElement> myL = driver.findElements(By.tagName("iframe"));  

Print the size of list (so that you know that how many iframes exist on the web page)
    System.out.println(myL.size());

Use a for loop to get html code of iFrame

    for(int i=0; i<myL.size(); i++){
      
        driver.switchTo().frame(i);                                                
      
        System.out.println("******* iFrame + " +i + " start ***********");

        System.out.println(driver.getPageSource());          //Printing HTML code of iFrame

        System.out.println("******* iFrame + " +i + " end ***********");
      
        driver.switchTo().defaultContent();                     // Switching WebDriver to default content
    }

Once you get the your iFrame html code with frame number then switch on it using

  driver.switchTo().frame(7);

-------------------------------------------------------------------------------------------------------------