Feature: FilesScenario: Search for regex in files Given a file named "file1.txt" with content matching "other"Given a file named "file2.txt" with content matching "content$"When I search for the regex "content" in each fileThen the following files should match | filename | regex | | file1.txt | other | | file2.txt | content$ |
steps.py
importrefrombehaveimport*@given('a file named "{filename}" with content matching "{regex}"')defstep_impl(context,filename,regex):withopen(filename,'w')asf:f.write('Some content\nSome other content\nAnd some more content')withopen(filename)asf:content=f.read()assertre.search(regex,content)isnotNone,f"Content does not match regex: {regex}"context.files.append({'filename':filename,'content':content})@when('I search for the regex "{regex}" in each file')defstep_impl(context,regex):forfileincontext.files:ifre.search(regex,file['content'])isNone:assertFalse,f"Regex '{regex}' not found in file '{file['filename']}'"@then('the following files should match')defstep_impl(context):forrowincontext.table:filename=row['filename']regex=row['regex']found=Falseforfileincontext.files:iffile['filename']==filenameandre.search(regex,file['content'])isnotNone:found=Truebreakassertfound,f"File '{filename}' with content matching regex '{regex}' not found"
environment.py
fromseleniumimportwebdriverdefbefore_all(context):# Set up the Selenium webdrivercontext.driver=webdriver.Chrome()defafter_all(context):# Clean up the Selenium webdrivercontext.driver.quit()
fromseleniumimportwebdriverdefbefore_all(context):# Set up the Selenium webdrivercontext.driver=webdriver.Firefox()defafter_all(context):# Clean up the Selenium webdrivercontext.driver.quit()