简体   繁体   中英

How to use different locator in framework without using switch() case?

I want to use a different locator in the framework without using a switch-case because, when I use a switch-case, it asks to change JDK1.6 to 1.7. Can anyone please let me know how I can use a different locator in the framework?

 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;



    }

}

This is what i would ideally do, hope this helps

public WebElement getElement(By locator){

WebElement temp = null;

temp = driver.findElement(locator);

return temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM