在SpecFlow和Selenium的功能设置中指定登录

时间:2013-01-10 12:21:04

标签: selenium bdd specflow

我已经设置了SpecFlow来执行一些Selenium测试来测试网站。一切正常!

我只是在考虑是否可以在SpecFlow功能的结构上进行一些优化。

特定功能中的所有方案都应使用相同的登录名。这是我现在使用[BeforeScenario()]钩子在StepDefinition中硬编码的东西,因为我真的不会用登录信息污染场景。它与测试无关。

但与此同时,我想删除硬编码部分,并将其移入我的功能。

我的问题是两部分。

  1. 我可以在功能说明中指定登录凭据。有点像给定:

    Feature: As a user I want to be able to see my ongoing orders, and interact with them.
    Given I am logged in with username abc and password xyz
    
    Scenario: See list of ongoing order
    Given I place an order
    When I navigate to MyOrders page
    Then I can see at least one order in the list
    
  2. 这是一个好习惯吗?

    在我所说的功能级别上这样做是否有意义。这些方案不依赖于特定的顺序,如果我不需要为每个方案登录,它们的执行速度会更快。

  3. 感谢您的意见。

3 个答案:

答案 0 :(得分:2)

对于功能中所有方案通用的步骤,您可以使用背景:

    Feature: As a user I want to be able to see my ongoing orders, and interact with them.

    Background:
    Given I am logged in with username abc and password xyz

    Scenario: See list of ongoing order
    Given I place an order
    When I navigate to MyOrders page
    Then I can see at least one order in the list

过多滥用后台步骤可能是一种不好的做法,因为它会引入您的场景之间的耦合。

另一种解决方案是将登录部分直接放入“我下订单”步骤。 这将删除有关登录内容的所有噪音,因为它暗示您需要登录才能下订单。

我还建议将其称为“我已下订单”而不是“我下订单”。 给定步骤通常是先于条件描述事先使用functionnality发生的事情(当步骤时)

答案 1 :(得分:2)

当我第一次阅读你的帖子时,我想到了Dan North: Who's domain is it anyway?,这让我觉得我们应该尝试编写测试,以便他们坚持一个知识领域。当您在列表中谈论特定用户以及导航和订单时,它非常类似于他给出的示例,即您的规范跨越域。这种方式导致我几乎不会使您的规格变得不那么具体,即它不是关于导航和检查列表,是否有订单!

场景:查看正在进行的订单列表     鉴于我使用用户名abc和密码xyz登录     我下了订单     然后应该至少有一个订单

我想给你另一个示例链接,我找不到的帖子,它讲述了团队通过实际定义一些示例用户以及给他们的测试带来什么价值所带来的好处。因此,在您的业务领域中,您可能会有一个名为Jack的用户,他将在系统中下大订单,而另一个预期客户Jill则没有订单。通过这种方式测试

Given I am Jack
When I search for my orders
Then I will find 1

Given I am Jill
When I search for my orders
Then I will find 0

可以保证您只测试您的搜索功能,而不是测试设置。

我还建议您查看Liz Keogh: Acceptance Criteria vs Scenarios谁声称更广泛的广义定义比非常具体的示例更有价值。 (毕竟是规范说明 :-))

所以为了回答你的问题,我认为拥有指定的用户是一件好事,但是如何做到这一点正在走向一条非常复杂的路线。

答案 2 :(得分:0)

给出的场景示例与功能完全不符。

   Feature: As a user I want to be able to see my ongoing orders, and interact with them.

Background:
Given I am logged in as John Doe

Scenario: This is taking to long
And I have placed an order more than 29 days ago
When I navigate to MyOrders page
Then I can see my order in the list
and I am able to cancel it

Scenario: Track package
And I have been notified my order has been send
When I navigate to MyOrders page
Then I can see my order in the list
and I am able to navigate to the postman's site and see its status

Scenario: Review item
And I have received my package
When I navigate to MyOrders page
Then I can see my order in the list
and I am able to review the item

Scenario: Contact Seller
And I have placed an order two weeks ago
When I navigate to MyOrders page
Then I can see my order in the list
and I am able to ask the seller what is going on

如您所见,“何时”和“然后”开始复制自身,您可能需要考虑将它们藏起来。