1

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')
JD2775
  • 3,658
  • 7
  • 30
  • 52
  • In real word, one page has many elements, if you put all elements need for script in one json file, please show us the json file with multiple elements, i need to know how you will organize the structure in that big JSON object. In the question, you just only show only one element use case. After I know the final structure, i can start write the answer code. – yong May 31 '18 at 03:06
  • @yong, I think I have a similar question, if you knew help to help I would appreciate it: https://stackoverflow.com/questions/60832771/python-and-selenium-extract-the-information-in-a-div-class-to-a-json-object-or?noredirect=1#comment107629414_60832771 – Slowat_Kela Mar 24 '20 at 15:08

0 Answers0