Wednesday, January 19, 2011

Data Driven Testing Using Testng

package shakeWeight;

import com.thoughtworks.selenium.*;

import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import java.io.File;
import jxl.*;

public class Yellow extends SeleneseTestCase {
    boolean Condition = true;

    @BeforeClass
    public void setUp() throws Exception {
        SeleniumServer seleniumserver = new SeleniumServer();
        seleniumserver.boot();
        seleniumserver.start();
        setUp("http://test04.yellowbook.com", "*firefox");
        selenium.open("/yellow-pages/?what=lawyers&where=Denver%2C+CO");
        selenium.windowMaximize();
        selenium.windowFocus();
    }

    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception {
        Object[][] retObjArr = getTableArray(
                "test\\Resources\\Data\\Book1.xls", "DataPool", "yellowbook");
        return (retObjArr);
    }

    @Test(dataProvider = "DP1")
    public void testDataProviderExample(String what1, String where1,
            String search1) throws Exception {
        // enter the movie title

        selenium.type("what", what1);
        selenium.type("where", where1);
        for (int second = 0; second < 60; second++) {
            try {

                if (selenium.isElementPresent("//input[@value='Search']"))
                    ;
                selenium.click("//input[@value='Search']");
                Condition = true;
                selenium.waitForPageToLoad("20000");
                break;

            } catch (Exception ignore) {
            }
            pause(1000);
        }
        assertTrue(Condition);
        // they keep switching the go button to keep the bots away
        //boolean gold_ad = selenium.isElementPresent("//div[@class=\"adFlag-gold\"]");
        //System.out.println("Searching for the text :: "+search1 +" Is Available :: "+selenium.isTextPresent(search1)+" IS Gold-Add :: "+gold_ad);
        //selenium.isTextPresent(search1);
        //a[contains(text(),'Shaffer Dan & Diana')]
        //li[@id='divInAreaSummary_2']/div[2]/div[1]/h2/a
        /*for(int i=1; i<=10; i++){
            boolean gold_add = selenium.isElementPresent("//li[@id='divInAreaSummary_"+ i +"']/div[6][@class=\"adFlag-gold\"]");
            boolean business_listing = selenium.isTextPresent("//li[@id='divInAreaSummary_"+ i +"']/div[2]/div[1]/h2/a[contains(text(),"+ search1 +")]");
            System.out.println("************* "+ i +"***************" + search1);
            System.out.println("Gold Add :: "+gold_add +" Businessh Listing ::"+business_listing);
            System.out.println("******************************");*/
       
       
        if (selenium.isTextPresent(search1)) {
           
                boolean gold_ad = selenium.isElementPresent("//div[@class=\"adFlag-gold\"]");
                boolean text = selenium.isElementPresent("//a[contains(text(),search1)]");
                System.out.println("Gold_add :: " + gold_ad);
                System.out.println("text :: " + text);
                   
                verifyTrue(selenium
                        .isElementPresent("//div[@class=\"adFlag-gold\"]"));
               
                           } else {
                //assertTrue(selenium.isElementPresent(search1));
                System.out.println("element not found");
            }
       
       
       


    }

    @AfterClass
    public void tearDown() {
        selenium.close();
        selenium.stop();
    }

    public String[][] getTableArray(String xlFilePath, String sheetName,
            String tableName) throws Exception {
        String[][] tabArray = null;

        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName);
        int startRow, startCol, endRow, endCol, ci, cj;
        Cell tableStart = sheet.findCell(tableName);
        startRow = tableStart.getRow();
        startCol = tableStart.getColumn();

        Cell tableEnd = sheet.findCell(tableName, startCol + 1, startRow + 1,
                100, 64000, false);

        endRow = tableEnd.getRow();
        endCol = tableEnd.getColumn();
        System.out.println("startRow=" + startRow + ", endRow=" + endRow + ", "
                + "startCol=" + startCol + ", endCol=" + endCol);
        tabArray = new String[endRow - startRow - 1][endCol - startCol - 1];
        ci = 0;

        for (int i = startRow + 1; i < endRow; i++, ci++) {
            cj = 0;
            for (int j = startCol + 1; j < endCol; j++, cj++) {
                tabArray[ci][cj] = sheet.getCell(j, i).getContents();
            }
        }

        return (tabArray);
    }

}// end of class
The Excel sheet has to be placed in the path which is been specified in the DP1 dataprovider 
"test\\Resources\\Data\\Book1.xls", "DataPool", "yellowbook"

No comments:

Post a Comment