【问题标题】:Is it possible to use the page function inside a class?是否可以在类中使用页面功能?
【发布时间】:2014-06-30 14:37:27
【问题描述】:

我正在用 calabash 编写一些测试,并尝试在辅助类中使用页面功能。

我有我的步骤文件

Given /^I am on my page$/ do
   mypage = page(MyPage)
   MyPageHelper.DoMultiActionStep()
end

还有我的页面文件

class MyPage < Calabash::ABase
    def my_element_exists
       element_exists(MY_ELEMENT_QUERY)
    end
end

还有我的帮助文件

class MyPageHelper
   def self.DoMultiActionStep
      mypage = page(MyPage) 
      mypage.do_action_one
      mypage.my_element_exists
   end
end

当我运行它时,虽然我得到了错误

MyPageHelper:Class 的未定义方法“页面”(NoMethodError)

页面函数在步骤文件中工作正常,但似乎从 MyPageHelper 类调用时出现问题。是否有可能做到这一点?是否需要添加 using 语句?

谢谢!

【问题讨论】:

  • calabash-ios 在标签中,但您使用的是来自calabash-android 的Calabash::ABase。是这个问题吗?
  • 啊,很可能。我有安卓和ios测试。我应该在 ios 测试中使用什么而不是 ABase?
  • 我把它改成IBase并重新运行它,但仍然遇到同样的问题。

标签: ruby calabash calabash-ios


【解决方案1】:

恐怕我不知道如何直接回答你的问题。

冒着被激怒的风险,我推荐另一种方法。

选项 1: 如果您不需要辅助类,请不要使用它。

我知道您的实际代码可能更复杂,但是您需要这里的帮助程序吗?为什么不在 MyPage 类中实现 do_multi_action_step 作为方法?

def do_multi_action_step
     my_element_exists
     my_other_method
end

选项 2: 传递 MyPage 的实例

在您的步骤中,您创建了 MyPage 的一个实例。您应该使用该实例,而不是在 MyPageHelper.do_multi_action_step 中创建一个新实例。

def self.do_multi_action_step(my_page)
  my_page.my_element_exists
  my_page.my_other_method
end

示例:

# my_page_steps.rb
Given /^I am on my page$/ do
  # use the await method to wait for your page
  my_page = page(MyPage).await

  # pass an instance instead of creating a new one
  MyPageHelper.do_multi_action_step(my_page)

  # or just use a method on the MyPage instance
  my_page.do_multi_action_step
end

# my_page_helper.rb
class MyPageHelper
  # pass the page as an object
  def self.do_multi_action_step(my_page)
    my_page.my_element_exists
    my_page.my_other_method
  end
end

# my_page.rb
require 'calabash-cucumber/ibase'

class MyPage < Calabash::IBase

  # some view that is unique to this page
  def trait
    "view marked:'some mark'"
  end

  def my_element_exists
    element_exists("view marked:'foo'")
  end

  def my_other_method
    puts 'do something else'
  end

  # why not do this instead?
  def do_multi_action_step
    my_element_exists
    my_other_method
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多