Why is the webdriver open for each operation?

advertisements

I am beginner in Selenium, there are two separate .xls file file one for GmailTestSuite.xls and other for objectrepository.xls.

I have created MainClass, in it I have written a code which read both .xls file, also I've open the driver in it and perform operation. But problem is that it continuously open new driver but don't perform any operation.

Please suggest and let me know where I'm going wrong.

public class MainClass {

    static Properties properties= null;

    public static void main(String[] args) throws IOException, BiffException{
        // TODO Auto-generated method stub

        ReadPropertyFile readConfigFile= new ReadPropertyFile();
        properties= readConfigFile.loadPropertiess();

        ExcelHandler testSuite= new ExcelHandler("D:\\GmailTestSuite.xls", "Suite");
        testSuite.columnData();

        int rowCount= testSuite.rowCount();
        System.out.println("Total Rows="+rowCount);

        for(int i=1;i<rowCount;i++)
        {
            String executable= testSuite.readCell(testSuite.getCell("Executable"), i);
            System.out.println("Executable="+executable);

            if(executable.equalsIgnoreCase("y")){
            // exe. the process
                String scenarioName= testSuite.readCell(testSuite.getCell("TestScenario"), i);
                System.out.println("Scenario Name="+scenarioName);
                ExcelHandler testScenarios= new ExcelHandler("D:\\GmailTestSuite.xls", scenarioName);

                int rowWorkBook1= testScenarios.rowCount();
                for(int j=1; j<rowWorkBook1;j++){
                    String framWork= testScenarios.readCell(testScenarios.getCell("FrameworkName"), j);
                    String operation = testScenarios.readCell(testScenarios.getCell("Operation"), j); //SendKey
                    String value= testScenarios.readCell(testScenarios.getCell("Value"), j);
                    System.out.println("FRMName="+framWork+",Operation="+operation+",Value="+value);

                    ExcelHandler objectRepository= new ExcelHandler("D:\\objectrepository.xls", "OR");
                    objectRepository.columnData();
                    int rowCount1= testSuite.rowCount();
                    System.out.println("Total Rows="+rowCount1);

                    for(int k=1;k<rowCount;k++){
                        String frameWorkName= objectRepository.readCell(objectRepository.getCell("Executable"), k);
                        String ObjectName= objectRepository.readCell(testScenarios.getCell("ObjectName"), k);
                        String Locator = objectRepository.readCell(testScenarios.getCell("Locator"), k); //SendKey

                        System.out.println("FrameWorkName="+frameWorkName+",ObjectName="+ObjectName+",Locator="+Locator);

                        //ExcelHandler executeOperation = new ExcelHandler(ObjectName, operation, value);
                        File file= new File("D:\\softs\\FF installed\\FF18\\firefox.exe");
                        FirefoxBinary fb = new FirefoxBinary(file);
                        WebDriver driver = new FirefoxDriver(fb,new FirefoxProfile());
                        driver.get("https://www.gmail.com");
                        WebElement we = driver.findElement(By.id("Email"));

                        if(operation.equalsIgnoreCase("SendKey"))
                        {
                            we.sendKeys("[email protected]");
                            we.sendKeys("[email protected]");
                        }   

                        if(operation.equalsIgnoreCase("Click"))
                            we.click();
                    }
                }
            }
        }
    }


A couple of guide lines for better formed code:

  • Break your code into methods that only do one thing each. That way it's easier to manage and nicely compartmentalized, plus you don't enter into indentation hell with such embedded loop and if structures as the one you have over here.
  • Use class variables for things like the WebDriver instance, so you can initialize it once and keep calling on it later.
  • Don't hard code text into the application, use constants. Then you only need to define the text once and can refer to it multiple times. Makes the code much easier to maintain and change, when you don't have to search and replace through entire class, after some details (like a file path) change.

Also, I'm guessing you meant to do the following:

  1. loop the rows in objectRepository in the k-loop, instead of looping the rows in the testSuite again.
  2. get cells from objectRepository rather than from testScenarios when reading the cells from objectRepository

Example:

public class MainClass {
    private static final String BROWSER_PATH = "D:\\softs\\FF installed\\FF18\\firefox.exe";
    private static final String TEST_SUITE_PATH = "D:\\GmailTestSuite.xls";
    private static final String OBJECT_REPOSITORY_PATH = "D:\\objectrepository.xls";
    private static final String ADDRESS_TO_TEST = "https://www.gmail.com";

    private static final By EMAIL_INPUT = By.id("Email");
    // other constants

    private WebDriver driver;
    private Properties properties;

    public MainClass() {
        File file = new File(BROWSER_PATH);
        FirefoxBinary fb = new FirefoxBinary(file);
        driver = new FirefoxDriver(fb, new FirefoxProfile());
        driver.get(ADDRESS_TO_TEST);
    }

    public static void main(String[] args) throws IOException, BiffException {
        MainClass main = new MainClass();

        main.handleTestSuite();
    }

    private void handleTestSuite() {
        ReadPropertyFile readConfigFile = new ReadPropertyFile();
        properties = readConfigFile.loadPropertiess();

        ExcelHandler testSuite = new ExcelHandler(TEST_SUITE_PATH, "Suite");
        testSuite.columnData();

        int rowCount = testSuite.rowCount();
        System.out.println("Total Rows=" + rowCount);

        for (int i = 1; i < rowCount; i++) {
            String executable = testSuite.readCell(testSuite.getCell("Executable"), i);
            System.out.println("Executable=" + executable);

            if (executable.equalsIgnoreCase("y")) {
                // exe. the process
                String scenarioName = testSuite.readCell(testSuite.getCell("TestScenario"), i);
                System.out.println("Scenario Name=" + scenarioName);
                handleScenario(scenarioName);
            }
        }
    }

    private void handleScenario(String scenarioName) {
        ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH, scenarioName);

        int rowWorkBook1 = testScenarios.rowCount();
        for (int j = 1; j < rowWorkBook1; j++) {
            String framWork = testScenarios.readCell(testScenarios.getCell("FrameworkName"), j);
            String operation = testScenarios.readCell(testScenarios.getCell("Operation"), j); // SendKey
            String value = testScenarios.readCell(testScenarios.getCell("Value"), j);
            System.out.println("FRMName=" + framWork + ",Operation=" + operation +
                               ",Value=" + value);

            handleObjects(operation);
        }
    }

    private void handleObjects(String operation) {
        ExcelHandler objectRepository = new ExcelHandler(OBJECT_REPOSITORY_PATH, "OR");
        objectRepository.columnData();
        int rowCount = objectRepository.rowCount();
        System.out.println("Total Rows=" + rowCount);

        for (int k = 1; k < rowCount; k++) {
            String frameWorkName = objectRepository.readCell(objectRepository.getCell("Executable"), k);
            String ObjectName = objectRepository.readCell(objectRepository.getCell("ObjectName"), k);
            String Locator = objectRepository.readCell(objectRepository.getCell("Locator"), k); // SendKey

            System.out.println("FrameWorkName=" + frameWorkName +
                               ",ObjectName=" + ObjectName + ",Locator=" + Locator);

            operateWebDriver(operation);
        }
    }

    private void operateWebDriver(String operation) {
        WebElement we = driver.findElement(EMAIL_INPUT);

        if (operation.equalsIgnoreCase("SendKey")) {
            we.sendKeys("[email protected]");
            we.sendKeys("[email protected]");
        } else if (operation.equalsIgnoreCase("Click")) {
            we.click();
        }
    }
}

If ExcelHandler is your own implementation, you really should move the getCell(String s) method inside the readCell() method to change the call pattern of handler.readCell(handler.getCell("foo"), i) into handler.readCell("foo", i). If it's a library you're using, you can always make a helper method:

private static String readCell(ExcelHandler handler, String cellName, int row) {
    return handler.readCell(handler.getCell(cellName), row);
}


EDIT

Since you're having problems with getting WebDriver to work, simplify things and take everything else out of the equation for now. In order to do that let's ignore all the reading data from .xls files. This is where having different methods for different things makes your design shine, since you can just comment one method call instead of having to comment out 50 lines of code from your one mega method.

Changed the above code a bit (just commented call to the other methods out and omitted them from the snippet, moved the line opening the correct page into constructor and rewrote the operateWebDriver() method a bit):

public class MainClass {
    private static final String ADDRESS_TO_TEST = "https://www.gmail.com";

    private static final By EMAIL_INPUT = By.id("Email");
    private static final By PASSWORD_INPUT = By.id("Passwd");
    private static final By SIGN_IN_BUTTON = By.id("signIn");

    private static final String EMAIL = "[email protected]";
    private static final String PASSWORD = "test123";

    private WebDriver driver;

    public MainClass() {
        File file = new File(BROWSER_PATH);
        FirefoxBinary fb = new FirefoxBinary(file);
        driver = new FirefoxDriver(fb, new FirefoxProfile());
        driver.get(ADDRESS_TO_TEST);
    }

    public static void main(String[] args) throws IOException, BiffException {
        MainClass main = new MainClass();

        //main.handleTestSuite();

        main.operateWebDriver("Click", EMAIL_INPUT);
        main.operateWebDriver("SendKey", EMAIL_INPUT, EMAIL);
        main.operateWebDriver("Click", PASSWORD_INPUT);
        main.operateWebDriver("SendKey", PASSWORD_INPUT, PASSWORD);
        main.operateWebDriver("Click", SIGN_IN_BUTTON);
    }

    private void operateWebDriver(String operation, By element) {
        operateWebDriver(operation, element, null);
    }

    private void operateWebDriver(String operation, By element, String keys) {
        WebElement we = driver.findElement(element);

        if (operation.equalsIgnoreCase("SendKey")) {
            we.sendKeys(keys);
        } else if (operation.equalsIgnoreCase("Click")) {
            we.click();
        }
    }
}

Then once you get WebDriver working, you can start reading the data from the files and using it to operate WebDriver.