【问题标题】:How to identify a particular Page Object fragment member element through Selenium如何通过 Selenium 识别特定的 Page Object 片段成员元素
【发布时间】:2018-12-20 12:12:18
【问题描述】:

在我们的项目中,我们使用黄瓜运行了多个硒测试,这些测试正在测试特定组件。这些组件之一是两个标识符,我们称之为国家标识符和站点标识符。 这些具有以下 HTML:

<div class="identifiers">   
    <div class="country-identifier">
        <span class="ident-label">
            Germany
        </span>
    </div>
    <div class="site-identifier">
        <span class="ident-label">
            Gaming
        </span>
    </div>
</div>

现在我们的测试有两个模型,每个标识符一个:

@PageObject(css = "div.country-identifier")
@Named("CountryIdentifier")
@ScenarioScoped
public class CountryIdentifier {

    @Inject
    @CurrentScope
    private WebElement scope;

    @FindBy(className = "ident-label")
    @CacheLookup
    private WebElement label;

}

还有:

@PageObject(css = "div.site-identifier")
@Named("SiteIdentifier")
@ScenarioScoped
public class SiteIdentifier {

    @Inject
    @CurrentScope
    private WebElement scope;

    @FindBy(className = "ident-label")
    @CacheLookup
    private WebElement label;

}

现在的问题是,当我想访问站点标识符的标签时,WebElement 标签的值是 Germany 而不是 Gaming。这是因为获取值的 css 选择器显然只应用了类名 ident-label 而没有考虑容器类。我希望生成的用于查找标签的选择器会将为页面对象定义的 css 与 @FindBy 注释中的选择器结合起来。 有没有办法告诉 Web 驱动程序仅在 @PageObject 注释的 css 选择器中指定的容器范围内按类名查找元素?还是我需要 FindBy 注释中的完整 css 选择器,例如:

@FindBy(css = "div.site-identifier .ident-label")

还有

@FindBy(css = "div.country-identifier .ident-label")

【问题讨论】:

  • label of the site identifier 不是 having the value "Germany" 但绝对是"Gaming"

标签: selenium selenium-webdriver css-selectors cucumber pageobjects


【解决方案1】:

看来你快到了。您可以使用以下定位器来识别相应的元素:

  • Germany:

    @FindBy(css = "div.country-identifier span.ident-label")
    
  • Gaming:

    @FindBy(css = "div.site-identifier span.ident-label")
    

更新

根据您询问的评论更新可以只指定“.ident-label”并使驱动程序仅在页面对象注释的 css 指定的容器内搜索,答案是。这是因为class ident-label 出现在两个&lt;div&gt; 标签下,如下所示:

  • Germany:

    <span class="ident-label">
        Germany
    </span>
    
  • Gaming:

    <span class="ident-label">
         Gaming
    </span>
    

所以,WebDriver 实例无法区分它们,并且总是会根据流行的HTML DOM 选择第一个匹配项。

解决方案

有两种解决方法:

  • 最简单的方法是借助父 &lt;div&gt; 标签,它的每个 child 标签都有不同的 class 属性。
  • 否则使用index

第一种方法是最好的方法,并且大多被遵循。

【讨论】:

  • 好的,这是我可以做的。但我希望行为是当我在 SiteIdentifier 模型上调用定位器时,它只在容器 div.site-identifier 中查找特定的类名,我在注释中指定了它。所以假设容器内有很多元素并且我更改了容器的类名,我需要更新每个元素的所有定位器。
  • 他已经有了css(注意他问题的底部),他想知道元素的css是否会加入到页面对象的css中。答案是否定的,他需要使用完整的 css。
  • @Christoph 如果我理解正确你的问题是div.site-identifier .ident-label 就足够了,而不是div.site-identifier span.ident-label?是吗?
  • @KyleFairns 我更新的答案现在是否完全符合这个问题?
  • @DebanjanB 确实如此
猜你喜欢
  • 2020-06-20
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 2019-09-01
  • 2017-09-13
相关资源
最近更新 更多