Parse Yaml file

Python

  • Install ruamel.yaml library
pip install ruamel.yaml
  • Default snippet to parse yaml file
from ruamel import yaml

data = yaml.safe_load(open('defaults.yaml'))
data['url']
  • Below is an implementation for python behave framework

defaults.yaml

url: https://www.google.com

SEARCH:
  EN: 'prashanth sams'

environment.py

from ruamel import yaml

def before_all(context):
 context.driver = webdriver.Chrome()
 context.data = yaml.safe_load(open('defaults.yaml'))
 context.base_url = context.data['url']
 context.driver.get(context.base_url)
 
 elem =context.driver.find_element(By.ID, 'lst-ib')
 elem.send_keys(context.data['SEARCH']['EN'])

 

Ruby

  • Default snippet to parse yaml file in ruby language
@data = YAML.load_file(File.dirname(__FILE__) + "/filename.yml")

@data['URL']
@data['SEARCH']['EN']
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s