如何用python写一个脚本,来跑java代码上的cucumber集成测试

作者&投稿:剧岸 (若有异议请与网页底部的电邮联系)
Java运行Python脚本的几种方式~

由于在项目需要执行Python,找寻相关资料,总结出以下几种方式:

直接执行Python脚本代码
    引用 org.python包

1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
  

  2. 执行python .py文件

1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();
  

  3. 使用Runtime.getRuntime()执行脚本文件

     这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No module named arcpy。

1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

package com.ylq.test;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class Test {NumSaver saver = new NumSaver();public static void main(String[] args) throws IOException {Test test = new Test();test.read("data/file.txt","data/out.txt");}public void read(String pathIn, String pathOut) throws IOException {BufferedReader reader = new BufferedReader(new FileReader(new File(pathIn)));BufferedWriter writer = new BufferedWriter(new FileWriter(new File(pathOut)));String tmp = null;while((tmp = reader.readLine()) != null){System.out.println(tmp);String years = getYearRange(tmp);writer.write(years.substring(1,years.length() - 1) + "
");}writer.write(saver.toString());writer.flush();reader.close();writer.close();}public String getYearRange(String tmp){String[] range = tmp.split(" \\- ");List years = new ArrayList();for(int i = Integer.valueOf(range[0]); i {private static final long serialVersionUID = 8101506360053704633L;public void save(int num){this.put(num, null);}@Overridepublic Integer put(Integer key, Integer value) {return super.put(key, super.get(key) == null ? 1 : super.get(key) + 1);}@Overridepublic String toString() {StringBuilder strBuilder = new StringBuilder();for(Entry entry : this.entrySet()){strBuilder.append(entry.getKey() + ":" + entry.getValue() + ", ");}return strBuilder.toString().substring(0,strBuilder.lastIndexOf(","));}}}目录结构如下图所示:

看看是否满足你的要求

1.直接执行Python脚本代码
    引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本

  
  2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();

  
  3. 使用Runtime.getRuntime()执行脚本文件
     这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No module named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

python脚本怎么写
答:1. 安装Python解释器 要编写Python脚本,您需要先安装Python解释器。您可以从Python官网下载最新版本的Python解释器,根据您的操作系统选择相应的版本进行下载和安装。安装完成后,您可以在命令行中输入“python”命令来检查Python是否安装成功。2. 编写Python代码 安装好Python解释器之后,您就可以开始编写Python脚...

python脚本怎么写
答:1、首先在开始编写脚本之前,需要明确脚本的目的和功能。2、其次使用Python语言编写脚本代码,实现所需的功能,可以使用Python的内置函数和模块,也可以使用第三方库来扩展脚本的功能。3、最后在编写完脚本代码后,需要进行测试和调试,以确保脚本能够正确地完成所需的操作。

用Python脚本编写1到100的整数和。
答:第一种方法 a = 0 for i in range(0,100):a += (i+1);print a 第二种方法 sum(range(1,101))第三种方法 sum([ x for x in range(0,101)])

如何用python写脚本
答:1、首先需要打开电脑桌面,按开始的快捷键,点击Python2.7如图所示的选项进入。相关推荐:《Python入门教程》2、打开之后,开始编辑脚本,脚本第一行一定要写上 #!usr/bin/python表示该脚本文件是可执行python脚本,如果python目录不在usr/bin目录下,则替换成当前python执行程序的目录。3、脚本写完之后,...

如何用python写一个脚本,来跑java代码上的cucumber集成测试
答:1.直接执行Python脚本代码 引用 org.python包 1 PythonInterpreter interpreter = new PythonInterpreter();2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本 2. 执行python .py文件 1 PythonInterpreter interpreter = new PythonInterpreter();...

用Python制作一个自动抢票脚本
答:开发环境:版本:python3.8.8。编辑器:pycharm。第一步,实现免登录。确定目标,设置全局变量。初始化加载 登录调用设置cookie 获取cookie 登录 打开浏览器 第二步,抢票并下单 判断元素是否存在 选票操作 选择座位 下单操作 抢票完成,退出 测试代码是否成功 最后看一下效果如何 ...

Python 如何写脚本?
答:Python写脚本步骤如下:1、首先下载安装python,建议安装2.7版本以上。2、打开文本编辑器,推荐editplus,notepad等,将文件保存成 .py格式,editplus和notepad支持识别python语法。脚本第一行一定要写上 #!usr/bin/python,表示该脚本文件是可执行python脚本,如果您的python目录不在usr/bin目录下,则替换...

想用python编写一个脚本,登录网页,在网页里做一系列操作,应该怎样实现...
答:python编写一个脚本的具体操作:1、首先,打开python并创建一个新的PY文件。2、其次,import os,因为涉及系统文件的路径,因此首先在此处导入系统模块。3、随后,可以双击打开HTML文件,然后就可以看到书写的网页,如下图所示。4、最后,添加html.close(),需添加此行代码以关闭,否则将占用大量内存,如...

如何利用pyrhon脚本输出txt文件和csv文件的相同数据,并将其输出为txt文...
答:你可以使用 Python 的内置模块来实现输出相同数据到 txt 文件和 csv 文件,并将其输出为 txt 文件。以下是一个示例代码:```python import csv 准备数据 data = [['Name', 'Age', 'City'],['John', '25', 'New York'],['Alice', '30', 'London'],['Bob', '20', 'Paris']]输出...

怎样开始写第一个基于python的selenium脚本
答:1、下载并安装python 2、安装selenium下载并解压缩selenium-2.32.0.tar.gz. 把selenium-2.32.0\py\下的selenium整个文件夹放入Python33\Lib\site-packages目录下。3 下载Eclipse后安装pydev插件 4 打开eclipse创建pydev project,并添加一个Pydev Module 输入一下代码 coding=gbk from selenium ...