I am designing a test framework utilizing Selenium in Python. I am trying to seperate out web element information into a JSON file and reading from that into my framework. My Test script will then pull from this script below, so all 3 entities are separate. I just started so I have one web element to test.
Script looks like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
from Values import strings
elements = 'elements.json'
data = json.loads(elements)
class HomeScreen:
def __init__(self, driver):
self.driver = driver
self.title = WebDriverWait(self.driver.instance, 10).until(
EC.visibility_of_element_located((
By.CLASS_NAME, data['name'][0])))
def validate_logo_is_present(self):
assert self.title.is_displayed()
elements.json file looks like this, currently only one entry:
{
"name": "LOGO",
"element": "logo img-responsive",
"type": "CLASS_NAME"
}
What I want to do for this particular case is access "logo img-responsive" in the following line of the top script:
By.CLASS_NAME, data['name'][0])))
I won't paste my test script because that is irrelevant to this I think, but when I run it I get the following error:
raise JSONDecodeError("Expecting value", s, err.value) from None
E json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Any ideas how to fix this? And in general, is this a bad idea to keep elements in the json file? Should I just embed them directly into the top script above? I figure using the file I would eliminate possible redundancy, but I know it will get lengthy.
EDIT
If it helps, this is how I am writing to the JSON file:
def write_to_json(lst, fn):
with open(fn, 'a', encoding='utf-8') as file:
for item in lst:
x = json.dumps(item, indent=4)
file.write(x + '\n')
write_to_json(res, 'elements.json')