testing - 在 STAF/STAX 中导入 json

标签 testing automation automated-tests staf

I have been working on STAF & STAX. My objective is to read a JSON file using STAF & STAX Return Testcase PASS or FAIL. I tried updating my staf to latest version with latest python version.

Python 版本详细信息

20130408-15:38:19   
Python Version : 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06) 
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)]

Here is my Code:

try:
      import simplejson as json
    except ImportError:
      import json
  title = [] 
  album = []
  slist = []
  json_data=open('d:\Json_File.txt')
  data = json.load(json_data)
  for i in range(data["result"].__len__()):
      title = data["result"][i]["Title"]
      album = data["result"][i]["Album"]
      slist = data["result"][i]["Title"] + ' [' + data["result"][i]["Album"] + '] \n'

It is giving error given below

20130408-11:32:26   STAXPythonEvaluationError signal raised. Terminating job. 
===== XML Information =====
File: new13.xml, Machine: local://local 
Line 15: Error in element type "script".

===== Python Error Information =====
com.ibm.staf.service.stax.STAXPythonEvaluationException: 
Traceback (most recent call last):
  File "<pyExec string>", line 1, in <module>
ImportError: No module named simplejson

===== Call Stack for STAX Thread 1 =====[
  function: main (Line: 7, File: C:\STAF\services\stax\samples\new13.xml, Machine: local://local)
  sequence: 1/2 (Line: 14, File: C:\STAF\services\stax\samples\new13.xml, Machine: local://local)
]

What's the process to include JSON in STAF Module.

最佳答案

STAX uses Jython (a version of Python written in Java), not Python, to execute code within a element in a STAX job. As i said i was using the latest version of STAX, v3.5.4, then it provides an embedded Jython 2.5.2 (which implements the same set of language features as Python 2.5) to execute code within a element.

Note: Jython 2.5.2 does not include simplejson since simplejson is included in Python 2.6 or later.

Appendix F: "Jython and CPython Differences" in the STAX User's Guide at talks about some differences between Jython and Python (aka CPython). Installing Python 2.7 or later in system will have no effect on the fact that STAX uses Jython 2.5.2 to execute code within a element in a STAX job. However, "simplejson can be run via Jython." I added the directory containing the simplejson module to the sys.path within my STAX job and then import simplejson.
For example:

<script> 
  myPythonDir = 'C:/simplejson'   

  import sys 
  pythonpath = sys.path 

  # Append myPythonDir to sys.path if not already present 
  if myPythonDir not in pythonpath: 
    sys.path.append(myPythonDir) 

  import simplejson as json 
</script> 

Or, if you want to use Python 2.7 or later that you installed on your system (that includes simplejson), you can run a Python script (that uses json) via your STAX job using a** element.

For example, to use Python 2.7 (if installed in C:\Python2.7) to run a Python script named YourPythonScript.py in C:\tests.

<process> 
  <location>'local'</location> 
  <command mode="'shell'">'C:/Python2.7/bin/python.exe YourPythonScript.py'</command> 
  <workdir>'C:/tests'</workdir> 
</process> 

关于testing - 在 STAF/STAX 中导入 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872359/

相关文章:

javascript - 无法从 super 测试应用程序请求中捕获异常

android - 如何转储 android UI 组件操作及其数据以帮助解决难以重现的错误

python - 云管理或基础设施管理中的自动化语言

java - selenium 中 visiblityOfAllElementsLocatedBy() 和 presenceOfAllElementsLocatedBy() 之间的区别?

angularjs - Protractor 测试后清理

java - 如何编写代码来测试另一个类的代码?

testing - 使用 Jenkins 进行负载测试

excel - 在 Excel 中获取 numFmtID 的正确 FormatCode

eclipse - 将 window tester 与 eclipse 集成以测试 UI

使用 Selendroid 在 Android Studio 中进行测试