java - 如何在 selenium webdriver 框架中定位或使用不同的定位器?

标签 java excel selenium

我是 selenium webdriver 的初学者,我正在尝试通过阅读 excel 表来使用不同的定位器,但在这些定位器中,只需要一个数据,通过查找定位器“id”将其放入一个字段中,当涉及到第二个文本时为此,我们使用“Xpath”定位器,但它没有占用。所以,我的问题是,如果可能的话,如何不使用 switch case 来使用不同的定位器。 下面是我的代码:

public class MainClass {
    private static final String BROWSER_PATH = "D:\\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";

    // 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 IOException, BiffException {
        MainClass main = new MainClass();

        main.handleTestSuite();
    }

    private void handleTestSuite() throws BiffException, IOException {
        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 BiffException, IOException {
        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 BiffException, IOException 
    {
        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) 
    {
        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)
    {
        WebElement temp = null;
        if(locator.equalsIgnoreCase("id"))
        {
            temp = driver.findElement(By.id(objectName));
        }else if(locator.equalsIgnoreCase("xpath")) {

            temp = driver.findElement(By.xpath(objectName)); 
        }       
        if(locator.equalsIgnoreCase("link"))
        {

        }
        return temp;
    }

} 

最佳答案

你需要反射(reflection)。请参阅以下 2 种方法中的示例:

import java.lang.reflect.Method;
...
@Test
public void TestAmazon() {
    driver = new ChromeDriver();
    driver.navigate().to("http://www.amazon.com");

    String locatorType = "id";
    String locatorExpression = "twotabsearchtextbox";           

    By locator = createLocator(locatorType, locatorExpression);                 
    WebElement textbox = driver.findElement(locator);
    textbox.sendKeys("Star Wars: The Digital Movie Collection");
    textbox.sendKeys(Keys.ENTER);

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    locatorType = "className";
    locatorExpression = "nav-logo-link";
    locator = createLocator(locatorType, locatorExpression);    
    driver.findElement(locator).click();
}


private By createLocator(String locatorType, final String locatorExpression) {
    By locator = null;
    Class<By> byClass = By.class;
    Class[] argTypes = new Class[] { String.class };
    try {
        Method m = byClass.getDeclaredMethod(locatorType, argTypes);

        try {
            locator = (By)m.invoke(null,locatorExpression);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return locator;
}

关于java - 如何在 selenium webdriver 框架中定位或使用不同的定位器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467581/

相关文章:

java - 设置listview项目的背景颜色

python - 如何在 python 中的 xlsxwriter merge_range 函数中添加新行?

selenium - 在 Selenium IDE 3.2.3 Chrome 插件中替代 storeEval 命令

java - 单击列表底部获取列表顶部的元素

java - 无法确定 jsp 中的列表大小

java - 客户端服务器android上的数据库

java正则表达式用于分割字符串

vba - 在 for 循环 VBA 中刷新 Bloomberg 请求

正则表达式确定字符串是范围名称还是单元格地址

java - Junit 和 Selenium 的 Maven 编译错误