Best Python code snippet using behave
command_steps.py
Source:command_steps.py  
...144    '''145    expected_text = text146    if "{__WORKDIR__}" in text or "{__CWD__}" in text or "{__HOME__}" in text:147        expected_text = textutil.template_substitute(text,148            __WORKDIR__ = posixpath_normpath(context.workdir),149            __CWD__     = posixpath_normpath(os.getcwd()),150            __HOME__    = posixpath_normpath(os.path.expanduser('~'))151        )152    actual_output = context.command_result.output153    if DEBUG:154        print(u"expected:\n{0}".format(expected_text))155        print(u"actual:\n{0}".format(actual_output))156    textutil.assert_normtext_should_contain(actual_output, expected_text)157@then(u'the command output should not contain "{text}"')158def step_command_output_should_not_contain_text(context, text):159    '''160    EXAMPLE:161        ...162        then the command output should not contain "TEXT"163    '''164    expected_text = text165    if "{__WORKDIR__}" in text or "{__CWD__}" in text or "{__HOME__}" in text:166        expected_text = textutil.template_substitute(text,167            __WORKDIR__ = posixpath_normpath(context.workdir),168            __CWD__     = posixpath_normpath(os.getcwd()),169            __HOME__    = posixpath_normpath(os.path.expanduser('~'))170        )171    actual_output  = context.command_result.output172    if DEBUG:173        print(u"expected:\n{0}".format(expected_text))174        print(u"actual:\n{0}".format(actual_output))175    textutil.assert_normtext_should_not_contain(actual_output, expected_text)176@then(u'the command output should contain exactly "{text}"')177def step_command_output_should_contain_exactly_text(context, text):178    """179    Verifies that the command output of the last command contains the180    expected text.181    .. code-block:: gherkin182        When I run "echo Hello"183        Then the command output should contain "Hello"184    """185    expected_text = text186    if "{__WORKDIR__}" in text or "{__CWD__}" in text or "{__HOME__}" in text:187        expected_text = textutil.template_substitute(text,188            __WORKDIR__ = posixpath_normpath(context.workdir),189            __CWD__     = posixpath_normpath(os.getcwd()),190            __HOME__    = posixpath_normpath(os.path.expanduser('~'))191        )192    actual_output  = context.command_result.output193    textutil.assert_text_should_contain_exactly(actual_output, expected_text)194@then(u'the command output should not contain exactly "{text}"')195def step_command_output_should_not_contain_exactly_text(context, text):196    expected_text = text197    if "{__WORKDIR__}" in text or "{__CWD__}" in text or "{__HOME__}" in text:198        expected_text = textutil.template_substitute(text,199            __WORKDIR__ = posixpath_normpath(context.workdir),200            __CWD__     = posixpath_normpath(os.getcwd()),201            __HOME__    = posixpath_normpath(os.path.expanduser('~'))202        )203    actual_output  = context.command_result.output204    textutil.assert_text_should_not_contain_exactly(actual_output, expected_text)205@then(u'the command output should contain')206def step_command_output_should_contain(context):207    '''208    EXAMPLE:209        ...210        when I run "behave ..."211        then it should pass212        and  the command output should contain:213            """214            TEXT215            """216    '''217    assert context.text is not None, "REQUIRE: multi-line text"218    step_command_output_should_contain_text(context, context.text)219@then(u'the command output should not contain')220def step_command_output_should_not_contain(context):221    '''222    EXAMPLE:223        ...224        when I run "behave ..."225        then it should pass226        and  the command output should not contain:227            """228            TEXT229            """230    '''231    assert context.text is not None, "REQUIRE: multi-line text"232    step_command_output_should_not_contain_text(context, context.text.strip())233@then(u'the command output should contain exactly')234def step_command_output_should_contain_exactly_with_multiline_text(context):235    assert context.text is not None, "REQUIRE: multi-line text"236    step_command_output_should_contain_exactly_text(context, context.text)237@then(u'the command output should not contain exactly')238def step_command_output_should_contain_not_exactly_with_multiline_text(context):239    assert context.text is not None, "REQUIRE: multi-line text"240    step_command_output_should_not_contain_exactly_text(context, context.text)241# -----------------------------------------------------------------------------242# STEPS FOR: Directories243# -----------------------------------------------------------------------------244@step(u'I remove the directory "{directory}"')245def step_remove_directory(context, directory):246    path_ = directory247    if not os.path.isabs(directory):248        path_ = os.path.join(context.workdir, os.path.normpath(directory))249    if os.path.isdir(path_):250        shutil.rmtree(path_, ignore_errors=True)251    assert_that(not os.path.isdir(path_))252@step(u'I rename the directory "{directory}" to "{destination}"')253def step_rename_directory(context, directory, destination):254    path_ = directory255    if not os.path.isabs(directory):256        path_ = os.path.join(context.workdir, os.path.normpath(directory))257    dst = destination258    if not os.path.isabs(dst):259        dst = os.path.join(context.workdir, os.path.normpath(dst))260    if os.path.isdir(path_):261        os.rename(path_, dst)262    assert_that(not os.path.isdir(path_))263    assert_that(os.path.isdir(dst))264@step(u'I move the directory "{directory}" to "{destination}"')265def step_move_directory(context, directory, destination):266    path_ = directory267    if not os.path.isabs(directory):268        path_ = os.path.join(context.workdir, os.path.normpath(directory))269    dst = destination270    if not os.path.isabs(dst):271        dst = os.path.join(context.workdir, os.path.normpath(dst))272    if os.path.isdir(path_):273        shutil.move(path_, dst)274        import ipdb;  ipdb.set_trace()275    assert_that(not os.path.isdir(path_))276    assert_that(os.path.isdir(dst))277@given(u'I ensure that the directory "{directory}" does not exist')278def step_given_the_directory_should_not_exist(context, directory):279    step_remove_directory(context, directory)280@given(u'a directory outside the workdir named "{path}"')281def directory_named_dirname(context, path):282    assert context.workdir, "REQUIRE: context.workdir"283    path_ = os.path.expanduser(os.path.normpath(path))284    if not os.path.exists(path_):285        os.makedirs(path_)286    assert os.path.isdir(path_)287@given(u'a directory named "{path}"')288def step_directory_named_dirname(context, path):289    assert context.workdir, "REQUIRE: context.workdir"290    path_ = os.path.join(context.workdir, os.path.normpath(path))291    if not os.path.exists(path_):292        os.makedirs(path_)293    assert os.path.isdir(path_)294@then(u'the directory "{directory}" should exist')295def step_the_directory_should_exist(context, directory):296    path_ = directory297    if not os.path.isabs(directory):298        path_ = os.path.join(context.workdir, os.path.normpath(directory))299    assert_that(os.path.isdir(path_))300@then(u'the directory "{directory}" should not exist')301def step_the_directory_should_not_exist(context, directory):302    path_ = directory303    if not os.path.isabs(directory):304        path_ = os.path.join(context.workdir, os.path.normpath(directory))305    assert_that(not os.path.isdir(path_))306@step(u'the directory "{directory}" exists')307def step_directory_exists(context, directory):308    """309    Verifies that a directory exists.310    .. code-block:: gherkin311        Given the directory "abc.txt" exists312         When the directory "abc.txt" exists313    """314    step_the_directory_should_exist(context, directory)315@step(u'the directory "{directory}" does not exist')316def step_directory_named_does_not_exist(context, directory):317    """318    Verifies that a directory does not exist.319    .. code-block:: gherkin320        Given the directory "abc/" does not exist321         When the directory "abc/" does not exist322    """323    step_the_directory_should_not_exist(context, directory)324# -----------------------------------------------------------------------------325# FILE STEPS:326# -----------------------------------------------------------------------------327@step(u'a file named "{filename}" exists')328def step_file_named_filename_exists(context, filename):329    """330    Verifies that a file with this filename exists.331    .. code-block:: gherkin332        Given a file named "abc.txt" exists333         When a file named "abc.txt" exists334    """335    step_file_named_filename_should_exist(context, filename)336@step(u'a file named "{filename}" does not exist')337def step_file_named_filename_does_not_exist(context, filename):338    """339    Verifies that a file with this filename does not exist.340    .. code-block:: gherkin341        Given a file named "abc.txt" does not exist342         When a file named "abc.txt" does not exist343    """344    step_file_named_filename_should_not_exist(context, filename)345@then(u'a file named "{filename}" should exist')346def step_file_named_filename_should_exist(context, filename):347    command_util.ensure_workdir_exists(context)348    filename_ = pathutil.realpath_with_context(filename, context)349    assert_that(os.path.exists(filename_) and os.path.isfile(filename_))350@then(u'a file named "{filename}" should not exist')351def step_file_named_filename_should_not_exist(context, filename):352    command_util.ensure_workdir_exists(context)353    filename_ = pathutil.realpath_with_context(filename, context)354    assert_that(not os.path.exists(filename_))355@step(u'I remove the file "{filename}"')356def step_remove_file(context, filename):357    path_ = os.path.expanduser(filename)358    if not os.path.isabs(path_):359        path_ = os.path.join(context.workdir, os.path.normpath(filename))360    if os.path.isfile(path_):361        os.remove(path_)362    assert_that(not os.path.isfile(path_))363@given(u'I ensure that the file "{filename}" does not exist')364def step_given_the_file_should_not_exist(context, filename):365    step_remove_file(context, filename)366@step(u'I move the file "{filename}" to "{destination}"')367def step_move_file(context, filename, destination):368    path_ = os.path.expanduser(filename)369    if not os.path.isabs(path_):370        path_ = os.path.join(context.workdir, os.path.normpath(filename))371    dst = destination372    if not os.path.isabs(dst):373        dst = os.path.expanduser(os.path.normpath(destination))374    new_path = os.path.join(dst, filename)375    if os.path.isfile(path_):376        shutil.move(path_, new_path)377    assert_that(not os.path.isfile(path_))378    assert_that(os.path.isfile(new_path))379# -----------------------------------------------------------------------------380# STEPS FOR FILE CONTENTS:381# -----------------------------------------------------------------------------382@then(u'the file "{filename}" should contain "{text}"')383def step_file_should_contain_text(context, filename, text):384    expected_text = text385    if "{__WORKDIR__}" in text or "{__CWD__}" in text or "{__HOME__}" in text:386        expected_text = textutil.template_substitute(text,387            __WORKDIR__ = posixpath_normpath(context.workdir),388            __CWD__     = posixpath_normpath(os.getcwd()),389            __HOME__    = posixpath_normpath(os.path.expanduser('~'))390        )391    file_contents = pathutil.read_file_contents(filename, context=context)392    file_contents = file_contents.rstrip()393    if DEBUG:394        print(u"expected:\n{0}".format(expected_text))395        print(u"actual:\n{0}".format(file_contents))396    textutil.assert_normtext_should_contain(file_contents, expected_text)397@then(u'the file "{filename}" should not contain "{text}"')398def step_file_should_not_contain_text(context, filename, text):399    file_contents = pathutil.read_file_contents(filename, context=context)400    file_contents = file_contents.rstrip()401    textutil.assert_normtext_should_not_contain(file_contents, text)402    # XXX assert_that(file_contents, is_not(contains_string(text)))403@then(u'the file "{filename}" should contain')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
