首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以在SeleniumBase中使用pytest-bdd?

是否可以在SeleniumBase中使用pytest-bdd?
EN

Stack Overflow用户
提问于 2021-02-09 09:13:17
回答 1查看 363关注 0票数 0

这就是我如何编写SeleniumBase/pytest-bdd测试的方式:

ddg.feature

代码语言:javascript
复制
Feature: Browse DuckDuckGo

    Going to DuckDuckGo webpage.

    Scenario: I can see the title
        When I go to DuckDuckGo webpage
        Then Duck is present in the title

test_ddg.py

代码语言:javascript
复制
from seleniumbase import BaseCase
from pytest_bdd import scenarios, when, then

scenarios("./ddg.feature")

class MyTestClass(BaseCase):

@when("I go to DuckDuckGo webpage")
def go_to_ddg(self):
    self.open('https://duckduckgo.com/')

@then("Duck is present in the title")
def is_title_present(self):
    assert 'Duck' in self.get_title()

然而,这是行不通的。函数无法查看何时和随后的描述符。

如果可能的话,你知道怎么做吗?

EN

回答 1

Stack Overflow用户

发布于 2021-03-23 09:01:20

您需要使用SeleniumBase作为pytest工具,而不是直接继承BaseCase。参见“sb pytest夹具”一节

因此-在您的示例中-您不需要导入BaseCase,您应该使用"sb“而不是"self”。

另一个例子:功能/网站特性

代码语言:javascript
复制
@homepage
Feature: Homepage

  Scenario: Homepage this and that
    Given the browser is at the homepage
    When the user clicks this
    Then that is shown

步骤防御/主页_test.py

代码语言:javascript
复制
from pytest_bdd import scenarios, given, when then
from .pom import *

# Constants
PAGE = 'https://seleniumbase.io'

# Scenarios 
scenarios('../features/homepage.feature')

# Given Steps
@given('the browser is at the homepage')
def the_browser_is_at_the_homepage(sb):
    """the browser is at the homepage."""
    sb.get(PAGE)

# When Steps
@when('the user clicks this')
def the_user_clicks_this(sb):
    """the user clicks this."""
    sb.click(Menu.this)

# Then Steps
@then('that is shown')
def that_is_shown(sb):
    """that is shown."""
    sb.assert_text('The sb pytest fixture',SyntaxPage.that)

梯级防御/防撞

代码语言:javascript
复制
class Menu():  
    this = "//nav//a[contains(text(),'Syntax Formats')]"

class SyntaxPage():
    that = "(//h3)[4]"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66115961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档