如何使用Java編寫自定義的RobotFramework Libhtml
本文包括2個章節java
一、 Robot Frdamwork中如何調用java Lib庫python
二、使用 java編寫自定義的Libmysql
本文做者爲:張永清,轉載請註明出處,版權歸做者全部。Robot Framework自動化測試框架核心指南-如何使用Java編寫自定義的RobotFramework Libgit
一、 Robot Frdamwork中如何調用java Lib庫github
咱們在前面介紹了,Robot Framework能夠支持跨語言,那麼對java確定也是能夠支持的。sql
並且Robot Framework中,RIDE中自己也提供了對測試案例的兩種執行方式,支持pybot和jybot兩種執行方式,以下圖所示。數據庫
其中jybot 就是以java虛擬機的形式來執行測試案例,此種方式執行,執行的環境中,須要安裝java jdk運行環境以及jython 安裝包,jython是一個python語言在java中徹底實現,也就是說用java來實現了python語言的功能,jython中不只提供了python庫,也支持java方法的執行。因此jython其實就是解決了Robot Framework中python 不支持直接調用java語言的問題。Jython 能夠從http://www.jython.org/downloads.html 連接中進行下載,而後安裝完成後,就能夠在Robot Framework中進行使用,以下圖所示。apache
咱們來看一個jython 語言來執行測試案例的例子。oracle
示例:這是一個將字符串所有轉換成大寫形式的例子,這個例子咱們用jython語言來執行。
${str} Set Variable aaBBccDDeeFF
${result} Convert To Uppercase ${str}
log ${result}
運行結果以下:
以下圖 所示,這裏咱們選擇了jybot 來進行執行,執行時,同pybot同樣獲得了咱們想要的運行結果。
其實在Robot Framework的官方網站中不少Library庫都提供了java語言版本的實現,以下圖所示。
這裏咱們以咱們在第二章節中講到的Database Library python庫來做爲示例,來看下這個庫對應的java語言的實現以及如何來經過java語言的形式來進行調用。
咱們使用時,能夠經過Robot Framework提供的maven插件robotframework-maven-plugin來直接引入
<dependency> <groupId>com.github.hi-fi</groupId> <artifactId>robotframework-dblibrary</artifactId> <version>3.1.1</version> </dependency>
或者咱們將源碼下載下來後,本身經過執行編譯打包的方式來生成均可以。這裏咱們選擇直接用源碼的形式進行編譯,源碼能夠從https://github.com/Hi-Fi/robotframework-dblibrary上直接獲取到。下載完成解壓到本身須要的目錄後,能夠經過cmd命令行切入到對應的目錄下,使用maven命令行進行編譯,前提是須要在本身的環境中事先安裝好maven的編譯環境,編譯打包時執行mvn clean install -Dmaven.test.skip=true 便可以生成咱們想要的java語言實現的Library庫,以下圖所示。
打包完成後,再生成的target目錄中,就能夠獲取到編譯好的java語言實現的Library,以下圖所示。
將編譯生成好的robotframework-dblibrary-3.2-SNAPSHOT.jar包和mysql的Driver驅動包一塊兒加入到運行環境的CLASSPATH下面,而後在測試案例集中引入DatabaseLibrary庫,以下圖所示。
引入後,咱們就能夠正常使用jython的方式來調用java語言版本的DatabaseLibrary庫了,以下圖所示。
使用java語言版本的DatabaseLibrary庫中的關鍵字時,和使用python語言版本的DatabaseLibrary庫中的部分關鍵字的傳參有些不同。Java語言版本的DatabaseLibrary鏈接數據庫時,是經過jdbc的方式來進行連接,這裏咱們列舉了java語言版本的DatabaseLibrary庫連接一些經常使用數據庫的示例,以下表所示。Java語言版本的Connect To Database關鍵字接收 [數據庫driver類|jdbc連接地址|數據庫用戶名|數據庫密碼| alias=default ]五個參數,alias若是不傳入的話,默認爲取名爲default。
Connect To Database |
com.mysql.jdbc.Driver |
jdbc:mysql://localhost:3306/world |
root |
root |
鏈接mysql數據庫 |
Connect To Database |
oracle.jdbc.driver.OracleDriver |
jdbc:oracle:thin:@servername:port:dbname |
system |
12345678 |
鏈接oracle數據庫 |
Connect To Database |
org.h2.Driver |
jdbc:h2:mem:robotframeworkt;DB_CLOSE_DELAY=-1 |
sa |
sa |
鏈接h2數據庫庫 |
這裏咱們看一個java語言實現的DatabaseLibrary庫的源碼的片斷,讓你們能夠了解下,java語言版本中是如何實現和封裝關鍵字的,這是Connect To Database關鍵字的實現方法,
咱們能夠看到是經過在方法上加java註解的方式來實現的,@RobotKeyword和@ArgumentNames註解均來自javalib-core庫,咱們本身在開發時,能夠經過maven依賴的方式引入。
<dependency> <groupId>org.robotframework</groupId> <artifactId>javalib-core</artifactId> <version>1.2.1</version> </dependency>
DatabaseLibrary庫的源碼片斷:
@RobotKeyword("Establish the connection to the database. This is mandatory before any of" + "the other keywords can be used and should be ideally done during the " + "suite setup phase. To avoid problems ensure to close the connection again " + "using the disconnect-keyword.\n\n" + "It must be ensured that the JAR-file containing the given driver can be " + "found from the CLASSPATH when starting robot. Furthermore it must be " + "noted that the connection string is database-specific and must be valid of course.\n\n" + "If alias is given, connection can be later referred with that. If alias was in use, existing connection " + "is replaced with new one\n\n" + "" + "Example: \n" + "| Connect To Database | com.mysql.jdbc.Driver | jdbc:mysql://my.host.name/myinstance | UserName | ThePassword | default |") @ArgumentNames({ "Driver class name", "Connection string", "Database username", "Database password","Database alias=default" }) public void connectToDatabase(String driverClassName, String connectString, String dbUser, String dbPassword, String... aliasParam) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String alias = aliasParam.length > 0 ? aliasParam[0] : defaultAlias; Class.forName(driverClassName).newInstance(); setConnection(DriverManager.getConnection(connectString, dbUser, dbPassword), alias); }
除了經過jython語言來調用java的Library外,咱們還能夠經過Remote的方式來實現調用Java語言實現的Library庫。從https://github.com/ombre42/jrobotremoteserver 連接中,能夠獲取到用java語言來實現的遠程接口服務,以下圖所示。
咱們不建議在實際工做中,過多的使用的jython的方式來調用java語言實現的Library,由於每一個測試案例在執行時,都須要一個啓動jvm虛擬機的過程,執行會比使用python語言的方式執行耗時更長,並且兼容性並非十分好,推薦使用Remote的方式來調用Java語言實現的Library庫。
二、使用 java編寫自定義的Lib
本文做者爲:張永清,轉載請註明出處,版權歸做者全部。Robot Framework自動化測試框架核心指南-如何使用Java編寫自定義的RobotFramework Lib
前面咱們已經講了如何調用java語言編寫的Lib庫,這一節中,咱們來介紹下如何使用java語言編寫自定義的Lib庫。
要使用java編寫自定義Lib庫,咱們首先須要構建一個java的開發工程,這裏咱們以構建一個java的maven項目爲例。在構建的maven工程的的pom.xml文件中,咱們須要引入以下必要依賴。
<dependency> <groupId>org.robotframework</groupId> <artifactId>javalib-core</artifactId> <version>0.9.1</version> </dependency>
這個是必須引入的依賴包,做用是提供了建立RobotFramework的關鍵字的註解等功能,方便咱們快速的去建立一個Lib庫以及對應的關鍵字。
<dependency> <groupId>com.github.ombre42</groupId> <artifactId>jrobotremoteserver</artifactId> <version>2.0-BETA</version> </dependency>
這個是Remote Server的依賴包,做用是能夠啓動一個RobotFramework的遠程調用接口服務,而後RIDE中經過Remote的方式就能夠鏈接到該遠程調用服務上,若是不使用遠程調用服務的話,能夠不引入該依賴包。
<plugin> <groupId>com.googlecode.robotframework-maven-plugin</groupId> <artifactId>robotframework-maven-plugin</artifactId> <version>1.1.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> </execution> </executions> <configuration> <variables> <variable>BUILDING:True</variable> </variables> </configuration> </plugin> </plugins>
這個是RobotFramework提供的maven插件,這個不是一個必須的maven工程依賴,該插件的做用在於能夠模擬咱們的RIDE同樣來執行測試案例,在使用maven編譯打包時,能夠同時來執行RobotFramework的測試案例。經過以下的方式來指定測試案例的位置。
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
在maven工程構建好了後,咱們就能夠來編寫自定義的Lib庫了,下表中描述了使用java語言來編寫自定義Lib時,一些經常使用的java註解。
註解名稱 |
使用描述 |
@RobotKeywords |
該註解通常用於java類的頭部,用來標註該java類提供的是一個RobotFramework關鍵字類。 |
@RobotKeyword |
該註解和@RobotKeywords註解須要一塊兒配合使用,@RobotKeyword註解通常用於java中的某個具體方法的頭部,用來標註該方法提供的是一個RobotFramework關鍵字。 能夠經過@RobotKeyword後面加括號的方式來講明該關鍵字的用途,好比@RobotKeyword("這是一個示例關鍵字") |
@ArgumentNames |
該註解須要和@RobotKeyword註解一塊兒使用,該註解用於標註一個RobotFramework關鍵字須要傳入的參數,使用示例: @ArgumentNames({"elementString"}) |
代碼示例:咱們這裏用java的方式來實現RobotFramework 中的Sting Lib庫(以下圖所示)中的部分關鍵字Convert To Lowercase和Convert To Uppercase。
package com.example.keywords; import org.robotframework.javalib.annotation.ArgumentNames; import org.robotframework.javalib.annotation.RobotKeyword; import org.robotframework.javalib.annotation.RobotKeywords; @RobotKeywords public class StringKeyWord { @RobotKeyword("Convert To Lowercase") @ArgumentNames({"string"}) public String convertToLowercase(String string) { System.out.print("Convert "+string+" To Lowercase"); return string.toLowerCase(); } @RobotKeyword("Convert To Uppercase") @ArgumentNames({"string"}) public String convertToUppercase(String string){ System.out.print("Convert "+string+" To Uppercase"); return string.toUpperCase(); } } 關鍵字編寫完了後,咱們還須要定義一個Library庫,經過繼承AnnotationLibrary這個類, 而且這裏咱們經過RemoteServer的方式來啓動。 package com.example; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.nio.charset.Charset; import org.apache.commons.io.IOUtils; import org.robotframework.javalib.library.AnnotationLibrary; import org.robotframework.remoteserver.RemoteServer; public class MyRemoteLibrary extends AnnotationLibrary { public MyRemoteLibrary() { // 關鍵字類的路徑 super("com/example/keywords/*.class"); } @Override public String getKeywordDocumentation(String keywordName) { if (keywordName.equals("__intro__")) return getIntro(); return super.getKeywordDocumentation(keywordName); } /** * 遠程接口服務的啓動 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { RemoteServer.configureLogging(); RemoteServer server = new RemoteServer(); //向server中加入自定義的library,而且設置遠程接口服務的端口 server.addLibrary(MyRemoteLibrary.class, 8270); server.start(); } /** * 定義Library的說明信息 * @return */ private String getIntro() { try { InputStream introStream = MyRemoteLibrary.class.getResourceAsStream("__intro__.txt"); StringWriter writer = new StringWriter(); IOUtils.copy(introStream, writer, Charset.defaultCharset()); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
定義完Library後,咱們就能夠以RemoteServer的方式來啓動咱們的遠程接口服務,以下圖中所示
在RIDE中,咱們經過Remote 鏈接到咱們啓動的遠程接口服務中,以下圖所示。
而後經過F5快捷鍵,就能夠看到咱們遠程服務中定義的關鍵字了,以下圖所示。
示例:咱們經過一個示例來調用一下咱們遠程接口服務中定義的關鍵字,以下圖所示。
運行結果以下:
Starting test: RobotFrameworkTest1.TestSuite11.TestCase001
20180822 10:04:23.328 : INFO : Convert robotFramework To Lowercase
20180822 10:04:23.328 : INFO : ${strLowercase } = robotframework
20180822 10:04:23.330 : INFO : robotframework
20180822 10:04:23.337 : INFO : Convert robotframework To Uppercase
20180822 10:04:23.338 : INFO : ${strUppercase } = ROBOTFRAMEWORK
20180822 10:04:23.340 : INFO : ROBOTFRAMEWORK
Ending test: RobotFrameworkTest1.TestSuite11.TestCase001
從運行結果看,能夠成功調用到咱們遠程接口服務中自定義編寫的兩個關鍵字。
上面咱們是經過RemoteServer的方式來調用java編寫的自定義的關鍵字,咱們也能夠改用jybot的方式來調用java編寫的自定義關鍵字,在工程中,咱們引入maven-assembly-plugin這個插件,這個插件能夠經過執行mvn clean assembly:assembly -Dmaven.test.skip=true 打包時,將全部相關的依賴包打包在一個jar包中,方便咱們在執行時,不須要手動一個個去配置執行時須要依賴的其它相關jar包。
<properties>
<!-- 定義打包時的字符集格式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<testLibraryClass>MyRemoteLibrary</testLibraryClass>
</properties>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 配置執行時的java main方法 -->
<mainClass>${testLibraryClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
另外須要注意是,咱們須要將咱們上面定義的MyRemoteLibrary這個類移動到咱們maven工程的根目錄下,以下圖所示。
執行mvn clean assembly:assembly -Dmaven.test.skip=true打包後,就能夠生成咱們須要的jar包了,以下圖所示。
將MyRemoteLibrary-1.0-jar-with-dependencies.jar 放置到java的classpath目錄下後,咱們就能夠在RIDE中引入MyRemoteLibrary庫了,以下圖所示。
引入後,咱們再用jybot的方式執行上面RemoteServer運行時的一樣示例,以下圖所示。
運行結果以下:以下圖所示。
能夠看到,運行後獲得了一樣的結果。
備註:本文內容摘選自做者本身出版的Robot Framework自動化測試框架核心指南 一書。
關於自動化測試的更多內容,請關注:
Robot Framework自動化測試框架核心指南京東官方購買
Robot Framework自動化測試框架核心指南電子版試讀
Robot Framework自動化測試框架核心指南天貓官方旗艦店購買
Robot Framework自動化測試框架核心指南噹噹網購買
Robot Framework自動化測試框架核心指南 做者簽名版本購買
相關博文彙總:
RobotFramework下的http接口自動化Create Http Context關鍵字的使用
RobotFramework下的http接口自動化Get關鍵字的使用
RobotFramework下的http接口自動化post關鍵字的使用
RobotFramework下的http接口自動化Get Response Body關鍵字的使用
RobotFramework下的http接口自動化Get Response Status 關鍵字的使用
RobotFramework下的http接口自動化Get Response header 關鍵字的使用
RobotFramework下的http接口自動化Set Request Header 關鍵字的使用
RobotFramework下HttpLibrary庫其它關鍵字
RobotFramework下的http接口自動化Set Request Body 關鍵字的使用
RobotFramework下的http接口自動化Follow Response關鍵字的使用
RobotFramework自動化測試框架的基礎關鍵字(一)
RobotFramework自動化測試框架的基礎關鍵字(二)
RobotFramework自動化測試框架的基礎關鍵字(三)
RobotFramework自動化測試框架的基礎關鍵字(四)
RobotFramework自動化測試框架的基礎關鍵字(五)
RobotFramework自動化測試框架-移動手機自動化測試AppiumLibrary介紹
RobotFramework自動化測試框架-移動手機自動化測試Open Application關鍵字的使用
RobotFramework自動化測試框架-經常使用斷言關鍵字
RobotFramework自動化測試框架-移動手機自動化測試AppiumLibrary庫其它的常見自動化關鍵字
RobotFramework自動化測試框架-移動手機自動化測試Input Text和Click Button關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Clear Text關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Click Element關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Click A Point關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Click Element At Coordinates關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Get Element Location關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Get Network Connection Status和Set Network Connection Status關鍵字的使用
RobotFramework自動化測試框架-移動手機自動化測試Element Attribute Should Match關鍵字的使用
RobotFramework自動化測試框架-DatabaseLibrary庫的使用(對數據庫的操做)
RobotFramework自動化測試框架-使用Python編寫自定義的RobotFramework Lib
RobotFramework自動化測試框架-Selenium Web自動化(-)-Open Browser和Close Browser
RobotFramework自動化測試框架-MongoDBLibrary庫的使用
原文出處:https://www.cnblogs.com/laoqing/p/12341442.html