1

他们是通过单独传递对象定位器来找到对象定位器类型的任何方式。例如,我需要单击登录按钮,它的位置id=loginclassname=loginbuttonxpath=//input[@name='login']。我需要构建方法,我将只传递对象定位器(id 或名称)作为输入,并且它的类型(id 或名称)应该在方法中决定,比如它是否包含//然后类型应该是 xpath 等。

我需要将返回类型的 objectLocator() 传递给findElement()

WebElement element = driver.findElement(objectLocator());
4

5 回答 5

2

我认为它不是现成的,您必须实现自己的逻辑。

唯一的事情是,假设您想通过链接文本进行搜索。根据您的用例,您将在对象存储库中指定“这是我的链接文本”。
现在你怎么知道它是一个 id 或一个名字或一个链接文本?

对于 xpath,您可以检查它是否以 / 开头,然后是 xpath。如果它只有 id 或 name,那么您可以使用 ByIdorName,但我认为使用 css 和 linktext 会变得棘手。

我能想到的一件事是您可以建立某种约定,例如,如果它是 linktext 在您的 lcoator 定义之前使用 linktext=blah blah ,然后您拆分并使用它。

于 2013-03-06T10:34:57.007 回答
1

执行此操作的现代方法是使用PageFactoryPageObjects

以下是一个快速而肮脏的方法,它将使 selenium 定位器字符串适应 WebDriver 定位器。

    public enum LocatorType {
         CLASSNAME, CSS, ID, LINK, NAME, TAGNAME, XPATH ;
    }

    public WebElement objectLocator(LocatorType type, String ref) {
    switch(type) {
    case ID:
        return this.webDriver.findElement(By.id(ref));
    case CLASSNAME:
        return this.webDriver.findElement(By.className(ref));
    case XPATH:
        return this.webDriver.findElement(By.xpath(ref));
    case CSS:
        return this.webDriver.findElement(By.cssSelector(ref));
    case LINK:
        return this.webDriver.findElement(By.linkText(ref));
    case NAME:
        return this.webDriver.findElement(By.name(ref));
    case TAGNAME:
        return this.webDriver.findElement(By.tagName(ref));
    }
    return null;
    }

    public WebElement objectLocator(String identifier) {
    String typeString = identifier.substring(0, identifier.indexOf('='));
    String ref = identifier.substring(identifier.indexOf('=')+1, identifier.length());
    if (typeString.toLowerCase().contains("classname")) {
        return objectLocator(LocatorType.CLASSNAME, ref);
    } else if (typeString.toLowerCase().contains("css")) {
        return objectLocator(LocatorType.CSS, ref);
    } else if (typeString.toLowerCase().contains("id")) {
        return objectLocator(LocatorType.ID, ref);
    } else if (typeString.toLowerCase().contains("link")) {
        return objectLocator(LocatorType.LINK, ref);
    } else if (typeString.toLowerCase().contains("name")) {
        return objectLocator(LocatorType.NAME, ref);
    } else if (typeString.toLowerCase().contains("tagname")) {
        return objectLocator(LocatorType.TAGNAME, ref);
    } else if (typeString.toLowerCase().contains("xpath")) {
        return objectLocator(LocatorType.XPATH, ref);
    } else {
        return null;
    }
}
于 2013-03-06T14:20:15.563 回答
1

我发现将所有定位器存储为 By 对象并直接使用 By 或根据需要将 By 传递给方法非常有用。例如:

By passwordField= By.id("login");
By userNameField = By.name("username");
By submitButton = By.xpath("\\myxpath\div[2]");


public void clickLogin() {
   driver.findElement(submitButton).click();
}

我也使用其他类的静态 Bys:

public void clickLogin() {
   driver.findElement(LoginPage.SUBMIT_BUTTON).click();
}
于 2013-03-06T20:00:45.777 回答
0

看起来您正在寻找此解决方案,因为您在某种属性文件或 xml 中的代码之外的某处维护了一个对象存储库。

Using gui maps has lot of disadvantages like,

- maintain an external file with a list of locators
- parse locator files to read keys (you can abstract this but still an overhead)
- when writing PageObjects you need to switch back and forth from Page to gui map
- possibility of multiple duplicate locators in gui maps
- object repo grows over time and becomes impossible to maintain
- debugging is far more difficult

您正在寻找的是增加一层复杂性,我认为这不是必需的。自动化浏览器本身就是一个挑战,编写可维护的测试自动化代码至关重要。

在页面对象中使用PageFactory 。

- Natural place for your locators are Page Objects themselves. 
- Locators easily accessible in page objects for review or correction
- No need for explicit driver.findElement, with @FindBy you get that for free
- modern Java and awesome annotations make page objects look beautiful & readable

我以前使用过 gui 地图并且很挣扎。切换到页面工厂让我意识到使用对象存储库是个坏主意!

于 2013-03-10T23:56:59.577 回答
0

这应该用于定位元素。我已经给出了 3 级深度的例子。

public WebElement findElement(String locator){
    WebElement  w = null;
    try{
        return (driver.findElement(By.id(locator)));
    }catch(Exception e1){
        try{
            return ( driver.findElement(By.name(locator)));
        }catch(Exception e2){
            try{
                return (driver.findElement(By.xpath(locator)));
            }catch(Exception e3){
                System.out.println("Cound not find a locator");
                e3.printStackTrace();
            }
        }
    }
    return(w);
}

public void type(String locator, String value){
    try{
        WebElement w= findElement(locator);
        w.sendKeys(""+value);
        Thread.sleep(sleepTime);
    }catch(Exception e){
        e.printStackTrace();
    }
}

-维奈

于 2014-04-18T09:42:01.573 回答