grails – 以示例的Geb登录规范
我试图熟悉
Geb.我正在尝试从Grails内部运行它,但这根本不重要,因为我的问题在这里是特定于Ge??b.
我有以下测试目录结构: myapp/ <lots of stuff here> test/ functional/ GebConfig.groovy LogInLogOutSpec.groovy pages/ LogInPage.groovy DashboardPage.groovy LoginPage.groovy(显然)是登录页面的位置,并且在成功登录后应将DashboardPage重定向到的位置.实际上,我有安全过滤器,可以检查您尝试访问的URL是否需要身份验证.如果是这样,他们会将您重定向到登录页面,并在成功登录后,再次将您重定向到您尝试访问的URL. 为了更好地衡量,这是包含我的登录页面的HTML(同样,这是Grails,因此GSP文件在运行时动态转换为HTML): <!DOCTYPE html> <html> <head> <title>Login</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <form action=“/myapp/auth/signIn" method="post"> <input type="hidden" name="targetUri" value="/dashboard" /> <table> <tbody> <tr> <td>Username:</td> <td><input type="text" name="username" value="" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" value="" /></td> </tr> <tr> <td /> <td><input type="submit" value="Sign in" /></td> </tr> </tbody> </table> </form> </body> </html> 所以我需要一个Geb测试: >尝试转到仪表板页面(未经授权) 此外: >我想选择:(a)基于浏览器(Firefox或Chrome)或(b)无头浏览器(我认为是HtmlUnitDriver,但如果我错了,请纠正我) 到目前为止我最好的尝试: GebConfig.groovy: driver = { // Again,or Firefox ChromeProfile profile = new ChromeProfile() Driver driverInstance = new ChromeDriver(profile) driverInstace.manage().window().maximize() driverInstance } baseNavigatorWaiting = true atCheckWaiting = true // Need to inject from some external process somehow,but make these // available to test specs somehow. username = System.env("USERNAME") password = System.env("PASSWORD") LogInLogOutSpec.groovy: import geb.spock.GerbReportingSpec import spock.lang.* import pages.* @Stepwise class LogInLogOutSpec extends GebReportingSpec { String username String password /* * Attempt to go to user dashboard,and confirm you are instead * redirected to the login page. Login with good credentials,* and verify you are now logged in and at the dashboard. */ def "successful login redirects to dashboard page"() { when: to DashboardPage then: "Login".equals($(".page-header").text()) when: $("#login-form input[name=username]").value(username) $("#login-form input[name=password]").value(password) then: "Dashboard".equals($(".page-header").text()) } } 我想我很亲密,但这显然是错的.我出错的任何想法?谢谢! 解决方法
您应该模块化您的测试代码.你应该用
> modules用于可在不同页面上出现的UI小部件 在您的代码示例中,您有一个仪表板页面,但没有登录页面对象.如果您的所有页面都可以登录或至少提供检测登录状态的可能性,您还应该考虑登录模块.有关模块定义,请参见SO answer.这里缺少的是确定登录状态的函数.对于另一个实现,请查看Geb examples的AuthModule和login test spec. 登录凭据:我认为您在环境中获得了凭据.这在开发人员计算机上以及构建服务器上都可以正常工作. 对于使用不同浏览器进行测试:geb-example-gradle是一个带有GebConfig和build.gradle的gradle示例项目.这允许您将浏览器作为参数切换为gradle: ./gradlew chromeTest ./gradlew firefoxTest ./gradlew phantomJsTest 这也回答了你的最后一个问题:我喜欢使用的无头浏览器是phantomJs. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |