Monday 30 September 2013

Checking for Broken images on Web Page

'Function to check for broken images on Page

public function checkbrokenimages(opage)
pgtitle=opage.getroproperty("title")
set allimages=opage.object.images
icount=allimages.length-1
for i =0 to icount
set curimg=allimages.item(i)
shtml="HTML="&curimg.outerHTML
if curimg.filesize=-1 then
'the image didnt load
reporter.reportevent micfail,pgtitle& "Broken Image",shtml
else
reporter.reportevent micpass,pgtitle& "Valid Image",shtml
end if
next
end function

checkbrokenimages  browser("micclass:=browser","index:=0").page("micclass:=page")

Note:We have used the DOM in this example given above because QTP child objects function will not search in side the web table objects. Some of the images the web table might get ignored while using the childobjects method



Thursday 19 September 2013

Selenium Grid Installations

Download Required

1) selenium-server-standalone-ver.jar : All client machines and Server Machine


Steps :

Step 1 : Start the hub : Start hub on the Server machine with below command

java -jar selenium-server-standalone-2.28.0.jar -role hub

The hub will automatically start-up using port 4444 by default. To change the default port, you can add the optional parameter -port when you run the command. You can view the status of the hub by opening a browser window and navigating to: http://localhost:4444/grid/console

Step 2: Start the nodes : Regardless on whether you want to run a grid with new WebDriver functionality, or a grid with Selenium 1 RC functionality, or both at the same time, you use the same selenium-server-standalone jar file to start the nodes.

java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/register

-browser  browserName=firefox,version=3.6,maxInstances=5,platform=LINUX : This can be supplied .
For Windows : platform=WINDOWS
Note: The port defaults to 5555 if not specified whenever the "-role" option is provided and is not hub.

Using grid to run tests

 For WebDriver nodes, you will need to use the RemoteWebDriver and the DesiredCapabilities object to define which browser, version and platform you wish to use. Create the target browser capabilities you want to run the tests against:

 

DesiredCapabilities capability = DesiredCapabilities.firefox();

 

Pass that into the RemoteWebDriver object:
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
The hub will then assign the test to a matching node.
A node matches if all the requested capabilities are met. To request specific capabilities on the grid, specify them before passing it into the WebDriver object.
capability.setBrowserName();
capability.setPlatform();
capability.setVersion()
capability.setCapability(,);
Example: A node registered with the setting:
 -browser  browserName=firefox,version=3.6,platform=LINUX
will be a match for:
capability.setBrowserName(“firefox ); 
capability.setPlatform(“LINUX”);  
capability.setVersion(“3.6”);
and would also be a match for
capability.setBrowserName(“firefox ); 
capability.setVersion(“3.6”);

Code Snippet : 

import java.net.MalformedURLException;
import java.net.URL;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;


public class testremote {

    @Test
    public void test1() throws MalformedURLException{
  
    DesiredCapabilities capability = DesiredCapabilities.firefox();
    capability.setBrowserName("firefox");
    capability.setVersion("18.0.2");
    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    driver.get("http://google.com");
  
  
}
  
}

 

Connecting TO Database(Mysql) In Selenium

Prerequisites:
Step 1:Down load  Mysql database(http://dev.mysql.com/downloads/)
Step2:Down load  Mysql-connector-java JAR(http://downloads.mysql.com/archives.php?p=mysql-connector-java-5.0&o=other)
Step3:Create your own database in Mysql and Create Tables
Step4:In eclipse create new project and under your project attach Mysql-connector-java  JAR files

Code follows:
import java.sql.*;
public class Database_connection {
           
public static void main(String[] args) throws SQLException {
                          Connection conn = null;
                          String url = "jdbc:mysql://localhost:3306/"; (Mysql server runs by default in this port -3306)
                          String dbName = "test";
                          String driver = "com.mysql.jdbc.Driver";
                          String userName = "root"; (Default user Name in Mysql)
                          String password = "sudhakar"; (Give the same pass word while you create database in Mysql)
                         
                          try{
                              Class.forName(driver).newInstance();// create object of Driver
                             conn = DriverManager.getConnection(url+dbName,userName,password);
                                      // connection will be established
                                         Statement stmt = conn.createStatement();
                                      ResultSet rs = stmt.executeQuery("select * from users");
                                     
                                      while(rs.next()){
       System.out.println(rs.getString(1) + "-- "+rs.getString(2)+" -- "+rs.getString(3)); 
                                      }
                                       System.out.println("*********************************");
                                      // *****************PREPARED STATEMENT**************
                                      PreparedStatement pstmt = conn.prepareStatement("select * from users where name = ? and  sex =?");
                                      pstmt.setString(1, "vagdevi");
                                      pstmt.setString(2, "F");
                                      ResultSet rs1 = pstmt.executeQuery();
                                     
                                      while(rs1.next())
 System.out.println(rs1.getString(1) + "-- "+rs1.getString(2)+" -- "+rs1.getString(3)); 
                                      }                                  
                                     
                          //              ********************Add row Insert************************
                        pstmt = conn.prepareStatement("insert into users values (?,?,?)");
                        pstmt.setString(1, "Jeniffer");
                        pstmt.setString(2, "US");
                        pstmt.setString(3, "F");
                                         int i=pstmt.executeUpdate();
                                        if(i==1){
                                                System.out.println("inserted the record");
                                        }
                                        
                                     
                          }catch(Exception e){
                                                e.printStackTrace();
                          }finally{
                                      conn.close();
                          }
                       
                      }
 }