Showing posts with label Selenium RC. Show all posts
Showing posts with label Selenium RC. Show all posts

Monday, 12 September 2011

Using Multiple @Test TestNG Annotations

Below is the script which is using multiple @Test TestNG Annotations..

import static org.testng.Assert.assertEquals;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import org.testng.annotations.*;
public class Second {
  
    public static Selenium selenium;
  
    @Test
      
        public static void sample_1() throws InterruptedException {
      
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
      
        selenium.start();
      
        selenium.open("http://www.google.co.in");
      
        System.out.println(a);
      
        selenium.type("id=lst-ib", "Testing");
          
        selenium.click("name=btnG");
      
        Thread.sleep(1000);

        }

    @Test
        public static void sample_2() throws InterruptedException  {

        assertEquals(selenium.getTitle(), "Testing - Google Search");

        Thread.sleep(1000);

        selenium.click("css=em");

        Thread.sleep(1000);

        assertEquals(selenium.getTitle(), "Software testing - Wikipedia, the free encyclopedia");

        Thread.sleep(3000);

        selenium.click("css=#ca-talk>span>a");      

        }
    }


Sample JUnit Test Suite

Test Suite using JUnit....
 

Test1.java
------------------------------------------------------------
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.*;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class Test1 {

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

@Test
public void testText1() throws Exception {
selenium.open("URL");
//* Do the Operation

*/

}
}

/***********************
Test2.java
***************/

public class Test2 {

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

@Test
public void testText1() throws Exception {
selenium.open("URL");
//* Do the Operation

*/

}
}


/***********************

Finally The Test Suite

***************/

package com.Testscripts;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {

public static Test suite()
{
TestSuite nag = new TestSuite();

nag.addTestSuite( Test1.class);
nag.addTestSuite( Test2.class);
return nag;
}

public static void main(String arg[])
{
TestRunner.run(suite());

}
}
------------------------------------------------------------
Run the TestSuite.java script ...it will automatically run the tests(Test1 and Test2)

Selenium RC + JUnit Sample Script

Below is the sample script using JUnit 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class yahoo{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
    selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
    seleniumServer = new SeleniumServer();
    seleniumServer.start();
    selenium.start();
    }
@Test
    public void testText1() throws Exception {
    selenium.open("http://www.yahoo.com");
    selenium.windowMaximize();
    }
@After
    public void tearDown() throws Exception {
    selenium.stop();
    seleniumServer.stop();
    }
}

Different flavours of browsers

  *pifirefox
  *piiexplore
  *firefox
  *mock
  *firefoxproxy
  *chrome
  *iexploreproxy
  *iexplore
  *firefox3
  *safariproxy
  *googlechrome
  *konqueror
  *firefox2
  *safari
  *piiexplore
  *firefoxchrome
  *opera
  *iehta
  *custom

Export to excel sheets

Do you want to write results/export run-time Data  to excel sheets ?

Below is the sample script ...

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class WriteExcel2 {
public Selenium selenium;

public static void main(String arg[]) throws IOException, BiffException, RowsExceededException, WriteException{

//Create File output stream object

    FileOutputStream fo=new FileOutputStream("C:T2.xls");
    WritableWorkbook wwb=Workbook.createWorkbook(fo);
    WritableSheet ws=wwb.createSheet("Results", 0);

// define label and asign values

    Label l = new Label(1, 1, "hi");
    Label l1 = new Label(2, 1, "Result");

//pass values to respective cell

    ws.addCell(l);
    ws.addCell(l1);   

// Close the objects

    wwb.close();
    fo.close();
}

Monday, 5 September 2011

Create connection with database and fetch the values

Following script will explain how to create connection with database and fetch the values
public class DB_Connection_SQL {

public static String url = "jdbc:sqlserver://000.000.0.000:0000;DatabaseName=TEST_DB" ;
public static void main(String [] args){

Connection con = DriverManager.getConnection(url,"UN","PWD");

// Create statement object which should use SQL statement.
Statement stmt = con.createStatement();

// Send SQL SELECT statements to the database which returns the //requested information as rows of data in a Result Set object.
ResultSet res = stmt.executeQuery( "select top 1 name from Emp" );

// Fetch value of "name" from "result" object.
String Ename = res.getString( "name" );

// Use the email Address value to login to application.
selenium.type( "userID" , Ename);
}
}
Note:Below are should import to run above script
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;