我正在使用Watir Splash框架来测试Web应用程序,我已经设置了两个页面类。第一个是“登录”页面,详情如下:
module App
module Page
class Login < WatirSplash::Page::Base
url "http://[removed].com"
def login_btn
modify button(:id => 'btnLogin'), :click => lambda {redirect_to VehicleSelection}
end
另一个页面类是“车辆选择”页面。我已经使用了文档here中所示的修改方法,以确保在成功登录后车辆选择页面对象可用于RSpec。
但是如果登录失败会怎么样?我有一些测试用例故意将不正确的信息提供给登录表单,以确保身份验证正常工作。 RSpec需要“Login”类中定义的方法来访问正确的元素以完成测试用例。在这种情况下,我指定方法的方式将返回“VehicleSeleciton”对象,无论如何。 (左右看起来)
感谢任何帮助。此外,我对其他测试框架的建议持开放态度,特别是如果有更多示例代码供我参考。
答案 0 :(得分:2)
以下是我尝试过的几种方法。我没有使用WatirSplash框架,但应用了相同的概念(尽管尝试过的WatirSplash示例代码可能不是100%准确)。
解决方案1:返回页面对象
我个人的偏好是没有页面对象返回页面对象。相反,我发现在测试中使用每个页面对象的显式初始化更容易阅读/工作。 Alister Scott在他的blog中讨论过这个问题。
您的测试将如下所示:
#For login successful tests
page = App::Page::Login.new
page.login_btn.click
page = App::Page::VehicleSelection.new #The VehicleSelection page is explicitly initialized
page.validate_page #or whatever you want to do with the page
#For login failed tests
page = App::Page::Login.new
page.login_btn.click
page.validate_page #or whatever you want to do with the page
解决方案2:创建多种登录方法
另一个解决方案是创建两个登录方法 - 一个用于成功登录,另一个用于不成功登录。
页面对象可以是:
module App
module Page
class Login < WatirSplash::Page::Base
url "http://[removed].com"
def login(user, password)
#Do whatever code to input name and password and then click the button
#Then redirect to the VehicleSelection page since that is where you will want to go most often
redirect_to VehicleSelection
end
def login_failed(user, password)
login(user, password)
#Return the Login page (instead of the VehicleSelection page).
redirect_to Login
end
end
end
end
测试是:
#For login successful tests
login_page = App::Page::Login.new
vehicle_page = login_page.login(user, password)
vehicle_page.validate_page #or whatever you want to do with the Vehicle Selection page
#For login failed tests
login_page = App::Page::Login.new
login_page.login_failed(user, password)
login_page.validate_page #or whatever you want to do with the Login page
解决方案3:让按钮知道它的去向
另一个解决方案是让登录按钮知道要重定向到哪个页面。
页面对象可以是:
module App
module Page
class Login < WatirSplash::Page::Base
url "http://[removed].com"
def login_btn(login_successful=true)
if login_successful
modify button(:id => 'btnLogin'), :click => lambda {redirect_to VehicleSelection}
else
modify button(:id => 'btnLogin'), :click => lambda {redirect_to Login}
end
end
end
end
end
测试是:
#For login successful tests
login_page= App::Page::Login.new
vehicle_page = login_page.login_btn.click
vehicle_page.validate_page #or whatever you want to do with the Vehicle Selection page
#For login failed tests
login_page= App::Page::Login.new
login_page.login_btn(false).click
login_page.validate_page #or whatever you want to do with the Login page
答案 1 :(得分:1)
感谢您试用我的宝石WatirSplash。我会在解决方案#2中写出一些内容 - 例如创建两个单独的方法来成功登录和登录失败。两种方法都不需要使用#modify
,就像贾斯汀那样。
另外,我建议你使用我的另一个gem test-page,它或多或少与WatirSplash中的Page Objects相同,但是它被提取到单独的gem中 - WatirSplash将被弃用长期由于其所有部分被提取到单独的宝石中,可以更好地控制每个项目中需要哪些功能。