如果在脚本中使用Selenium和Python,如何使用BDD在testrail中编写测试用例?

2024-05-23 22:36:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习在PyCharm中使用Selenium和Python实现的Android移动应用程序自动化的BDD(Behave)(因此不是纯Python;我从未理解其中的区别,尽管我试图找到相关信息)。 我是初学者,所以我需要使用Selenium和Python脚本在TestRail中创建测试用例(这是我的目标)。 我听说过黄瓜配小黄瓜,但我很困惑。 我需要学习纯Python吗?我可以用PyCharm编写测试用例吗? 有人能帮我提些建议吗? 从哪里开始?提前谢谢

PS:我有一些与文档的链接,但我仍然感到困惑:https://behave.readthedocs.io/


Tags: 脚本信息应用程序目标selenium测试用例bdd黄瓜
1条回答
网友
1楼 · 发布于 2024-05-23 22:36:53

我将尝试为webdriver进行类似于此的页面对象模式测试:
对于页面:

class LoginPage(BasePage):

def __init__(self, context):
    BasePage.__init__(self,
                      context.browser,
                      )
    self.context = context
    self.base_url = URL.TWITTER
    self.visit(url=self.base_url)

locator_dictionary = LoginLocators.__dict__

def login(self, username='', password=''):
    self.username.send_keys(username)
    self.password.send_keys(password)
    self.submit_btn.click()
    return TwitterTimeline(self.context)

对于基本页面:

class BasePage(object):
"""
Really the only method used here is '__getattr__' now
"""

def __init__(self, browser, base_url='', **kwargs):
    self.browser = browser
    self.base_url = base_url
    self.timeout = 10

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    old_page = self.find_element_by_tage_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))

def find_element(self, *loc):
    return self.browser.find_element(*loc)

def find_elements(self, *loc):
    return self.browser.find_elements(*loc)

def visit(self, url='', route=''):
    if not url:
        url = self.base_url
    self.browser.get(url + route)

def hover(self, element):
    ActionChains(self.browser).move_to_element(element).perform()
    # I don't like this but hover is sensitive and needs some sleep time
    time.sleep(5)

def __getattr__(self, what):
    try:
        if what in self.locator_dictionary.keys():
            try:
                element = WebDriverWait(self.browser, self.timeout).until(
                    EC.presence_of_element_located(self.locator_dictionary[what])
                )
            except(TimeoutException, StaleElementReferenceException):
                traceback.print_exc()

            try:
                element = WebDriverWait(self.browser, self.timeout).until(
                    EC.visibility_of_element_located(self.locator_dictionary[what])
                )
            except(TimeoutException, StaleElementReferenceException):
                traceback.print_exc()
            # I could have returned element, however because of lazy loading, I am seeking the element before return
            return self.find_element(*self.locator_dictionary[what])
    except AttributeError:
        super(BasePage, self).__getattribute__("method_missing")(what)

def method_missing(self, what):
    print "No %s here!" % what

全文如下:
http://blerdeyeview.com/testing-with-behave-framework/
还有一个:
https://www.testrisk.com/2015/03/page-object-patter-for-selenium-test.html

我也是初学者,这对我很有帮助。我正在测试网站,不是Android,但它应该适合您的想法。
非常适合编写小黄瓜的步骤
祝你好运

另外,我在每个子页面类上都使用定位器字典,因为它工作得很好。不幸的是,这种方法没有自动完成功能,但我相信Android应用程序不需要字典。它应该像写在这些链接中一样工作。
编辑以获得更多代码输入

相关问题 更多 >