使用Jpype

时间:2017-04-07 08:35:55

标签: python selenium-webdriver jpype

ExtentReports可以在selenium java web-driver脚本中使用,以生成优质且丰富的HTML测试报告。我试图在我的selenium python web-driver脚本中使用JPype(JPype是一种努力允许python程序完全访问java类库)。 我的代码就像

from jpype import *
classpath = """lib\\extentreports-2.41.2.jar;lib\\freemarker-2.3.23.jar"""
startJVM(getDefaultJVMPath(), "-Djava.class.path=%s" % classpath)
ExtentReports = JClass('com.relevantcodes.extentreports.ExtentReports')
ExtentTest = JClass('com.relevantcodes.extentreports.ExtentTest')
LogStatus = JClass('com.relevantcodes.extentreports.LogStatus')
extent = ExtentReports("TestResult\\Test_Report.html")
test = extent.startTest("My First Test", "Sample description")
test.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)")
extent.endTest(test)
extent.flush()
shutdownJVM()

它给出了错误

Traceback (most recent call last):
File "C:\Users\test\workspace\ExtentReportsProject\ExtentReportsPackage\report.py", line 4, in <module>
ExtentReports = JClass('com.relevantcodes.extentreports.ExtentReports')
File "C:\Program Files\Python35\lib\site-packages\jpype1-0.6.2-py3.5-win-amd64.egg\jpype\_jclass.py", line 55, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class com.relevantcodes.extentreports.ExtentReports not found

我正在使用extentreports-2.41.2.jar和freemarker-2.3.23.jar。 Python中使用的是3.5.1和Java 8更新121

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我得到了答案。以下代码将起作用:

from jpype import *

classpath = """extentreports-2.40.2.jar;freemarker-2.3.23.jar"""
startJVM(getDefaultJVMPath(), "-Djava.class.path=%s" % classpath)
ExtentReports = JClass('com.relevantcodes.extentreports.ExtentReports')
ExtentTest = JClass('com.relevantcodes.extentreports.ExtentTest')
LogStatus = JClass('com.relevantcodes.extentreports.LogStatus')
extent = ExtentReports("Test_Report.html")

test = extent.startTest("Test Case 1", "Sample description")
test.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)")
test.log(LogStatus.PASS, "Step Passed")
extent.flush()

test = extent.startTest("Test Case 2", "Sample description")
test.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)")
test.log(LogStatus.PASS, "")
test.log(LogStatus.FAIL, "Step Passed")
extent.flush()

extent.endTest(test)
extent.flush()
shutdownJVM()