英文:
How to implement setup and teardown approach for my Behave Framework
问题
我正在尝试使用Behave项目的装置/拆卸方法来实施测试固件。我在让它正常工作方面遇到了问题,以下是我放在environment.py文件中的代码:
import logging as logger
import pdb
def before_feature(context, feature):
all_tags = feature.tags
if '@CyberArk' in all_tags:
context.CyberArk = True
logger.info("Before feature")
logger.info("")
def after_feature(context, feature):
logger.info("After feature")
logger.info("")
def before_scenario(context, scenario):
logger.info("Before Scenario")
logger.info("")
if scenario.name == 'User has logged into Jenkins process with valid Credentials':
# 设置测试场景
# 调用()
pass
有人能告诉我问题出在哪里吗?当我运行代码时,什么都没有打印,但我期望输出进行验证。
英文:
I am trying to implement setup/teardown approach using fixtures for my Behave project.
I am having trouble getting it to work, here is my code which I have kept inside environment.py
import logging as logger
import pdb
def before_feature(context,feature):
all_tags = feature.tags
if '@CyberArk' in all_tags:
context.CyberArk = True
logger.info("Before feature")
logger.info("")
def after_feature(context, feature):
logger.info("After feature")
logger.info("")
def before_scenario(context, scenario):
logger.info("Before Scenario")
logger.info("")
if scenario.name == 'User has logged into Jenkins process with valid Credentials':
# setup for the scenario
# call()
pass
Can anyone tell me what the problem is? Nothing is printing when I run the code, and I expected output for validation
答案1
得分: 0
以下是翻译好的内容:
有一个现有的框架,其中一个模式完全结合了 selenium
和 behave
。 https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/ReadMe.md(您可以从中学到很多东西,甚至可以使用它。)
如果您正在尝试定义设置/拆卸方法,您可以使用这些内置的 behave
方法,这些方法应该存在于一个名为 environment.py
的文件中:
before_all(context)
before_feature(context, feature)
before_scenario(context, scenario)
before_step(context, step)
after_step(context, step)
after_scenario(context, scenario)
after_feature(context, feature)
after_all(context)
使用 SeleniumBase,这些方法在这里定义:https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/behave/behave_sb.py,然后在这里导入到 environment.py
文件中:https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/features/environment.py
与步骤文件一起使用,例如 https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/features/steps/calculator.py 和/或 https://github.com/seleniumbase/SeleniumBase/blob/master/sbase/steps.py,然后可以轻松创建这样的测试:
Feature: SeleniumBase scenarios for the RealWorld App
Scenario: Verify RealWorld App (log in / sign out)
Given Open "seleniumbase.io/realworld/login"
And Clear Session Storage
When Type "demo_user" into "#username"
And Type "secret_pass" into "#password"
And Do MFA "GAXG2MTEOR3DMMDG" into "#totpcode"
Then Assert exact text "Welcome!" in "h1"
And Highlight "img#image1"
And Click 'a:contains("This Page")'
And Save screenshot to logs
When Click link "Sign out"
Then Assert element 'a:contains("Sign in")'
And Assert text "You have been signed out!"
运行时使用 behave
,您将看到以下输出:
> behave realworld.feature
Feature: SeleniumBase scenarios for the RealWorld App # realworld.feature:1
Scenario: Verify RealWorld App (log in / sign out) # realworld.feature:3
Given Open the RealWorld Login Page # steps/real_world.py:4
When Login to the RealWorld App # steps/real_world.py:11
Then Assert exact text "Welcome!" in "h1" # steps/real_world.py:89
When Highlight element "img#image1" # steps/real_world.py:19
And Click element 'a:contains("This Page")' # steps/real_world.py:29
Then Save a screenshot to the logs # steps/real_world.py:49
When Click link "Sign out" # steps/real_world.py:39
Then Assert element 'a:contains("Sign in")' # steps/real_world.py:55
And Assert text "You have been signed out!" # steps/real_world.py:79
✅ Scenario Passed!
--- LogPath: /Users/michael/github/SeleniumBase/integrations/behave/features/latest_logs/
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
9 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m2.813s
英文:
There's an existing framework with a mode that fully combines selenium
with behave
. https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/ReadMe.md (You could learn a lot from that, or even use it.)
If you are trying to define setup/teardown methods, you can use these built-in behave
methods, which should exist in an environment.py
file:
before_all(context)
before_feature(context, feature)
before_scenario(context, scenario)
before_step(context, step)
after_step(context, step)
after_scenario(context, scenario)
after_feature(context, feature)
after_all(context)
With SeleniumBase, those methods are defined here: https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/behave/behave_sb.py and then imported into the environment.py
file here: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/features/environment.py
In combination with steps files, such as https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/features/steps/calculator.py and/or https://github.com/seleniumbase/SeleniumBase/blob/master/sbase/steps.py, you can then easily create tests like this:
Feature: SeleniumBase scenarios for the RealWorld App
Scenario: Verify RealWorld App (log in / sign out)
Given Open "seleniumbase.io/realworld/login"
And Clear Session Storage
When Type "demo_user" into "#username"
And Type "secret_pass" into "#password"
And Do MFA "GAXG2MTEOR3DMMDG" into "#totpcode"
Then Assert exact text "Welcome!" in "h1"
And Highlight "img#image1"
And Click 'a:contains("This Page")'
And Save screenshot to logs
When Click link "Sign out"
Then Assert element 'a:contains("Sign in")'
And Assert text "You have been signed out!"
When run with behave
, you'll see the following output:
> behave realworld.feature
Feature: SeleniumBase scenarios for the RealWorld App # realworld.feature:1
Scenario: Verify RealWorld App (log in / sign out) # realworld.feature:3
Given Open the RealWorld Login Page # steps/real_world.py:4
When Login to the RealWorld App # steps/real_world.py:11
Then Assert exact text "Welcome!" in "h1" # steps/real_world.py:89
When Highlight element "img#image1" # steps/real_world.py:19
And Click element 'a:contains("This Page")' # steps/real_world.py:29
Then Save a screenshot to the logs # steps/real_world.py:49
When Click link "Sign out" # steps/real_world.py:39
Then Assert element 'a:contains("Sign in")' # steps/real_world.py:55
And Assert text "You have been signed out!" # steps/real_world.py:79
✅ Scenario Passed!
--- LogPath: /Users/michael/github/SeleniumBase/integrations/behave/features/latest_logs/
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
9 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m2.813s
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论