【问题标题】:Keep Session open - Capybara/Cucumber保持会话打开 - Capybara/Cucumber
【发布时间】:2019-12-30 22:57:43
【问题描述】:

我有一个功能需要运行超过 100 个场景,第一步是登录我的应用程序。这通常使用Before 钩子和After 钩子来处理,这将使您注销。这使我的测试彼此独立,对我来说通常是个好主意。但是在这种情况下,我只想登录我的应用程序,运行我的所有场景并注销。

我似乎遇到了会话问题,因为在一个场景完成后我被重定向到 about:blank 并且我的会话被终止了。

我已经试过了

class Capybara::Selenium::Driver < Capybara::Driver::Base
def reset!
# Use instance variable directly so we avoid starting the browser just to reset the session
if @browser
  begin
    #@browser.manage.delete_all_cookies <= cookie deletion is commented out!
  rescue Selenium::WebDriver::Error::UnhandledError => e
    # delete_all_cookies fails when we've previously gone
    # to about:blank, so we rescue this error and do nothing
    # instead.
  end
  @browser.navigate.to('about:blank')
end
end
end

然后我在控制台中收到以下错误

expected not to find xpath "/html/body/*", found ...

所以我的问题是如何完成一个场景,然后只需单击我网站中的另一个链接,然后执行下一个场景

谢谢

【问题讨论】:

    标签: ruby session cucumber capybara


    【解决方案1】:

    这是一个很好的例子,说明何时使用 Cucumber 的 Background 功能是个好主意。

    如果你让用户在后台标签内登录,他们将在所有后续场景中登录。

    【讨论】:

    • 谢谢,除非我误解了The background is run before each of your scenarios but after any of your Before Hooks.,所以它仍然会运行多次?
    • 啊,是的,我看错了!您可能有一个步骤运行另一批步骤github.com/cucumber/cucumber/wiki/… 因此,登录后,有一些步骤可以触发不同场景的每个步骤。虽然必须有更好的方法
    【解决方案2】:

    读完Poltergeist Docs后发现我可以实现一个简单的解决方案

    这是我想出的:

    Given(/^I setup the form with the correct template \- benchmarking$/) do
      if !defined? $i == true
        login_to_app
      elsif page.current_url == "about:blank"
        options = { domain: 'mydomain',
                    httponly: true, 
                    name: 'name',
                    path: '/',
                    secure: true
                  }
       page.driver.set_cookie("name", $session_cookie, options)
       page.visit('url')
      end
       click_link('mylink')
       wait_for_ajax
    end
    
    Then('This is my final step')
      $i = 1
      $session_cookie = page.driver.cookies["PHPSESSID"].value
    end
    

    所以在第一次迭代中,$i 没有定义,所以我可以登录,在第二种情况下它被定义,这意味着我可以根据最后一个会话设置 cookie 信息并访问我在我的应用程序中需要的页面使用与我登录时相同的凭据。

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 2012-04-30
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多