Katalon Studio通过关键字实现变量值的存储和读取

2018年8月8日 16.41k 次阅读 34 条评论 8 人点赞

在日常的自动化测试中,你一定会遇到数据的存储与读取。例如,业务操作流程中的任务编号,业务操作流程中的审批人账号等数据。这些数据往往在操作流程中需要复用。
Katalon Studio提供了关键字封装功能,所以,我们可以使用封装关键字的方式,来实现以上数据的存储以及读取。那么Katalon Studio如何通过关键字实现变量值的存储和读取呢?

封装关键字

新建关键字,在新的关键字对话框输入关键字的名称,并为关键字指定一个包。单击OK。File > New > Keyword如果不知道如何新建关键字,详细操作步骤见笔者之前写的文章:
Katalon Studio如何定义关键字
本文实现步骤解释:
1.项目根目录下创建一个名称为keyconfig的文件夹;
2.根据输入的文件夹名创建一个[你输入的文件名称.properties]的配置文件;
3.根据你输入的变量名称存储变量和数值;存储格式为:变量名=数值;
备注:
1.创建文件夹的时候先判断是否有同名的配置文件[你输入的文件名称.properties],有同名的则直接存储到该文件里面,没有则新增配置文件;
2.根据[你输入的文件名称.properties]配置文件中判断是否存在你输入的变量名称,有相同的则覆盖数据,没有则新增变量名,存储变量名对应的数据;
详细代码如下所示:

package com.example
import groovy.json.JsonSlurper

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.stringtemplate.v4.compiler.CodeGenerator.region_return
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import com.mysql.jdbc.Connection
import com.mysql.jdbc.Driver
import WebUiBuiltInKeywords as WebUI

import groovy.sql.Sql
import sun.management.counter.StringCounter
import internal.GlobalVariable as GlobalVariable
import java.util.Date
import java.text.SimpleDateFormat

class ValueUtil {

	/**
	 * Get all rows of HTML table
	 * @param caseName case name
	 * @param key Value of variable
	 * @return value  Value of variable
	 */
        @Keyword
	def propertiesWrite(caseName,key,value){
		String usrDir = System.getProperty("user.dir");
		println (usrDir);
		File f = new File(usrDir+File.separator+"keyconfig"+File.separator+caseName+".properties");
		println f.getAbsolutePath();
		if(!f.exists()){
			f.getParentFile().mkdirs();
			f.createNewFile();
		}
		Properties p = new Properties();
		FileInputStream fis = new FileInputStream(f);
		p.load(fis);
		fis.close();
		p.put(key,value);
		FileOutputStream fos = new FileOutputStream(f);
		p.store(fos,"");
		fos.close();
	}
	@Keyword
	def propertiesRead(caseName,key){
		String usrDir = System.getProperty("user.dir");
		println (usrDir);
		File f = new File(usrDir+File.separator+"keyconfig"+File.separator+caseName+".properties");
		println f.getAbsolutePath();
		println this.getClass().getSimpleName();
		if(!f.exists()){
			f.getParentFile().mkdirs();
			f.createNewFile();
		}
		Properties p = new Properties();
		FileInputStream fis = new FileInputStream(f);
		p.load(fis);
		fis.close();
		Object value = p.get(key);
		return value;
	}
	

调用关键字

封装好以上关键字,数据的存储propertiesWrite和读取propertiesRead,接下来解释一下如何在测试案例中调用该关键字。
例如你获取了一个Alert弹出框中的任务编号,你需要把任务编号存储到本地文件中,然后在下一个审批流程中需要根据该任务编号查询出该笔数据,进行审批。
详细操作步骤如下:
数据的存储
save_data
存储效果如下图所示:
save_data1
数据的读取
存储完数据以后,在下一个审批流程中需要根据该任务编号查询出该笔数据,进行审批。
读取数据如下图所示:
read_data

风里云里,我在这里等你!

文章评论(34)

  • 雪枫轩

    麻烦请教个问题,我之前短暂试用过两天katalon,比较早了,当时好像katalon不支持
    xpath中使用变量,不知道现在支持吗

    2020年7月10日
  • Amyageva

    where can i buy ampicillin?

    2020年6月6日
  • Ugoageva

    where to buy prednisone online without a script?

    2020年6月5日
    • Altumn

      Go to a mental hospital. plse

      2020年6月8日
  • Ugoageva

    lasix tablet price

    2020年6月3日
    • Altumn

      Go to a mental hospital. plse

      2020年6月8日
  • Jenna

    I am not sure where you're getting your info, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for magnificent information I was looking for this info for my mission.

    2020年5月25日
    • Altumn

      Go to a mental hospital. plse

      2020年6月8日
  • 더존카지노

    Greetings! Very helpful advice within this article!
    It is the little changes that produce the greatest changes.

    2020年5月25日
  • 더존카지노

    Greetings! Very helpful advice within this article! It is the little changes that produce the greatest changes.
    Many thanks for sharing!

    2020年5月25日
    • Altumn

      Go to a mental hospital. plse

      2020年6月8日
  • Laura

    I could not refrain from commenting. Well written!

    2020年5月25日
  • jiao120

    我也想了解下,如何存入到文件中的时候,可以显示中文,看到你说需要转换下格式,能否大概说一下

    2019年6月19日
  • seo nederland

    Nice post! Thank you.

    2019年6月1日
  • 11

    自动化测试学习交流群:670250199

    2019年3月28日
  • Brigid Tocco

    Hi there would you mind lettingme know which webhost you're using? I've loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most.

    2019年1月29日
  • Lane

    I like it whenever people get together and share thoughts.
    Great website, continue the good work! I couldn’t resist commenting.
    Perfectly written! Hello! Someone in my Facebook group shared this website
    with us so I came to check it out. I'm definitely loving the information. I'm
    bookmarking and will be tweeting this to my followers!
    Superb blog and outstanding design and style.

    2018年11月16日
  • passenger

    怎么使用java写这些代码呀

    2018年11月15日
    • Altumn

      直接在Script tab模式编写脚本;https://www.testclass.cn/katalon_studio_start.html中介绍,Script tab模式可以直接编写脚本;

      2018年11月16日
  • 范范

    博主,我看不到回复

    2018年8月29日
    • Altumn

      不好意思,我的站点用了CDN加速,需要刷新缓存,才能更新,刚更新了。你的那个问题,需要打印出来看看你获取到的内容是什么,然后再看你要存哪部分。

      2018年8月29日
      • 范范

        谢谢,博主。我还有个问题,我想要使用全局变量在不同的测试用例中。
        测试步骤:
        account设置为全局变量,且为空
        1.在teseCase Registration:account 为注册用户,且设置为全局变量
        2.在testCase login:使用全局account作为用户名登陆

        我在测试时希望从注册的测试用例中读取到该值,在测试用例登陆中使用该变量

        但使用其出现该全局变量account为null,并不显示,如何解决这种情况

        2018年9月5日
        • Altumn

          就用我这篇文章写的关键字就可以实现了。
          1.注册的时候存储account到文档;
          2.在其他案例需要调用的地方读取account,然后再赋值到你要用的地方。

          2018年9月6日
          • 范范

            我也是这样想的,但为什么提示我because (of) (Stack trace: groovy.lang.MissingPropertyException: No such property: account for class: Script1535352389798

            2018年9月7日
      • 范范

        博主 是不是需要刷新缓存 看不到回复

        2018年9月6日
        • 范范

          可以了,谢谢博主

          2018年9月7日
  • 范范

    我用这个方法value值是不是只能是数字,我输入字符串,显示为\u7269\u5316\u751F

    2018年8月28日
    • Altumn

      是的,需要把获取的内容进行一下格式转换,这个方法是存储的数字。

      2018年8月29日
      • 测试爱好者

        怎么进行格式转换啊

        2018年11月19日
        • Altumn

          请问需要转换什么格式?

          2018年11月20日