简体   繁体   English

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

[英]Data driven tests. reading parameters from a .json or .yaml file using Selenium Python

I create a framework using Selenium and Python. 我使用Selenium和Python创建了一个框架。 My data driven framework consists of 2 files so far: The 1st file- Setup.py contains class Actions() that consist of all possible functions that I will use in my framework, such as: 到目前为止,我的数据驱动框架包括2个文件:第一个文件Setup.py包含Actions()类, Setup.py包含将在框架中使用的所有可能的函数,例如:

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

My click and sendKeys functions have a minimum of 2 parameters. 我的clicksendKeys函数至少具有2个参数。 They take the first parameter such us :id_for_element , xp_for_element , or css_for_element , slicing words until they get the 1st two characters and find element by either id, xp or any other way. 他们采用第一个参数,例如我们:id_for_elementxp_for_elementcss_for_element ,将单词切成css_for_element ,直到获得第一个两个字符并通过id,xp或任何其他方式找到元素。 Then they use the 2nd parameter to specify an element's value, and 3rd parameter to specify a value for send_keys() function. 然后,他们使用第二个参数指定元素的值,并使用第三个参数指定send_keys()函数的值。 All of these functions are now working, but so far value hardcoded in my test file. 所有这些功能现在都可以使用,但是到目前为止我的测试文件中都已进行了硬编码。

setup.py file : setup.py file

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')

Instead I am trying to pass value for parameters from another file as described by the best practices for data driven testing framework. 相反,我试图按照数据驱动测试框架的最佳做法所述,从另一个文件传递参数值。 My next step is to create a data.json file that I will use to map out all elements of the DOM in the following format: "id_login_button":"some id value", "xp_input_field":"some xp value" etc. Can someone, please, help me figure out how to implement this technique? 下一步是创建一个data.json文件,该文件将以以下格式用于映射DOM的所有元素:“ id_login_button”:“某些id值”,“ xp_input_field”:“某些xp值”等有人,请帮我弄清楚如何实施这项技术?

Here is the simple approach if you want to use the json as your uiRepo. 如果要使用json作为uiRepo,这是一种简单的方法。

sample.json: sample.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"
}

test.py test.py

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

Reading value in Actions.py: 在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'])

Edit 1:setup.py Un-Tested updates 编辑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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM