在 Cucumber 表中使用元素定位器

Using element locators in Cucumber tables

我有一个登录测试,其中我 运行 使用 cucumber 中的表的正面和负面场景。

场景大纲:登录

Given User navigates to Field
And User enters a "<Username>" username
And User enters a "<Password>" password
When User clicks on the login button
Then User should see the "<message>"

Examples:
  | Username | Password  | message |
  | name  | pwd | Logout |
  | name1 | pwd1 | Your login name or password is incorrect.|

我现在使用 "message" 断言它。

我的问题是:是否可以使用 xpath 或其他元素定位器进行断言?这是推荐的方法还是我应该为每个案例创建不同的场景?

是否可以使用 xpath 或其他元素定位器进行断言?

是的,这是可能的。

你可以这样断言:

String successMsg = driver.findElement(By.xpath("some xpath")).getText();

并且您已经收到了消息(意味着您已经知道应该收到什么)。

String expected = "your expected msg";  

现在您要做的就是比较这两个字符串。

testng 中类似于:

Assert.assertEquals(successMsg, expected) 可以胜任。

参考:

Cucumber assertion

不要在一个场景中组合有效或无效的案例。几乎在所有情况下,都会有不同的页面流。您将需要使用 if 条件来处理它以考虑到这一点。将它们分开并避免复杂化。

将它们分开的另一个好处是,如果您只想 运行 有效案例。只需在该场景上添加标签并在 运行ner 中提及即可。