Hnadling webtable using web driver



//Handling WebTable with verifications

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WebTable_SalesForce {
      
       WebDriver driver;
       @Test
       public void test() {
              driver=new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
              driver.get("http://www.salesforce.com");
              driver.findElement(By.id("button-login")).click();
              driver.findElement(By.id("username")).sendKeys("manu.immadi7373@gmail.com");
              driver.findElement(By.id("password")).sendKeys("selenium@123");
              driver.findElement(By.id("password")).sendKeys(Keys.ENTER);
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
              driver.findElement(By.xpath("//form[@id='hotlist']/table/tbody/tr/td[2]/input")).click();
              String firstName="Pavan";
              String lastName="Kalyan";
              String company="Tollywood";
              driver.findElement(By.id("name_firstlea2")).sendKeys(firstName);
              driver.findElement(By.id("name_lastlea2")).sendKeys(lastName);
              driver.findElement(By.id("lea3")).sendKeys(company);
              driver.findElement(By.xpath("//td[@id='bottomButtonRow']/input")).click();
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
             
              driver.findElement(By.xpath("//input[@name='go' and @value='Run Report']")).click();
              String xpath_Start="//*[@id='fchArea']/table/tbody/tr[";
              String xpath_End="]/td[1]";
              boolean recordfound=false;
              int i=2;
              while(isElementPresent(xpath_Start+i+xpath_End))
              {
                     String fname=driver.findElement(By.xpath(xpath_Start+i+xpath_End)).getText();
                     if(fname.equals(firstName)) {
                           //check the lastname
                           String lastnamecol=xpath_Start + i + xpath_End.replace("td[1]","td[2]");
                           String lname=driver.findElement(By.xpath(lastnamecol)).getText();
                           if(lname.equals(lastName)) {
                                  System.out.println("Both are same " +firstName + lastName);
                                  recordfound=true;
                                  i++;
                                  break;
                           }
                     }
              }
       Assert.assertTrue(recordfound, "Record NotAvilable");
              }
       public boolean isElementPresent(String objxpath) {
              int count = driver.findElements(By.xpath(objxpath)).size();
              if(count==0)
                     return false;
              else
                     return true;
       }
      
}

Verify broken links using Web driver




import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrokenLinks {
    public static int invalidLink;
    String currentLink;
    String temp;
    public static void main(String[] args) throws IOException {
       //Launch The Browser
        WebDriver driver=new FirefoxDriver();
        //Enter Url
        driver.get("http://www.excellenceq.com");
        //Get all the links url
        List<WebElement> ele=driver.findElements(By.tagName("a"));
        System.out.println("size:" +ele.size() );
        for(int i=0;i<ele.size();i++) {
            System.out.println(ele.get(i).getAttribute("href"));
            int statusCode=0;
            try{
            statusCode=getResponseCode(ele.get(i).getAttribute("href"));
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            if(statusCode==404) {
                System.out.println("InvalidLink ********** :"+ele.get(i).getAttribute("href"));
            }else {
                System.out.println(":ValidLinks:"+ele.get(i).getAttribute("href"));
            }
        }


    }
    //The below script will identify all the broken links(if any) in excelenceq.com and store the 404 URLs in List. It verifies the server status, any links giving 404 status.
    public static int getResponseCode(String urlString) throws IOException{
        URL u = new URL(urlString);
        HttpURLConnection h =  (HttpURLConnection)  u.openConnection();
        h.setRequestMethod("GET");
        h.connect();
        return h.getResponseCode();
    }

}

Manoj Kumar
manojqa5006@gmail.com