繁体   English   中英

Python:如何将测试数据表从 Javascript 转换为 Python

[英]Python: How do I translate testing data tables from Javascript to Python

我是 Python 的新手。 I currently have this code in Javascript (using Cypress) but unfortunately, I had issues with the iframe using Firefox so we switched to Selenium (with Python) to run the test case. 我已经尝试搜索了几个小时,但没有运气。 此脚本与 Python 的等效项是什么? 我也在写 BDD。 不确定如何处理 Python 中的数据表。

我为 Python 使用了match case ,但是在 Python 中处理带有hashes()forEach()的数据表时遇到了困难。 请帮忙。 :(

Javascript:

And (‘step title', (firstInput, dataTable)=>{
    switch (firstInput) {
        case 'First Case':
            dataTable.hashes().forEach(elem =>{
                if(elem.titleOfDataTable == 'First Data'){
                    <someActions>
                }else if(elem.titleOfDataTable == 'Second Data'){
                    <someActions>
                }else if(elem.titleOfDataTable == 'Third Data'){
                    <someActions>
                }else{
                    throw new Error(<someMessage>)
                }
            })
            break;

            default:
                <defaultActions>
        }
})

您可以简单地使用 if/elif/else 语句和for循环(假设 elem 是字典/JSON):

def test(first_input, data_table):
    if (first_input == 'First Case'):
        for elem in data_table.hashes():
            if elem['titleOfDataTable]' == 'First Data':
                <some actions>
            elif elem['titleOfDataTable'] == 'Second Data':
                <some other actions>
            elif elem['titleOfDataTable'] == 'Third Data':
                <some more actions>
            else:
                raise ValueError("Unknown title: " + elem['titleOfDataTable'])
     else:
         <default actions>

And('step title', test)  # <- modify this line according to the Python API of your test application

暂无
暂无

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

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