简体   繁体   English

Robot Framework:如何连接Oracle数据库

[英]Robot Framework : how to connect to Oracle database

We are trying our luck with robot framework for tests.我们正在尝试使用机器人框架进行测试。 Automation.自动化。 I am stuck at database connection at this point.此时我被困在数据库连接上。

A DB connection using cx_Oracle is displaying an error saying “ No keyword withy the name cx_Oracle'.使用 cx_Oracle 的 DB 连接显示错误消息“No keyword withy the name cx_Oracle”。 If you have any idea please help.如果您有任何想法,请提供帮助。 It will be helpful if you could put out an example of the Oracle dB connection sample.如果您能提供 Oracle dB 连接样本的示例,将会很有帮助。

Check DatabaseLibrary available with RobotFramework.检查 RobotFramework 可用的DatabaseLibrary

Check Here for more info !!! 在这里查看更多信息!!!

DatabaseLibrary Keyword Documentation 数据库库关键字文档

It has two keywords for Database Connection:它有两个用于数据库连接的关键字:

  1. Connect To Database连接到数据库
  2. Connect To Database Using Custom Params使用自定义参数连接到数据库

I had the same issue and This is the solution which I found我遇到了同样的问题,这是我找到的解决方案

Step 1: Install Oracle instant client (32 bit) ( I'm using instantclient_18_3, You don't have to install cx_oracle seperately )第一步:安装Oracle instant client(32位)(我用的是instantclient_18_3,不用单独安装cx_oracle)

Step 2: Install Operating System Literary and the Database Library for Robot and import it第二步:安装Operating System Literary和Robot数据库库并导入

*** Settings ***
Library           DatabaseLibrary
Library           OperatingSystem

Then in your robot script, Add The following variable and make sure it's with the test cases (NOT in an external resource file)然后在你的机器人脚本中,添加以下变量并确保它与测试用例一起使用(不在外部资源文件中)

*** Variables ***
${DB_CONNECT_STRING}    'DB_USERNAME/DB_PASSWORD@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=YOUR_DB_OR_HOST)(PORT=YOUR_PORT))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=YOUR_SID)))'

Then you can use the following keywords to set the environment variables and to run queries然后您可以使用以下关键字设置环境变量并运行查询

*** Keywords ***
Connect To DB
    [Arguments]    ${DB_CONNECT_STRING_VALUE}
    Set Environment Variable    PATH    PATH_TO_YOUR_INSTANT_CLIENT\\instantclient_18_3
    Set Global Variable    ${DB_CONNECT_STRING_VALUE}
    #Connect to DB
    connect to database using custom params    cx_Oracle    ${DB_CONNECT_STRING_VALUE}

Run Query and log results
    [Arguments]    ${QUERY_TO_EXECUTE}
    Set Global Variable    ${QUERY_TO_EXECUTE}
    ${queryResults}    Query    ${QUERY_TO_EXECUTE}
    log to console    ${queryResults}

Disconnect From DB
    #Disconnect from DB
    disconnect from database

Finally, in your test case run it like this最后,在你的测试用例中像这样运行它

*** Test Cases ***
Test Connetion
    [Tags]    DBConnect
    Connect To DB    ${DB_CONNECT_STRING}
    Run Query and log results    SELECT sysdate from Dual

This should work fine for you这应该适合你

"No keyword withy the name cx_Oracle" means that you are using 'cx_Oracle' as keyword. “没有名称为 cx_Oracle 的关键字”意味着您正在使用“cx_Oracle”作为关键字。 I do not see given library in commonly used Robot Extended libraries .我在常用的Robot Extended 库中没有看到给定的库 If the case is that you installed the library wrongly and imported it correctly into Robot script as:如果情况是您错误地安装了库并将其正确导入到 Robot 脚本中,如下所示:

*** Settings ***
Library   MyLibraryName

then I would expect it to fail on the import.那么我希望它在导入时失败。

I agree with Dinesh.我同意 Dinesh。 Try to use the Database Library first.首先尝试使用数据库库。

Or if you cannot use Database Library (the Connect To Database Using Custom Params has limitations), it is possible to write your own Robot Library that would reuse the cx_Oracle python library and expose keywords to robot.或者,如果您不能使用数据库库(使用自定义参数连接到数据库有限制),则可以编写您自己的机器人库来重用 cx_Oracle python 库并将关键字公开给机器人。 It is generally not that complicated and I did that with pyodbc.它通常没有那么复杂,我用 pyodbc 做到了。

It was indeed an installation issue.确实是安装问题。 We had to use Anaconda3 and had to installed the library under its site-packages.我们必须使用 Anaconda3,并且必须在其站点包下安装该库。 I had this one under default Python folder.The issue is now resolved.我在默认的 Python 文件夹下有这个。这个问题现在已经解决了。

You can use robotframework-oracledb-library您可以使用robotframework-oracledb-library

  • Install OracleDBLibrary module安装 OracleDBLibrary 模块

    pip install robotframework-oracledb-library
  • After the installation, you can import OracleDBLibrary and connect to Oracle Database as follows安装完成后,可以导入OracleDBLibrary,连接Oracle数据库,如下

    *** Settings *** Library OracleDBLibrary *** Variables *** ${HOST} localhost ${PORT} 1521 ${SID} ORCLCDB ${USER} SYS ${PASSWORD} Oradoc_db1 ${MODE} SYSDBA *** Test Cases *** CONNECT TO ORACLE DATABASE WITH SID SUCCESSFULLY ${DSN} = ORACLE MAKEDSN host=${HOST} port=${PORT} sid=${SID} ORACLE CONNECT user=${USER}... password=${PASSWORD}... dsn=${DSN}... mode=${MODE} ${CONNECTION STATUS} = ORACLE CONNECTION PING SHOULD BE EQUAL ${CONNECTION STATUS} ${NONE} ORACLE CONNECTION CLOSE

For more information, you can check robotframework-oracledb-library-test有关更多信息,您可以查看robotframework-oracledb-library-test

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

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