Pages

Search This Blog

Monday, May 17, 2010

How to pass parameters to JUNIT or TestNG test case.

You can parametrize your test cases using excel sheet. With the help you TestNG we can pass different set of data to our test cases by following steps

1. create a data file excel rename column as username and fill below data like

username  
test1         
test2
test3
test4


2. create a dsn through control pannel--> administrative tool--> Data source (ODBC) --> select system dsn --> click add
then select "dirver do microsoft excel" select workbook your data file which you created above.

now your data source and provider is ready now connect this in your test cases using java class for

Class.forName("sun.jdbc.odbc.
JdbcOdbcDriver");


3. Write the test cases

import org.openqa.selenium.server.RemoteControlConfiguration;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.*;
import org.testng.annotations.*;
public class TestExcel extends SeleneseTestBase {
@BeforeClass
public void setUp()throws Exception{

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.trustAllSSLCertificates();
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
seleniumServer.start();
selenium.start();
}
@Test
public  void testread()throws Exception{
// Connection connection = null;


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection( "jdbc:odbc:nk" );
// Connection con = DriverManager.getConnection( "jdbc:odbc:nk" ); here you write your driver which you created using ODBC connecting excel workbook.

Statement st = con.createStatement();
ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );

ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();

while (rs.next()) {

for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
selenium.open("/");

pause(5000);
selenium.click("link=Sign in");
selenium.waitForPageToLoad("30000");


try {
if (selenium.isElementPresent("//a[@id='overridelink']"))
selenium.click("//a[@id='overridelink']");
}
catch (Exception e) {}
pause(30000);
selenium.type("userid", columnValue);
selenium.type("pass","password");
selenium.click("//button[@class='bfbt' and @type='submit']");
pause(8000);
String wc = selenium.getTable("//div[@id='dynamicmenu-hdrCtr']/table[1].0.1");
System.out.print(wc);

selenium.click("link=Sign out");
selenium.waitForPageToLoad("20000");
selenium.isTextPresent("Welcome!");

}
System.out.println("");   

}

st.close();
con.close();

} catch(Exception ex) {
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
}


10 comments:

  1. How to pass parameters to JUNIT or TestNG test case?
    This example is not working properly.
    It is showing error in getting user name and password.

    ReplyDelete
  2. sudhakar3.qa@gmail.comDecember 7, 2011 at 9:46 PM

    HOW TO ENTER THE TEXT INTO RICH TEXT BOX? PLEASE SUGGEST U HAVE ANY SOLUTION ABOUT THIS?

    ReplyDelete
  3. Hi Sudhakar,
    Go through this post http://automationtricks.blogspot.com/2010/09/how-to-write-in-iframe-in-selenium.html
    you will get an idea to enter text in rich text box.
    Thanks

    ReplyDelete
  4. String wc = selenium.getTable("//div[@id='dynamicmenu-hdrCtr']/table[1].0.1");

    This code through the Exception

    Pleaase help me

    Thanks
    Shubham

    ReplyDelete
  5. Hi,
    I have a browse button on my application page, which takes accepts Image file then i need to save the

    application.

    I need to do this operation only on IE browser ( So i’m using Eclipse) , as my application don’t support any

    other browser not even FF.

    I have tried with the below code:

    selenium.type(“quiz_image”, “C:\\Test photo\\C.jpg”);

    But my script is escaping the above part and going to the next step without performing it.

    Pls suggest me some solution asap. Its really urgent for me:)

    ReplyDelete
  6. you need to create an ant script

    ReplyDelete
  7. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  8. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  9. Interest to learn Selenium Online Training by Real Time Software Testing Professional Trainer having 10+ Years of Expert.
    Selenium Training in Hyderabad

    ReplyDelete