如何在不使用switch()的情况下在框架中使用不同的定位器?

时间:2014-11-27 03:53:29

标签: java selenium-webdriver

我想在框架中使用不同的定位器而不使用switch-case,因为当我使用switch-case时,它要求将JDK1.6更改为1.7。谁能告诉我如何在框架中使用不同的定位器?

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

    // other constants

    private WebDriver driver;
    private Properties properties;
    /*private WebElement we;*/

    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 Exception {
        MainClass main = new MainClass();

        main.handleTestSuite();
    }

    private void handleTestSuite() throws Exception {
        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) throws Exception {
        ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH);
        testScenarios.setSheetName("Login");
        testScenarios.columnData();
        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("FRMNameKK=" + framWork + ",Operation=" + operation +
                               ",Value=" + value);

            handleObjects(operation,value,framWork);
        }
    }

    private void handleObjects(String operation,String value,String framWork) throws Exception 
    {
        System.out.println("HandleObject--> "+framWork);
        ExcelHandler objectRepository = new ExcelHandler(OBJECT_REPOSITORY_PATH, "OR");
        objectRepository.columnData();
        int rowCount = objectRepository.rowCount();
        System.out.println("Total Rows in hadleObject=" + rowCount);

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

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


            if(framWork.equalsIgnoreCase(frameWorkName))
            {   
                    operateWebDriver(operation,Locator,value,ObjectName);

            }       
        }
    }

    private void operateWebDriver(String operation,String Locator,String value, String objectName) throws Exception 
    {
        System.out.println("Operation execution in progress");
        WebElement temp=getElement(Locator,objectName);
        if (operation.equalsIgnoreCase("SendKey")) 
        {                   
            temp.sendKeys(value);            
        } 

        if (operation.equalsIgnoreCase("Click")) 
        {        
            temp.click();            
        }

    }


    public WebElement getElement(String locator,String objectName) throws Exception
    {
        WebElement temp = null;
        if(locator.equalsIgnoreCase("id"))
        {
            temp = driver.findElement(By.id(objectName));
            return temp;

        }else if(locator.equalsIgnoreCase("xpath")) {

            temp = driver.findElement(By.xpath(objectName)); 

            System.out.println("xpath temp ----->" +temp);

            return temp;
        }else if(locator.equalsIgnoreCase("link")){

            temp= driver.findElement(By.className(objectName));
            return temp;
        }else if(locator.equalsIgnoreCase("partialLinkText")){

            temp=driver.findElement(By.partialLinkText(objectName));
            return temp;
        }
        return temp;



    }

}

1 个答案:

答案 0 :(得分:0)

这是我理想的做法,希望这会有所帮助

public WebElement getElement(By locator){

WebElement temp = null;

temp = driver.findElement(locator);

return temp;
}