how read write data from excel sheet selenium web driver
このチュートリアルでは、FILLO APIおよびSQLステートメントを使用して、SeleniumWebDriverのExcelファイルからデータを読み書きする方法を学習します。
データの読み取りまたは書き込みは、データベーステーブルから値をフェッチするか、Excelシートから値をフェッチして分析を実行するために使用する、最も一般的に使用される操作の1つです。
この記事では、SQLステートメントとFILLOAPIを使用してExcelファイルから値をフェッチする方法について説明します。
学習内容:
SONAPIの概要
FILLOは、Excelファイルからデータをフェッチするために使用されるJavaAPIです。 FILLO APIを使用すると、パラメーター化が非常に簡単になります。つまり、異なるデータセットを使用してSeleniumでテストケースを実行できます。
これ以前は、パラメータ化を行うためにJXL APIを使用し、後にapache POIが市場に登場しました。これらのAPIの両方で、行と列をトラバースし、Excelシートに格納されている値をフェッチするための大きなコードを記述する必要があります。
.epsファイルを開く方法
しかし現在、この新しいFILLO APIを使用すると、行と列のサイズについて心配する必要がなくなり、すべてがAPIによって内部的に処理されます。 APIで定義されたクラスを呼び出し、Excelファイルでテストデータを宣言するだけです。 jarファイルのドキュメントとダウンロードについては、公式サイトを参照してください- SONJavaライブラリ
Mavenを使用している場合は、Maven依存関係を使用します。
Maven依存関係のあるFILLOjar
開始
- これは、Java言語用のExcelAPIです。
- .xlsおよび.xlsxファイルをサポートします。
- SELECT、UPDATE、およびINSERTクエリをサポートします。
- WHERE句とLIKE句の有無にかかわらず使用します。
Filloで許可される操作
SELECT操作: SELECTステートメントは、テーブルから値をフェッチしてエンドユーザーに表示する場合と同じ機能を実行します。ここでのSELECTステートメントは、Excelシートからデータを返します。
構文:
SELECT * From Sheet Name
UPDATE操作: UPDATEステートメントは、Excelシートの既存のレコードを変更します。
利用できないデフォルトゲートウェイを修正する方法
構文:
UPDATE sheet1 Set Column Name= ‘Value’
INSERT操作: INSERTステートメントは、Excelシートに新しいレコードを挿入します。
構文:
INSERT INTO Sheet Name (ColumnName1,ColumnName2) VALUES (‘Val1’,’Val2’)
WHERE演算子とLIKE演算子を使用して同じ操作を実行します。
- 「SELECT * from Sheet Name where ID = 1 and name = ’Jesus'」
- 「SELECT * from Sheet Name where column1 = value1 and column2 = value2 and column3 = value3」
- 「UPDATESheetName Set Country = 'UK' where ID = 10 and name = 'Jesus'」
- 「SELECT * from Sheet Name where Name like‘Jes% ’」
SELECT / INSERT / UPDATE操作の実行手順:
#1) // Filloクラスのオブジェクトを作成します。
Fillo fillo = new Fillo();
#二) // Connectionクラスのオブジェクトを作成し、Filloクラス内で定義されたgetConnection()メソッドを使用して、ExcelシートとFilloAPI間の接続を確立します。
Connection connection = fillo.getConnection(”excelPath”);
#3) //シートに存在するすべての値を選択します。それらはExcel内に存在し、その出力を文字列変数に格納します。
String strSelectQuerry = 'Select * from SheetName';
#4) // Selectクエリを実行し、FilloAPIに存在するRecordsetクラスに結果を保存します。
Recordset recordset =connection.executeQuery(strSelectQuerry);
#5) // whileループを使用して、Excelファイル内にあるシートで使用可能なすべての列と行を反復処理します。
while(recordset.next()){ // through getfield() method retrieve the data present in a particular column System.out.println(recordset.getField('Column1')); }
#6) //更新クエリを使用してExcelファイルの詳細を更新します。
String strUpdateQuerry = 'Update Data Set SiteTitle = 'SoftwareTestingHelp.com' '; connection.executeUpdate (strUpdateQuerry);
# 7) //挿入クエリを使用してExcelシートにデータを挿入します。
String strInsertQuerry = 'INSERT INTO Data (SiteTitle,SiteTopic) Values('Bharat','NewDelhi')'; connection.executeUpdate (strInsertQuerry);
#8) //メモリリークを回避するためにレコードセットを閉じます。
recordset. Close();
#9) //メモリリークを回避するために接続を閉じます。
connection. Close();
上記の手順を使用して、Seleniumフレームワークで、Excelファイル(「TestFile.xlsx」)に格納されている行と列の値をフェッチし、デモサイトに値を入力します。 https://wordpress.com/start/about?ref=create-blog-lp
行の値を持つExcelシート:「新しいテストヘルプ」と「Testing_Related_Contents」
プロジェクトフォルダに配置されたExcelファイル:
Seleniumコード
エクセルシートから値を取得し、テストサイトに入力する完全なSeleniumコードを以下に示します。
package softwareTestingHelp.Com; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import com.codoid.products.exception.FilloException; import com.codoid.products.fillo.Connection; import com.codoid.products.fillo.Fillo; import com.codoid.products.fillo.Recordset; public class ReadWriteExcel { static WebDriver driver; //demo site -https://wordpress.com/start/about?ref=create-blog-lp //download jar file - https://mvnrepository.com/artifact/com.codoid.products/fillo publicstaticvoid main(String args()) throwsInterruptedException, FilloException{ //Calling up the GoogleChrome driver System.setProperty('webdriver.chrome.driver', 'D:\Srinivas\New folder\exe\chromedriver.exe'); driver = newChromeDriver(); //Opening the demo site - wordpress.com driver.get('https://wordpress.com/start/about?ref=create-blog-lp'); //Locating the Test data excel file String excelPath = '.\Data\TestFile.xlsx'; System.out.println(excelPath); //Create an Object of Fillo Class Fillo fillo = newFillo(); //Create an Object for Connection class and use getConnection() //method defined inside Fillo class, to establish connection between excelsheet and Fillo API’s. Connection connection = fillo.getConnection(excelPath); //Select all the values present in a sheet, which is present inside the excel and store its output in a String variable String strSelectQuerry = 'Select * from Data'; System.out.println(strSelectQuerry); //Execute the Select query and store the result in a Recordset class present in Fillo API. Recordset recordset =null; recordset = connection.executeQuery(strSelectQuerry); //use while loop to iterate through all columns and rows available in sheet present inside excel file while(recordset.next()){ System.out.println('Column 1 = ' +recordset.getField('SiteTitle')); String siteTitle = recordset.getField('SiteTitle'); driver.findElement(By.xpath('//input(@name='siteTitle')')).clear(); driver.findElement(By.xpath('//input(@name='siteTitle')')).sendKeys(siteTitle) ; System.out.println('Column 2 = ' +recordset.getField('SiteTopic')); String siteTopic = recordset.getField('SiteTopic'); driver.findElement(By.xpath('//input(@name='siteTopic')')).clear(); driver.findElement(By.xpath('//input(@name='siteTopic')')).sendKeys(siteTopic) ; connection.close(); } //Use update query to update the details in excel file Connection connection1 = fillo.getConnection(excelPath); System.out.println('Column 1 value before Update clause = ' +recordset.getField('SiteTitle')); String strUpdateQuerry = 'Update Data Set SiteTitle = 'SoftwareTestingHelp.com' '; System.out.println(strUpdateQuerry); connection1.executeUpdate(strUpdateQuerry); System.out.println('Column 1 value after Update clause = ' +recordset.getField('SiteTitle')); //Use Insert query to update the data in excel sheet Connection connection2 = fillo.getConnection(excelPath); System.out.println('Column 1 and column 2 value before insert clause = ' +recordset.getField('SiteTitle') +recordset.getField('siteTopic')); String strInsertQuerry = 'INSERT INTO Data (SiteTitle,SiteTopic) Values('Bharat','NewDelhi')'; System.out.println(strInsertQuerry); connection2.executeUpdate(strInsertQuerry); System.out.println('Column 1 and column 2 value after insert clause = ' +recordset.getField('SiteTitle') +recordset.getField('siteTopic')); } }
実行中のコードのスナップショット
SQLステートメントの結果を示すコード出力:
更新および挿入操作が実行された後のExcelファイル:
Javaで配列をパラメータとして渡す方法
Excelからデータを取得してサイトに挿入したデモWebサイト:
結論
- Filloは、Excelシートからデータを抽出するための非常に便利なJava APIであり、.xlsファイルと.xlsxExcelファイルの両方をサポートします。
- SELECT、UPDATE、およびINSERTステートメントをサポートします。
- Excelファイルを編集するときは注意してください。不要な行または列がある場合は、それらを削除します。
- 行全体を削除せずに行と列から値を消去した場合、APIはフィールドに値があると想定し、行と列から値をフェッチしようとします。その結果、空白の値が取得されます。
- 最後に、Excelからの値のフェッチが完了したら、接続を閉じることを忘れないでください。
幸せな読書!!
推奨読書
- Selenium WebDriverの概要– Seleniumチュートリアル#8
- 知っておくべきトップ25のSeleniumWebDriverコマンド
- 2021年の10の最高のAPIテストツール(SOAPおよびREST APIテストツール)
- 初心者向けのJAVAチュートリアル:100以上の実践的なJavaビデオチュートリアル
- トップ90のSQLインタビューの質問と回答(最新)
- 30以上の最高のSeleniumチュートリアル:実際の例でSeleniumを学ぶ
- Seleniumフレームワークの作成とExcelからのテストデータへのアクセス-Seleniumチュートリアル#21
- ApachePOIを使用したSeleniumWebDriverのデータ駆動型フレームワーク