python - 数据驱动的测试。使用 Selenium Python 从 .json 或 .yaml 文件读取参数

标签 python json selenium pyyaml

我使用 Selenium 和 Python 创建了一个框架。 到目前为止,我的数据驱动框架由 2 个文件组成: 第一个文件 - Setup.py 包含类 Actions() 包含我将使用的所有可能的功能 在我的框架中,例如:

def __init__(): 
def setup():
def tearDown():
def click():
def sendKeys():

我的 clicksendKeys 函数至少有 2 个参数。 它们采用第一个参数,例如 :id_for_elementxp_for_elementcss_for_element,对单词进行切片直到 他们获取前两个字符并通过其中之一查找元素 id、xp 或任何其他方式。然后他们使用第二个参数 指定元素的值,以及要指定的第三个参数 send_keys() 函数的值。所有这些功能 现在正在工作,但到目前为止值已硬编码在我的测试文件中。

setup.py 文件:

from   selenium import webdriver
from   selenium.webdriver.common.by import By
from   selenium.webdriver.common.keys import Keys
from   selenium.webdriver.support.ui import Select
from   selenium.common.exceptions import NoSuchElementException
from   selenium.common.exceptions import NoAlertPresentException

class Actions(object):

    def __init__(self,driver=None):
        if driver != None:
            self.driver = driver

    def setUp(self, browserName):
        if browserName == 'Chrome':
            self.driver = webdriver.Chrome()
            self.driver.delete_all_cookies()
            self.driver.maximize_window()
        elif browserName == 'Safari':
            self.driver = webdriver.Safari()
            self.driver.delete_all_cookies()
            self.driver.maximize_window()
        elif browserName == 'Firefox':
            self.driver = webdriver.Firefox()
            self.driver.delete_all_cookies()
            self.driver.maximize_window()
        self.driver.implicitly_wait(30)

    def tearDown(self, browserName):
        self.driver.quit()

    def webPage(self):
        self.driver.get('https://www.yahoo.com')

    def click(self, elemLocator, elemValue):
        elp = elemLocator
        flp = slice(0,2)
        if elp[flp] == 'id':#return by ID
            try:
                self.driver.find_element_by_id(elemValue).click()
            except:
                pass
        elif elp[flp] == 'xp':#return by XPATH
            try:
                self.driver.find_element_by_xpath(elemValue).click()
            except:
                pass


    def sendKeys(self, elemLocator, elemValue, messageValue):
        elp = elemLocator
        flp = slice(0,2)
        if elp[flp] == 'id':#return by ID
            try:
                self.driver.find_element_by_id(elemValue).click()
                self.driver.find_element_by_id(elemValue).clear()
                self.driver.find_element_by_id(elemValue).send_keys(messageValue)
            except:
                pass

        elif elp[flp] == 'xp':#return by XPATH
            try:
                self.driver.find_element_by_xpath(elemValue).click()
                self.driver.find_element_by_xpath(elemValue).clear()
                self.driver.find_element_by_xpath(elemValue).send_keys(messageValue)
            except:
                pass

#test.py file

    from Setup import Actions

class test_case_1: #find element by id

    action = Actions(object)
    action.setUp('Chrome')
    action.webPage()
    action.click('id_of_yahoo_logo', 'uh-logo')
    action.tearDown('Chrome')


class test_case_2: #find element by xpath

    action = Actions(object)
    action.setUp('Chrome')
    action.webPage()
    action.click('xp_of signIn_button', '//*[@id="uh-signin"]')
    action.tearDown('Chrome')

class test_case_3: #login to email

    action = Actions(object)
    action.setUp('Chrome')
    action.webPage()
    action.click('xp_of signIn_button', '//*[@id="uh-signin"]')
    action.sendKeys('id_username_login_inp_field','login-username','deniska')
    action.click('id_of submit_button', 'login-signin')
    action.tearDown('Chrome')

相反,我尝试从另一个传递参数值 文件,如数据驱动测试最佳实践所述 框架。我的下一步是创建一个 data.json 文件 我将在下面使用它来映射 DOM 的所有元素 格式: "id_login_button":"一些 id 值", "xp_input_field":"一些 xp值”等。请有人帮我弄清楚 如何实现这个技术?

最佳答案

如果您想使用 json 作为 uiRepo,这是简单的方法。

示例.json:

{
    "id_of_yahoo_logo":"uh-logo",
    "xp_of_signIn_button":"//*[@id='uh-signin']",
    "id_username_login_inp_field":"New York",
    "id_of_submit_button":"login-signin"
}

测试.py

# you have to just specify the name as shown below
action.click('xp_of_signIn_button)

Actions.py 中的读取值:

import json

#parse json
with open('sample.json') as f:
    uiRepo = json.load(f)

# update the generic methods to get the property form json
print(uiRepo['id_of_yahoo_logo'])

编辑 1:setup.py 未经测试的更新

from   selenium import webdriver
from   selenium.webdriver.common.by import By
from   selenium.webdriver.common.keys import Keys
from   selenium.webdriver.support.ui import Select
from   selenium.common.exceptions import NoSuchElementException
from   selenium.common.exceptions import NoAlertPresentException
import json

class Actions(object):
    # initialize uiRepo
    uiRepo = None
    def __init__(self,driver=None):
        if driver != None:
            self.driver = driver
            # load the uiRepo from json when you init the driver
            with open('sample.json') as repo:
                uiRepo = json.load(repo)

    def setUp(self, browserName):
        browserName = browserName.lower()
        if browserName == 'chrome':
            self.driver = webdriver.Chrome()
        elif browserName == 'safari':
            self.driver = webdriver.Safari()
        elif browserName == 'firefox':
            self.driver = webdriver.Firefox()
        self.driver.delete_all_cookies() #< === moved this line
        self.driver.maximize_window() #< === moved this line
        self.driver.implicitly_wait(30)

    def tearDown(self): #< === Removed browser here as you don't need browser name while closing it.
        self.driver.quit()

    def webPage(self,url): #< === Added url as param, rather hard code
        self.driver.get(url)

    # new method to get the element based on the jsonKey
    def getElement(self, jsonKey):
        locatorStrategy = jsonKey[:2]  # <=== getting first 2 chars to figure out if it's id/xp
        locator = uiRepo[jsonKey]  # < == getting value from json
        ele = None
        try:
            if locatorStrategy == 'id':  # return by ID
                ele =  self.driver.find_element_by_id(locator).click()  # <=== using the locator got from json
            elif locatorStrategy == 'xp':  # return by XPATH
                ele =  self.driver.find_element_by_xpath(locator).click()
        except:
            pass
        # if you want you can add additional logic something like highlighting the element
        return ele

    def click(self, jsonKey): # < ==== updated method to accept only jsonElementName (Removed the element value param)
        ele = self.findElement(jsonKey)
        ele.click()

关于python - 数据驱动的测试。使用 Selenium Python 从 .json 或 .yaml 文件读取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56386997/

相关文章:

python - 从 PySpark python 中的日期获取工作日名称

python - 将django1.2项目迁移到django1.4

python - 从子进程 stdout 可靠地非阻塞读取

java - 如何使用TextViews创建JSON? Android Studio

python - 为什么我的 Python 和 pip 位于不同的地方?

ios - JSON 文件将内容类型设置为 application/json AFNetworking

javascript - 无法使用 javascript 对 JSON 对象 POST 中的双引号进行转义

java - 如何使用 Java 从属性文件中选择一组 5 个随机值?

selenium - Nightwatch.js - 如何断言复选框未选中?

java - 无法访问浏览器异常 : Could not start a new session Possible causes are invalid address of the remote server or browser start-up failure