how read write data from excel sheet selenium web driver
이 자습서에서는 FILLO API 및 SQL 문을 사용하여 Selenium WebDriver의 Excel 파일에서 데이터를 읽고 쓰는 방법을 배웁니다.
데이터 읽기 또는 쓰기는 데이터베이스 테이블에서 값을 가져 오거나 Excel 시트에서 값을 가져 와서 분석을 수행하는 데 사용하는 가장 일반적으로 사용되는 작업 중 하나입니다.
이 기사에서는 SQL 문과 FILLO API를 사용하여 Excel 파일에서 값을 가져 오는 방법에 대해 설명합니다.
학습 내용 :
SON API 개요
FILLO는 Excel 파일에서 데이터를 가져 오는 데 사용되는 Java API입니다. FILLO API를 사용하면 매개 변수화가 매우 쉬워집니다. 즉, 다른 데이터 세트로 Selenium에서 테스트 케이스를 실행합니다.
이전에는 매개 변수화를 위해 JXL API를 사용했고 나중에 apache POI가 시장에 출시되었으며이 두 API 모두에서 행과 열을 가로 질러 엑셀 시트에 저장된 값을 가져 오기 위해 큰 코드를 작성해야했습니다.
하지만 이제이 새로운 FILLO API를 사용하면 행과 열의 크기에 대해 걱정할 필요가 없습니다. 모든 것이 API에서 내부적으로 처리됩니다. API에 정의 된 클래스를 호출하고 Excel 파일에 테스트 데이터를 선언하기 만하면됩니다. jar 파일의 문서화 및 다운로드는 공식 사이트를 참조하십시오. SON Java 라이브러리
Maven을 사용하는 경우 Maven 종속성을 사용하십시오.
Maven 종속성이있는 FILLO jar
C ++의 이중 연결 목록
스타트
- Java 언어 용 엑셀 API입니다.
- .xls 및 .xlsx 파일을 지원합니다.
- SELECT, UPDATE 및 INSERT 쿼리를 지원합니다.
- WHERE 절 및 LIKE 절과 함께 또는없이 사용합니다.
Fillo로 허용되는 작업
SELECT 작업 : SELECT 문은 테이블에서 값을 가져와 최종 사용자에게 표시하는 것과 동일한 기능을 수행합니다. 여기서 SELECT 문은 Excel 시트에서 데이터를 반환합니다.
구문 :
SELECT * From Sheet Name
업데이트 작업 : UPDATE 문은 Excel 시트의 기존 레코드를 수정합니다.
파이썬에 가장 적합한 IDE는 무엇입니까
통사론:
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”
- '업데이트 시트 이름 세트 국가 = 'UK'여기서 ID = 10 및 이름 = '예수''
- “SELECT * from Sheet Name where Name like‘Jes % '”
SELECT / INSERT / UPDATE 작업을 위해 따라야 할 실행 단계 :
#1) // Filleo 클래스의 객체를 생성합니다.
Fillo fillo = new Fillo();
#두) // 연결 클래스 용 객체를 생성하고 Fillo 클래스 내에 정의 된 getConnection () 메서드를 사용하여 Excel 시트와 Fillo API 간의 연결을 설정합니다.
Connection connection = fillo.getConnection(”excelPath”);
#삼) // 시트에있는 모든 값을 선택합니다. 엑셀 안에 존재하고 출력을 문자열 변수에 저장합니다.
String strSelectQuerry = 'Select * from SheetName';
# 4) // Select 쿼리를 실행하고 Fillo API에있는 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) // 쿼리 삽입을 사용하여 엑셀 시트에 데이터를 삽입합니다.
String strInsertQuerry = 'INSERT INTO Data (SiteTitle,SiteTopic) Values('Bharat','NewDelhi')'; connection.executeUpdate (strInsertQuerry);
# 8) // 메모리 누수를 방지하기 위해 레코드 세트를 닫습니다.
recordset. Close();
# 9) // 메모리 누수를 방지하기 위해 연결을 닫습니다.
connection. Close();
이제 위의 단계를 사용하여 Selenium 프레임 워크에서 엑셀 파일 인 'TestFile.xlsx'에 저장된 행과 열의 값을 가져 와서 데모 사이트에 값을 입력합니다. https://wordpress.com/start/about?ref=create-blog-lp
행 값이있는 Excel 시트 : 'New Test Help'및 'Testing_Related_Contents'
Project 폴더에있는 Excel 파일 :
셀레늄 코드
Excel 시트에서 값을 가져오고 테스트 사이트의 입력을 가져 오는 전체 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 파일 :
엑셀에서 데이터를 가져와 사이트에 삽입 한 데모 웹 사이트 :
C ++ 입력 출력 파일
결론
- Fillo는 Excel 시트에서 데이터를 추출하는 데 매우 유용한 Java API이며 .xls 및 .xlsx Excel 파일을 모두 지원합니다.
- SELECT, UPDATE 및 INSERT 문을 지원합니다.
- 엑셀 파일을 편집 할 때주의하십시오. 필요하지 않은 행이나 열이 있으면 삭제하십시오.
- 전체 행을 삭제하지 않고 행과 열에서 값을 지우면 API는 필드에 값이 있다고 가정하고 행과 열에서 값을 가져 오려고 시도하고 그에 따라 빈 값을 얻습니다.
- 마지막으로 Excel에서 값 가져 오기가 완료되면 연결을 닫는 것을 잊지 마십시오.
행복한 독서 !!
추천 도서
- Selenium WebDriver 소개 – Selenium Tutorial # 8
- 알아야 할 상위 25 가지 Selenium WebDriver 명령
- 2021 년 최고의 10 가지 API 테스트 도구 (SOAP 및 REST API 테스트 도구)
- 초보자를위한 JAVA 튜토리얼 : 100 개 이상의 실습 Java 비디오 튜토리얼
- 상위 90 개 SQL 인터뷰 질문 및 답변 (최신)
- 30 개 이상의 최고의 셀레늄 튜토리얼 : 실제 예제로 셀레늄 배우기
- Selenium 프레임 워크 생성 및 Excel에서 테스트 데이터 액세스-Selenium Tutorial # 21
- Apache POI를 사용하는 Selenium WebDriver의 데이터 기반 프레임 워크