Session 40 – Selenium with Java | Data Driven Testing |… — Transcript

Learn how to perform data-driven testing in Selenium with Java using Apache POI to read/write Excel files in a Maven project.

Key Takeaways

  • Selenium WebDriver alone cannot handle Excel files; Apache POI is required.
  • Apache POI is a widely used open-source Java library for Microsoft Office document manipulation.
  • Maven dependency management simplifies integrating Apache POI into Selenium projects.
  • Data-driven testing involves using Excel files to supply test data for automation.
  • Proper configuration of dependencies is crucial for working with Excel in Selenium tests.

Summary

  • Introduction to data-driven testing using Excel files in Selenium with Java.
  • Explanation of how Selenium WebDriver does not support Excel file operations by default.
  • Need for integrating third-party libraries like Apache POI to handle Excel files.
  • Overview of Apache POI as a free, open-source Java API for Microsoft Office documents.
  • Steps to add Apache POI dependencies in a Maven-based Selenium project via pom.xml.
  • Guidance on finding and adding correct Apache POI dependencies from the Maven repository.
  • Details on the two main Apache POI libraries needed: poi and poi-ooxml.
  • Instructions on refreshing the Maven project to include new dependencies.
  • Mention of Apache POI supporting other Microsoft Office formats like Word and PowerPoint.
  • Clarification that Apache POI allows both reading from and writing to Excel files in automation.

Full Transcript — Download SRT & Markdown

00:05
Speaker A
So in today's session, we'll see how we can do data-driven testing using an Excel file. But before discussing data-driven testing, we have to know how we can work with the Excel sheet, like how we can read data from an Excel sheet and if you want to write new data in the Excel sheet, how we can write data in the Excel sheet. So these are the things we have to know first, and then we'll see data-driven testing.
00:23
Speaker A
the Excel sheet how we can write data in Excel sheet so these are the things we have to know first and then we'll see data R testing so data R testing is basically uh we will specify or we
00:35
Speaker A
So data-driven testing is basically, we will specify or we will prepare test data in Excel files, and we read the data from Excel and we use it in our automation testing. We will pass the same data to the application and we will do testing. So that is basically called data-driven testing. The testing is driven by the data.
00:54
Speaker A
before that we need to know how to work with the Excel files Microsoft Excel files so by default selinum is doesn't support any Excel files okay selinum is only meant for web automation means what it will interact with the different type of web elements on the web page and it will perform
01:12
Speaker A
So before that, we need to know how to work with the Excel files, Microsoft Excel files. So by default, Selenium doesn't support any Excel files. Okay, Selenium is only meant for web automation. It means what? It will interact with the different types of web elements on the web page and it will perform certain actions, and we can locate elements, we can perform actions. So that's the only part WebDriver will take care of. But other than this, Selenium doesn't support anything else.
01:32
Speaker A
have to integrate with the selenium and then we will able to achieve it so Excel file or Excel Microsoft Excel doesn't support by selenium web driver by default so we need to use a third party Library which is called Apachi P Library so that name of the library is Apachi POI library or
01:54
Speaker A
So to support these things, what we can do is we can integrate some third-party APIs or some third-party libraries. We have to integrate with Selenium and then we will be able to achieve it. So Excel file or Microsoft Excel doesn't support Selenium WebDriver by default. So we need to use a third-party library which is called Apache POI library. So the name of the library is Apache POI library, or we can say Apache POI library. So this is also a third-party library or we can say an API, which we need to integrate with the existing project.
02:14
Speaker A
technology is Java only so this basically Java project which also having Maven integration right so because in the pom.xml we already added selenium uh selenium drivers so now in on top of it we will also add Apachi Li so then we will able to access different type of classes some
02:38
Speaker A
So whatever project we are currently working with, this is a purely Selenium WebDriver project, right? And underlying is Java. So the underlying technology is Java only. So this is basically a Java project which also has Maven integration, right? So because in the pom.xml, we already added Selenium, Selenium drivers. So now on top of it, we will also add Apache POI, so then we will be able to access different types of classes, some additional classes and methods from Apache POI library, and by using those classes and methods, we can work with the Excel files.
02:58
Speaker A
adding one dependency similarly we should also add another dependency another dependency for Apachi POI so now let me show you how we can add another dependency for Apachi POI so from where we will get all the dependencies what is a website mvn because we are working with mavan project so if
03:22
Speaker A
Okay, so first let us try to see how we can configure this library into our current project. It is very simple, just like how we have configured Selenium by just adding one dependency. Similarly, we should also add another dependency, another dependency for Apache POI. So now let me show you how we can add another dependency for Apache POI.
03:48
Speaker A
POI and uh you can find everything and if you want to know the documentation and everything from apach Pui you can search for the Google you can search forp and it will first go to the their official website so if I look at here this is AP POI the
04:11
Speaker A
So from where will we get all the dependencies? What is the website? MVN, because we are working with a Maven project. So if you go back to the MVN repository, and from the MVN repository, we can get all the third-party libraries. Just type with MVN repository, and this is a website, actual Maven global repository. So from it, we can download everything.
04:33
Speaker A
more examples we can find in this particular website this is official website of apachi POI so this is also free open- source API which is developed from Apachi foundation so Apachi is provided so many third party apis not only this so this is the documentation page and if
04:53
Speaker A
Now we need to search for Apache POI, and you can find everything. And if you want to know the documentation and everything from Apache POI, you can search on Google. You can search for POI, and it will first go to their official website. So if I look here, this is Apache POI, the Java API for Microsoft documents. Java API for Microsoft documents. It is not only for Excel, it will also support Word, PowerPoint, and other tools which are there in Microsoft Office Suite.
05:06
Speaker A
jar file you can just click on this link it will give the jar file that we need to add but we are not following that approach we're following Mayan project so we have to just add one dependency so to add dependency we need to just go with FN repository search for apach POI and you can find
05:23
Speaker A
And this is the main website and all the download information, documentation, more examples we can find on this particular website. This is the official website of Apache POI. So this is also a free open-source API which is developed from Apache Foundation. So Apache has provided so many third-party APIs, not only this. So this is the documentation page.
05:44
Speaker A
POI uh AP GPI common this is the first one and 525 so this is the first one we need to add in the depend dependency section previously we added selenium Java dependency now I'm adding another dependency inside the dependency so this is one this is the link if you want to directly
06:10
Speaker A
And if you go to the download section, and suppose if you create a normal Java project, if you don't have one, then we can also download a specific jar file and we can attach it. So if you want to download the jar file, you can just click on this link. It will give the jar file that we need to add. But we are not following that approach; we're following Maven project. So we have to just add one dependency.
06:28
Speaker A
uh there is another dependency also we need to add related to POI that is uh Apachi POI W XML you can see something called wo XML the second one this is the one apach API based on OPC and O XML
06:47
Speaker A
So to add a dependency, we need to just go with MVN repository, search for Apache POI, and you can find so many things here. So which one do we need to add? So in Apache POI library, these are the options you can see. There are two libraries which we have to add, actually two libraries which are related to this Apache POI which we need to add.
07:17
Speaker A
will automatically download required Char files okay now if I look at under reference libraries uh sorry yeah we already had this friend libraries so once you added these to now let me save it and then refresh your project as soon as you refresh under uh May dependencies apart from
07:48
Speaker A
So the first library is just Apache POI, Apache POI common. This is the first one, and 5.2.5. So this is the first one we need to add in the dependency section. Previously, we added Selenium Java dependency. Now I'm adding another dependency inside the dependency. So this is one. This is the link if you want to directly get this. You can open this link and you can get it. You can directly go to this link and go to the latest version 5.2.5, copy this and add it.
08:12
Speaker A
configuration we have to do so the first step we need to add these two additional dependencies in pom.xml I'll put this dependency here just you need to add them in existing form. XML dependencies in pom.xml okay that's it so once you added these two dependencies we will get some additional
08:40
Speaker A
Okay, this is the one dependency which we need to add, Apache POI. And there is another dependency also we need to add related to POI, that is Apache POI W XML. You can see something called W O XML, the second one. This is the one Apache API based on OPC and O XML schemas.
09:00
Speaker A
part of dependencies stag so at the time of adding uh you need to just make sure all the dependency should be part of dependency okay now let me close this so you can just get this URL and browse it in
09:20
Speaker A
Okay, so just get this second one, again same version 5.2.5, just copy this. So these are the two dependencies which we need to add in Apache POI. So these are the two dependencies required. So what will happen when you add dependencies? It will automatically download required jar files.
09:43
Speaker A
one directly go here and open this URL you can see the same version again 525 okay so you can get it okay so if you need I can just put in the chart box all right so now we have added the dependencies in form. XML now our project
10:11
Speaker A
Okay, now if I look under reference libraries, sorry, yeah, we already had these friend libraries. So once you add these two, now let me save it and then refresh your project. As soon as you refresh under Maven dependencies, apart from the Selenium jar files, you will also see Apache POI related jar files. So it has started downloading. You can see here 5.2.5 POI XML. So these are the jar files which are automatically downloaded by Maven project just by adding dependencies in the pom.xml.
10:34
Speaker A
everything so what we need to do is whenever uh you want to work with Excel file so mainly reading Excel and writing data into Excel these are the only two operations we do and before doing this operations first we need to understand what is an Excel what it contains so let me open
10:53
Speaker A
Okay, now that's it. This is simple configuration we have to do. So the first step, we need to add these two additional dependencies in pom.xml. I'll put this dependency here. You just need to add them in existing pom.xml dependencies in pom.xml. Okay, that's it.
11:10
Speaker A
can save it Excel is basically a file Excel file and also we can say this is Excel file Excel file contains what Excel workbook So currently this is one Excel file contains one workbook okay so if you want to create create another workbook you need to go to the file and you need to
11:33
Speaker A
So once you add these two dependencies, we will get some additional classes, some additional methods through them we can deal with the Excel files. Okay, so now our project is ready. Now let me close this and make sure all the dependencies. This is one dependency, this is another dependency, and this is another dependency. So these are all dependencies that are part of dependencies tag.
11:50
Speaker A
to that particular workbook so normally for Excel workbook we will provide a name so Excel file and which contains a workbook so the Excel workbook we will provide some name that's called Excel file name so what is workbook contains workbook contains a Excel sheets worksheets we can call
12:11
Speaker A
So at the time of adding, you need to just make sure all the dependencies should be part of dependency. Okay, now let me close this. So you can just get this URL and browse it in MVN repository. You will get this dependency. If you are not able to navigate or if you're confused which one you have to select because there are so many Apache are there, so directly go to this link, directly go to this link. It will show you all the versions Apache POI common and another one is this one.
12:36
Speaker A
workbook workbook contains a multiple sheets again every sheet contains a multiple rows every row or each row contains multiple cells so this is the hierarchy of the Excel file this is the hierarchy of the Excel file now in this hierarchy where exactly we want to from where we want to read
12:59
Speaker A
Directly go here and open this URL. You can see the same version again, 5.2.5. Okay, so you can get it. Okay, so if you need, I can just put in the chart box.
13:18
Speaker A
and also we will write the data into the cell so cell is a a final object but we cannot directly interact with the cells at the time of reading and writing so so first we need to open the file we
13:32
Speaker A
All right, so now we have added the dependencies in pom.xml. Now our project is ready to deal with the Excel files. Okay, now by adding these Apache POI jar files, we are ready to handle Excel files.
13:45
Speaker A
particular row we will find exact cell in that cell we can write the data or we can read the data so we need to follow the entire hierarchy to reach this particular cell okay and we cannot directly interact with the cell so if you want to read or write data into the cell we need to open the file
14:06
Speaker A
Okay, so now before handling Excel files, we need to understand something about Excel file, Excel sheet, and the structure of Excel file, how we can organize the data and everything. So what we need to do is whenever you want to work with Excel file, mainly reading Excel and writing data into Excel, these are the only two operations we do.
14:20
Speaker A
an object let's say Excel file workbook sheet row cell these are the main components are there in Excel sheet Excel file now to representing every component there is a dedicated classes are available what are those classes provided by selenium uh provided by apach py so if you look at
14:43
Speaker A
And before doing these operations, first we need to understand what is an Excel, what it contains. So let me open a small Excel file, a small...
14:58
Speaker A
file either in the reading mode or writing mode so if you open the Excel file in the reading mode we can read the data from Excel and if you open the Excel file in writing mode then we can write data
15:12
Speaker A
into the Excel file so to open this Excel file in the reading mode or writing mode there are two classes are available they are file input stream and file output scam so basically these two classes are from java itself okay from Java itself so these two classes are not from Apachi
15:33
Speaker A
POI why because these classes we can use it not only for Excel files whichever file you have so notepad properties file Excel files PDF file any type of file if you want to open we can use file input Steam and any type of file if you want to open in the writing mode we will use file
15:55
Speaker A
output Stream So these two classes are same for all kinds of document ments all kinds of files so that is the reason Java is already provided file input stream and file output stream by using these two classes we can open the Excel file either in the reading mode or in the writing and apart from
16:15
Speaker A
these two there are other other classes are provided by Apachi POI which will deal with workbook sheet row and cells now there are four additional classes are provided by apachi poi right xssf workbook xssf sheet xssf row xssf cell so what are those three classes the four
16:40
Speaker A
classes these four classes are representing the four components of Excel file so file is already represented using file input stream or file output stream depends upon whether you want to read the data or whether you want to write the data based upon that you can use one of the class and once
16:58
Speaker A
you open Excel file then we have to deal with the workbook the for that we have a special class called xss workbook and to deal with the sheet we have a xsf sheet and to deal with the rows
17:11
Speaker A
we have xssf row and to deal with the cell we have another class called xssf cell for remaining four components of excel we have four different like kinds of classes provided by Apachi POI so this Apache POI library is provided these four classes which are especially deal with workbook sheet row
17:35
Speaker A
and cell okay so now by using these classes we can handle rest of the components now we'll see the first operation how to read data from Excel reading data from Excel suppose I already have some data in my Excel file or Excel sheet now I want to read the data from Excel sheet and then
18:04
Speaker A
I'll print in my console window but in actual scenario is what once you read data from Excel sheet we will use the data for testing purpose we will pass the data to the application then we will do the testing that is the secondary part but before that we will try to understand how to
18:23
Speaker A
read data from the Excel file okay so this is a these are the components in Excel and if you go back this is an Excel file or workbook which contains a sheet one you can add a number of
18:38
Speaker A
sheets and every sheet contains a multiple rows every row contains a multiple cells so this is hierarchy of the Excel okay now let's see how we can handle this excels Excel how to read data from the Excel by using these four different classes this is a step by-step process very easy just
19:00
Speaker A
we need to remember a few classes and methods we can do it so what is a prerequisite before reading the data from Excel file we should have an Excel file already which contains some data now I have already some Excel file I prepared let me show you so you can keep that Excel file somewhere in your
19:20
Speaker A
system like say an automation automation files now here I have some file called data. XLS so this is my sheet one sheet two multiple sheets are there but in the sheet one I have some data so let's say
19:36
Speaker A
book name purchase date amount and then location so I want to read this data and I will print in that that data in my console window in our Eclipse okay so normally what happens is when you maintain the test data files in our project we don't keep these files outside of the project so normally we
19:57
Speaker A
don't keep them in C drive or Drive some external places most of the times whenever you maintain the test data normally what you will do is in the same project we will create a new folder like this and I'll name it as a test data folder and inside the test data we will keep the Excel files so now
20:19
Speaker A
what I will do is whatever Excel I have shown you just now I'll copy this Excel file and copy contrl C and go back to the test data select the test data folder and simply control V so it is copied
20:33
Speaker A
so here you can see data. XLS file okay so like this you can open if you if I double click on this and you won't see anything in the eclipse okay so what you need to do is you need to associate this
20:48
Speaker A
particular file with X LSS extension so when you double click on this it will ask you first time so Associated this XLS file with current editor and do not ask again so you can just select this or else show ID extensions for this file type and let me install them it is asking for this because
21:09
Speaker A
if you have if you already installed some Excel related plugins it will directly open the Excel file if I double click on this it will directly open okay so otherwise it will just show a small icon like this can see we can open with it will not show anything so now what we need to do is we
21:27
Speaker A
need to associate it this file with some extension so I'm selecting the first one show ID extensions for the file type and let me install them because currently XLS extensions are not there that's the reason it is not able to recognize now I'm just clicking in okay button so once you click on okay
21:46
Speaker A
button so it will open Eclipse Marketplace so here you can search for uh Excel something like this it will show you what all related plugins are available to identify that particular extension or if you're not able to see anything here uh another way
22:08
Speaker A
is simple double click and select the second one because Excel file we already installed Excel already there in our system select the second one say okay so currently it is empty now it's not showing anything here but if you are installed
22:29
Speaker A
uh excels plugin so then the icon will be different okay let me try to install the plugin so first open this okay so let me go to the help let me find out proper one so I can just go to Eclipse
22:48
Speaker A
Marketplace so this is the optional step so this is not manded but if you want to see exact Excel file format you need to do it so I'm just typing something called XLS X so do XLS X extension so it could not find
23:31
Speaker A
you can just type Excel plugin okay so we'll see this Excel plugin later I think we could not find here so but if you install that Excel plugin so this icon will be just like an Excel icon so how you're able to see this icon
24:21
Speaker A
here same icon will be display just visibility purpose otherwise there is no use of it okay so now I copy this Excel file into this test data folder so once we have this file in the test data folder so this is the data which we have currently in Excel file so now we want to
24:39
Speaker A
read this data and put this in the console window that's our job so now what you need to do is go to your Eclipse now go to your project create new package for today day 40 and inside this I'm creating a new class limit as reading data from from
25:10
Speaker A
Excel okay so here we are not dealing with any type of applications for now we just focus only on the Excel part so once you understood about Excel sheet and Excel part then we will try to deal with application then I will show you the entire data D test case so that is a concept
25:30
Speaker A
tomorrow we'll discuss but today we'll just focus on uh operations on Excel file okay now we'll do step by step so how to read data from Excel file so before doing that you need to remember this hierarchy this is most important so with the same hierarchy or with the same process we do it
25:51
Speaker A
first Excel file then workbook which contains a sheet sheet contains a rows rows contains a cell so same hierarchy we have to follow okay the first step is what is our what is our goal here is we want to read data from Excel file that's our main objective so before reading the data
26:11
Speaker A
from Excel sheet we have to open the Excel file so in which mode we have to open reading mode or writing mode because we are reading the data from Excel so we have to open the Excel file in
26:24
Speaker A
the reading mode so to open the Excel file in the reading mode what is the class we have to use file input stream class we have to use file input stream class now let us use it here I can
26:38
Speaker A
say file input stream file input stream that is representing a Excel file so I'm creating one variable called file equal to new file input to stream so I have created one file input stream class object and import this file input stream from java. iio this is not come from Apachi POI
27:01
Speaker A
this is comes from java itself so java. iio is a package which we have to import so when you create file input stream object this will accept this will expect the file from from where we want to read so we already have that file in the test data folder so we need to provide the location
27:21
Speaker A
of this XLS file so I'm just go to properties of this file so that I can get the location of this file so this is the location that we have to pass it here this is the location of the file
27:35
Speaker A
which includes a name of the file also okay now this Butler statement will throw some exception file not for exception just add that so this is my first step Excel file we open in the reading mode and suppose if you look at here this is hardcoded path the full path we have
27:58
Speaker A
given it went to automation my workspaces selum web driver another selum web driver test data and so on so if you want to capture this path dynamically so this current project path is still here so actual location is this one right inside the test data data. XLS file
28:18
Speaker A
is present so if you want to capture the location till here this part dynamically what we can do is we can use one method in the the last classes we discussed what is that method anyone remember yes system do get property of user. Dr so if you want to get this location dynamically what you can do
28:43
Speaker A
remove this part and write system. get property of parameter is small case letter user dot d and then conet so this will return return the current project location inside this it will go to test data folder inside this data. XLS file is present so now this file is represented with this
29:10
Speaker A
variable name or file object so this is my first step so we have to open the file in the reading mode okay now we open the file what's the next step we have to capture the workbook from this
29:24
Speaker A
particular file we have to get the workbook that's the next step so how to get the workbook we have to use a special class what is the class which you have to use for Workbook the xssf workbook this is a class now xssf workbook workbook equal to new xssf workbook and from which file we have
29:49
Speaker A
to extract that workbook this particular file so in the bracket we have to specify the file semic and this xssf workbook class we have to import from org. apach POI xssf do user model so this is actual class which is comes from Apachi POI so new xss workbook of file so this statement also will
30:18
Speaker A
throw some exception just add this IO exception so two steps we have done we open the Excel file in the reading mode we open the workbook from the Excel file so here we pass the Excel file object so obviously what this will do this will extract the workbook from this particular file
30:39
Speaker A
and once it is extracted workbook from the file we can put that inside the xsf workbook type so this particular variable is representing workbook of the Excel file now we deal with Excel file and then workbook what's the next one sheets so inside the workbook there are multiple sheets but we have
31:00
Speaker A
to extract a specific sheet from the workbook so how to extract the sheet from the workbook take this workbook object there is a method called get dot get sheet get sheet is a method so get sheet get sheet at there are two methods are there and get sheet index also there so you can use one of
31:24
Speaker A
the method so when I say get sheet you have to specify the name of the sheet so currently my name of the sheet is what sheet one that is a name you have to provide sheet one and what the
31:38
Speaker A
statement will do this particular method from the workbook will extract the particular sheet that we are going to store in a variable and what is the type of this variable is what xsf sheet this is a special class is available so we need to refer the variable with xssf sheet
31:59
Speaker A
and this we have to import okay first we have to open the file in the reading mode then extracted workbook from the file then from the workbook we get the sheet and store into the variable and instead of get sheet we also have another method suppose can use like this instead of get sheet
32:23
Speaker A
there is another method dot get sheet at and here we can pass the number and it will start from zero 0 1 2 3 4 like this you can sheet number also we can provide index okay this is alternative method
32:43
Speaker A
so you can use either get sheet or get sheet at both are correct but when I use get sheet you have to provide the name of the sheet when I use get sheet at you need to provide the index of the
32:55
Speaker A
sheet so but always if you you have 10 sheets and 20 sheets it is very difficult to find the index so most of the times we provide the name of the sheet okay so we extracted the file we extracted
33:10
Speaker A
workbook from the file then we extracted specific sheet from the file so we have number of sheets but we extracted only sheet one now inside the sheet we have so many rows and in every row there are so many cells so inside the sheet we have so many rows and and in each row there are multiple
33:30
Speaker A
cells so now we need to extract rows so sheet contains a multiple rows so how to extract the number of rows from a sheet so before we need to read all the rows and all the cells all the rows
33:44
Speaker A
data and all the cells data in each row we have to read so before doing that we have to count how many number of rows are present how many number of cells are present in each row that we need to
33:59
Speaker A
find out so for that it is just like a matrix it's just like a two dimensional array so now we need to find out number of rows and number of cells in a particular row so to find out number of
34:13
Speaker A
rows you need to take the take the sheet object sheet dot there is a method called get last row num sheet Dot and get last row num this is the method and this method will return the last row
34:31
Speaker A
number so suppose here this is my data this is my first row second row third four five six rows are there okay so so total six will be written so what is that question worksheet and Sheet are the same yeah the terminology is different okay worksheet and sheet is same exactly the same no
34:51
Speaker A
difference no they are not different they are same when I call worksheet or sheet both are same there is no difference so to represent in the sheet we use xssf sheet is a class there some people will call it as a worksheet some people just call it just a sheet that's it both are
35:13
Speaker A
same see there are synonyms are always there right so synonyms is always there there will not be a single word for anything so one item we can call them with multiple names same thing here also so the sheet is also called as a worksheet what is the new thing in that same thing workbook is also
35:38
Speaker A
called as a workbook and row is also called as a working row cell is also called as a working cell that's it all right so now let's go back to the X so to get the number of rows from exert sheet
35:55
Speaker A
we use a one method called get last number so this will return the last row number from the Excel sheet that is exactly equal to number of rows normally the last row number is exactly equal to number of rows right so here what is the last row number six is the last row number
36:12
Speaker A
so this is the data which we have in the sixth row so this particular number will be return returned get last row number means six will be return returned so this is the method and that I can store in a variable I can create one integer variable in into I can say total rows or number of
36:34
Speaker A
rows okay now if you want to extract the number of cells so where exactly the cells are present inside the row okay so if you want to find number of cells first we need to capture the row any Row
36:54
Speaker A
in that particular row we can count number of cells okay okay from the worksheet or from the sheet we have to extract one particular row on that particular row we have to extract the cell this is the process okay so from that particular sheet because in the sheet there are multiple
37:13
Speaker A
rows so we need to extract particular Row in that particular row we can find the number of cells we cannot find number of cells directly first we need to extract the row from the sheet then we can find
37:25
Speaker A
the number of cells now let us has captured the row from the sheet so how we can capture the particular Row from the sheet take the sheet object dot there is a method called get row get row and here we need to provide the row number in which row because almost every row is having same
37:46
Speaker A
number of cells so I will provide some number let's a row of one so normally the row index will start from zero so you can say Row one first row so this will get the first row sheet. get row
38:00
Speaker A
of one in this we want to find number of cells so get the last cell num this is the method so from the particular sheet we have to extract the particular Row from this particular row we are finding the last cell number that is exactly equal to number of cells so in total number of cells
38:28
Speaker A
so I have captured two values I find out the total number of rows and also I find out the total number of cells in each row so by using this information we have to read data from all the rows
38:41
Speaker A
and all the cells from Excel file so now let us start writing a for Loop so we need to write a two for Loop so first of all let's print this data can say system. pinell a number of rows
38:58
Speaker A
printing total rows and uh number of cells and here I can say total cells okay now just execute till here and we'll see I'm running the code till here run as Java application yeah now we can see so number of rows are five number of cells are four number of rows
39:29
Speaker A
are five number of cells are four okay so when you executing this you are getting some kind of an error uh status logger lock for J2 could not find for logging implementation please add log for J core to the class path so you may see you may see this kind of a error this will not block
39:52
Speaker A
your execution still you can work on the Excel file but if you don't want to see this we need to just add one additional Library which is related to log for J2 so log for J core so how we can do
40:05
Speaker A
it you can just search for this log for J core and go to m repository but this is not bloger okay so if you just want don't want to see that error we need to add log forj Library so just go to Mayan
40:19
Speaker A
repository and search for log for j i core now you can see Apache log for J iph core because this particular jar is missing from the dependency that's the reason it's giving some error so go to this Apache lock for core and get the dependency so get the latest dependency these are all still
40:40
Speaker A
in beta versions so 2231 you can take the stable version and I'm adding this dependency because this log for J core is missing from the existing apach dependency so just go to P XML and additionally add this dependency so this is for eliminating this particular error so once you
41:05
Speaker A
added this dependency save it and just refresh the project once again so it will download required jar file now if you execute this you don't see the error now I'm running one more time run as Java application so you got another error so no class founder error okay so let's add uh latest
41:29
Speaker A
one so sometimes if these two dependencies are not compatible for example this apach POI and this one is not compatible also it won't work so let me put another version latest version also try to update this forcefully once again
42:31
Speaker A
I think still we are getting log for J core okay let's add a log for J2 what say [ __ ] C okay so we'll see this error later I think it is not recognizing that jar files so
43:57
Speaker A
we are getting this error fine so this is not a blocker so we can still proceed with the rest of the steps so number of rows are five and number of cells are four if I look at this and go to
44:10
Speaker A
the Excel file so this is Excel file currently how many number of rows we have including header part six rows we have so 1 2 3 4 five six rows we have including header right it is giving only five what
44:27
Speaker A
does it mean mean it is counting rows from zero it is counting rows from zero so we can say 0 1 2 3 4 5 so rows are counting from zero according to the Excel and four what is this four number
44:42
Speaker A
of cells are four four means it is counting from one cells are counting from one so this is first cell second third and four four cells are there so rows are counting from zero cells are counting from one okay remember this point because this is most important so once you get
45:00
Speaker A
number of rows and cells now we'll start reading the data so how we can read it we need to write two different for Loops one for Loop is it for what incrementing the number of rows another for Loop is what incrementing the number of cells in each row is just like a two Dimension array
45:19
Speaker A
so whenever you see number of rows and columns we have to write a two different looping statements so I'm writing one another for Loop inside the another for Loop so two for Loops I'm writing nested for Loop now outer for Loop is representing rows and inner for Loop is representing the cells
45:36
Speaker A
or columns now here I'm starting rows number starting from what zero right so here I'm taking a variable int r equal to Z because row numbers are starting from zero and R less than or equal to how many rows we have total number of rows are there so equal to total number of rows
45:58
Speaker A
and r+ so number of rows are returning five so all five rows we will get so five is nothing but it is also counted from zero so R less than or equal to Total rows and r++ this is incrementation of row
46:14
Speaker A
now come to the cell part so from where we have to start reading the cell so cell numbers are starting from one actually 1 2 3 4 it is rning four but according to the Java at the time of
46:26
Speaker A
reading the the cell also it is counting from zero so as per the Excel the row count is start from zero and column count will start from one but according to the Java row count will start from zero only at the same time cell count is also starting from zero only according to the Java so
46:48
Speaker A
that is a reason here the column cell also we need to start from zero I can say in c equal to0 but here we have to say C less than total cells C less than total cells we should not say equal to C less
47:05
Speaker A
than total cells and C++ because we starting from zero it should be less than here okay but here we should be less than or equal to why here it is less than or equal to because total number of
47:18
Speaker A
cells it is returning by counting from zero itself when you count from zero itself number of rows are five that's the reason when you say zero still it should be less than or equal to total number of rows but here it is not like that actually the total cells are there 1 2 3 4 it counted from one
47:36
Speaker A
but aser Java it will start from zero so we start because we are starting from zero we say less than that is the logic now this is representing outer for Loop is representing the rows inner for Loop is representing the columns or cells now we need to read the data from the particular cell and this
47:56
Speaker A
is the crucial point so whenever you want to read data from the particular cell any cell suppose I want to read data from the cell we need to go to this particular Row first we need to extract the
48:09
Speaker A
row entire row then we have to get all the cells once it is done now go to another row then read all the cells once it is done then go to another row then read all the cells that means for every
48:25
Speaker A
row we have to repeat number of cells and once it is done next go to the next row and then repeat all the cells so before getting the cell and cell data we have to extract the particular row
48:39
Speaker A
okay in the previous step also we have seen so to count number of cells first we have extracted the particular row and then we find out the number of cells similarly when you want to read the data from the cell first we need to extract the particular row and then we can read all the cells
48:57
Speaker A
so inner for Loop is representing for what cells so before reading the data from the cell that means before using inner F Loop first we need to get the particular row once you get the row then we will start repeating this inner F loop on that particular row so how to extract the row from
49:16
Speaker A
the sheet same method sheet dot get row and which row we want to get r r is representing row number so get row sheet do get row so what this will return this will return return a row object that
49:34
Speaker A
we have to store in a variable so I can create as a current row some variable I'm creating and what is the type of this variable this get row method will return return the row entire row and the
49:46
Speaker A
type of this row is what xssf Row Third object this this one so row is represented what xssf row only so this variable type is des and xss of row we have to import so what we have done
50:03
Speaker A
here is before reading the cells using inner for Loop we have to get the entire row and once you get entire row from this particular row we can read all the cells by using inner F okay now by
50:20
Speaker A
taking this current row here we want to read all the cells so how to read the cells current row dot there is a method called get cell and which cell we want to get we pass the number so C cell in that particular cell there is some data is present that data we
50:41
Speaker A
have to get so from this particular cell I can get the data by calling another method called two string this is the method I can use so from the current row it will get particular cell from that particular cell two string method will return the data from the cell okay this is
51:04
Speaker A
a combined statement suppose first I want to extract the cell then I want to get the data then what you should do is as soon as you get a cell from the current row store that cell in a variable and now the type of this variable is xssf cell this also we can import so now all the
51:26
Speaker A
classes have covered workbook is represented with xsf workbook sheet is represented with the sheet xsf sheet row is represented with xsf row cell is represented with xsf cell if I look at the code here you can notice this is a file workbook sheet and here we capture the current row and here we
51:48
Speaker A
capture the current cell so from the current row we extracted particular cell and storing into the cell object now from the cell object we can extract the data cell dot get cell get string uh two string this is the common method we use to capture the data from the cell there
52:10
Speaker A
are other methods also there I will introduce them later two string is a method so this will actually get the data from the cell now we can print the data so by using system dos out. print Ln system out. print Ln you can print cell do to string like this okay so now what will happen let
52:39
Speaker A
me repeat once again so we have find out the number of rows and cells from the Excel sheet and now first we have to repeat the rows from 0 to five and then we have to repeat the column from uh
52:53
Speaker A
1 to 4 so before getting the cells we have to extract the particular row how we can extract the row from the sheet sheet. get row of r r is what representing row number in every iteration in every round of execution we are passing the different row number accordingly it will return
53:12
Speaker A
the particular row that will become the current row and from the current row so this inner for Loop is representing reading the cells in the particular row so which in which row we want to read the cell current row so current row. get cell of c c is representing the cell number that we are
53:29
Speaker A
passing it will return the cell object once you return the cell object from that particular cell we are extracting the data by calling two string method that we are printing so first it will get the first row and it will read all the cells once it is done again it will go up outer for loop
53:46
Speaker A
again get another row and read all the cells third iteration get another row and again read all the cells so like this it will repeat outer for Loop is repeat five times because we have a five rows inner for Loop will repeat four times in every row because we have a four cells so once this Loop is
54:06
Speaker A
executed we will get all the rows and cells from the Excel sheet so let me execute it and once we have read everything two things we have to mainly we have to do what is that first we need to close
54:20
Speaker A
the workbook because we open the workbook from the file right once our job is completed workbook Dot close we have to call this method because whenever you open the workbook and worksheet it will occupy certain amount of memory so we need to clear that memory so workbook do close
54:39
Speaker A
so once you close the workbook then we have to close the file also because here we open the file in the reading mode and after completion of our job close the file also file Dot close so these are the two things we have to do now let us execute and see whether it's reading data or not
54:57
Speaker A
I can say run as Java application yes so now we can see we got all the data from Excel sheet and if I look at this data Excel sheet this is my data current data which we have and book name purchase
55:13
Speaker A
date amount location these are the headers actually and it is combined everything just I want to place this in row and columns format just a small design I can do so what I have to do is instead of print Ln you can simply say own only print and after printing the value we can
55:30
Speaker A
just give one tab space so here I'm giving slity so that it will give one tab space after printing the value and after completion of inner for loop I will jump to the next line for that I'm using
55:45
Speaker A
only print Ln so this will print the values in single line after printing all the cells in single line then it will go to the next line so now this time you will get the data exactly in the table format let me execute yes now we can see this is the data so we got all the book names
56:07
Speaker A
purchase dates amounts and location so some basic overlapping is there but that is fine okay so these are the purchase dates we got amounts you got and location also we got so this is how we can simply read the data and here you have to not no is one thing do you see any difference between
56:29
Speaker A
both data and data which is there in Excel data we have read in the console window do you see any difference do you see any difference guys the data book names are exactly printed no change purchase data also exactly printed no change location is also exactly printed only amount is little bit
56:55
Speaker A
change so actually the amount it don't have any decimals here but here additionally decimals are added why because two string method we have used actually so two string method this is the common method which will read all the data in a string format so basically Excel also having some data
57:14
Speaker A
types Excel is also having their own data types for example if you right click on any particular cell go to format cells and here you can see the different data types General number currency okay okay date time percentage there are different kinds of data types are available in Excel file
57:32
Speaker A
so at the time of reading this data we will use one common method which is called two string so what the two string method will do is two string method will convert any type of data into string format the data can be any it can be date or it can be number integer it can be string whatever
57:52
Speaker A
it is the whole data will be part of in the form of string only so that is the specialty of the two string method is a one common method which we can use this will read entire all kinds of data into
58:04
Speaker A
string format so in that particular process the amount whatever we specified in Excel sheet that is also read in the form of string at the time of reading in the form of string by default it will add dot zero decimal point it will add but there is no difference even if you add decimal point do
58:23
Speaker A
zero no difference it is s again representing 200 200 and 200 are zero there is no difference okay the two string method will add decimals to the existing number okay remember that point so other than this rest of them exactly the same so this is the process of reading the data from the Excel
58:44
Speaker A
sheet we'll able to read all the rows whatever rows are exist and we are able to read all the columns whichever they are exist so is this clear to everyone so far please confirm in the chat box rows are starting from zero that's the reason it is returning five as per Excel row
59:12
Speaker A
count will be starting from zero and cell count is starting from one as per Java row count and cell count both will start from zero only that's the reason we started from zero here okay so this is a step by-step process let me repeat once again what we have done first
59:33
Speaker A
you open the file in the reading mode then we extracted workbook from the file then we extracted specific sheet from the file by using get sheet method and we find out total number of rows by using get last row number we find out total number of cell by using get
59:49
Speaker A
last cell number we have printed and then we have written a looping statement in that outer for Loop is repeating number of rows inner for Loop is repeating number of cells in each row but before reading the cells from each row we have to extract the particular row then only we
60:06
Speaker A
will able to read the cells so once we are able to read the cells we are printing the data and that's it so after completion of reading data we are closing the workbook we are closing the file okay so here the classic for Loop must be there because we are dealing with the index okay if
60:26
Speaker A
you're dealing with the indexes you must use a classic for Loop enhanced for Loop is only for collection types remember that point only when I work with the list collection or set collection hash map hash set in those cases only we have to use a enhanced for Loop in all other cases
60:45
Speaker A
we use a normal for Loop okay this is all about one example how to read data from Excel sheet so if the row start from zero then line number 28 where is line number 28 yeah if the rows are
60:59
Speaker A
start from zero then in line 28 why is right get you can put zero also no problem you can put zero also nothing wrong in this I just put some row number you can put zero also nothing wrong okay so still you got a number of rows are five number of rows are four any row because all
61:22
Speaker A
the rows are having same number of cells in that case you can put any row number number here no problem understood everyone so now I'll show you how to write data into the Excel file so we'll take one empty Excel file then I will write the data new data in the Excel
61:41
Speaker A
file so we'll see how we can do that exactly opposite so whatever methods we used here exactly opposite methods we will use okay let me show you how we can do this let's close it and create a new class writing data into
62:04
Speaker A
Excel and take the main method you can also create another empty Excel file in the same test data folder or else we can maintain a separate place so in this I will create one uh a new file so do we have any file here okay so what I can do is here you can create your own Excel
62:42
Speaker A
before or is through our automation you can create our automation script first it will check the Excel is exist or not Excel file exist if Excel file is exist it will just write the data to the Excel IF the Excel file itself is not exist then it will create a new Excel file then it will
62:59
Speaker A
create a data so for now uh I don't have any file inside the test data I will create my own Excel file and then I will update the data in the test data okay so now what I will do is writing data
63:13
Speaker A
into the Excel currently I don't have any Excel file inside the test data other than this data.
63:17
Speaker A
XLS I will create a new Excel file and then we will write a data so we'll see how we can do step by step same exactly opposite it of the previous one in the previous one we have read
63:28
Speaker A
the data but this time we have to write our own data now again same hierarchy we have to follow file workbook sheet row and cells okay now first step is what we need to open the Excel file if the
63:45
Speaker A
file is already exist we can open it if the file is not exist it will automatically create a new Excel file but uh in which mode we have to open the file in the reading mode or writing mode in
63:58
Speaker A
which mode we have to open the file reading mode or writing mode writing mode so to open the file in the writing mode what is the method we have to use file output stream okay file output stream so
64:10
Speaker A
let's use it close this file so first step file output stream file equal to new file output stream of so you need to specify the location of the file if you already file is exist empty file that location you can provide here if the file is not exist still no problem it will automatically
64:37
Speaker A
create a new file so where you want to create a new file inside my test data I will create my new file so how to extract the location of the test data file test data folder and this is the
64:49
Speaker A
location okay so how to extract this uh default location from the web from the project by using system dot get property of here I can say user DOD okay and concatenate single quotations and I can remove the rest of the thing I just provide
65:17
Speaker A
only folder name so test data slash and you can provide some name of the file I can say my file dox X okay now this is the file I want to create because currently I don't have such file here
65:34
Speaker A
so I'm going to create a new file if file is already exist no problem it will override if the file is not exist it will create a new file so my file. XLS I'm going to create inside the
65:45
Speaker A
test data folder again this is a part of your current project directory now import this file output stream from java. IO package and this will throw some exception just add this yeah so now we created a new file now here we have to create everything we have to create newly we created
66:07
Speaker A
a file we have to create a workbook we have to create a worksheet we have to create a row we have to create a cell then we can update the data everything is newly creation okay so in this particular file we have to create a new workbook new workbook we have to create how to
66:23
Speaker A
create a new workbook in the file simply you can take xssf uh workbook class xssf workbook workbook equal to new xssf workbook so the workbook is a class which is representing the workbook right so just create an object for the workbook and don't pass this file inside the workbook for
66:52
Speaker A
now we will do this at the end of the code because we just created only file still we are not using the file first we have to create a workbook we have to create a row we have to create a cell
67:06
Speaker A
we have to update the data and finally we have to attach this workbook to the file that is a final step So currently there is no connection between these two but earlier how we have done because already existed file we are reading the data from the file so we have extracted the workbook from
67:23
Speaker A
the file so we have passed file as a parameter in the previous case but now we are everything creating newly so we should not pass anything here just it will create a new workbook finally after writing the data into the workbook we will attach to that to the file we'll do the Step at
67:40
Speaker A
the final level now we'll create a workbook inside the workbook we have to create new sheet sheet we have to create so workbook dot create sheet this is the method prev L which method we used to get the sheet from the workbook get sheet method we used and this time create sheet so we can
68:05
Speaker A
provide some name of the sheet I can provide some name called sheet one sheet two sheet three is a common names but here I'm giving a different name called Data data is my sheet name and this I'm going to store in a variable called sheet and the type of this variable is what xssf sheet
68:26
Speaker A
and this xsf sheet is also we need to import okay so we created a file we created a workbook we created a sheet and inside the sheet we have to create multiple rows and every row we have to create multiple cells in every cell we have to write some data so let's create a first row
68:49
Speaker A
in the sheet how to create a row in the sheet sheet dot create row create create row which row zero because row count will start from zero only right create row so once you created this row that row I will store in one variable called Row one and here I'll name it as xssf row object
69:13
Speaker A
so in this step what I have done in the sheet I have created first row and I given the name as a row one the type of this variable is row xssf row now in this row I want want to create
69:26
Speaker A
multiple cells currently we are in the first row in the first row I will create multiple cells now let's take this Row one and in the row one how to create a new cell dot create cell and which cell zero zero cell dot inside the cell we have to update some value how we can update
69:49
Speaker A
the value using another method called set cell value set cell value you can see set cell values having different flavors we can update bullan value date double okay all kinds of string all kinds of data you can update in the cell so set cell value here I'm passing some data let's say
70:13
Speaker A
I'm passing string data called Welcome this is only one cell in row one now I want to write another cell in the same row again take Row one create cell of one and set cell of and here I'm
70:26
Speaker A
writing some number welcome I can say 1 2 3 4 and again in the same Row one I write another cell so in the same Row one another cell two and here set cell value here I will write something else I can
70:41
Speaker A
say I can put another string let's say automation like this and in the first one I can say Java okay so now in the sheet I have created first row and I updated three cells now same thing we
71:02
Speaker A
repeat for multiple rows so create another row create another row so same thing we'll repeat as per Java we have to start from zero that's the reason I'm starting from zero create cell is zero previously also at the time of reading the data from cell also we started from zero only but
71:23
Speaker A
according to the Excel Counting will be different Excel will count from 1 2 3 4 but according to the Java start from zero so that's the reason either while reading the row or while reading the cell we always start from zero remember this okay now I'm creating another row so I can say create row of
71:41
Speaker A
one and here this is a row two now in the row two I will create a new cells everything you have to CH now in the row two I create zero cell and I'll update a new value let's say python and uh in the
71:57
Speaker A
row two another cell and here update another value python uh let's say this is uh 19 19th version It's a version number 19th and let's say Python 3 is a three version automation so in the zero row I
72:15
Speaker A
say python another cell I say three and automation this is a second row similarly I can put another row in the same uh Excel sheet third row third row so row number three row number three and here create row of zero create row of one create number of two create row of two and again this we need to
72:40
Speaker A
change Row three in the row three we are getting cell number 0 1 2 and updating the value here I'm writing another value C sh five and automation like this so how many number of rows we created three rows how many cells we have created in each row three cells so once it is done so we
73:03
Speaker A
open we create a new file workbook we created sheet we created we created the three rows and as soon as we created the three rows in every row we assigned some data so once it is done what is
73:17
Speaker A
the final step is we need to attach this workbook to the file so far there is no connection between these two we created a new workbook in inside this we created a sheet inside this we created a rows and cells and finally once your job is done we will attach this workbook to the file so how
73:35
Speaker A
we can attach this very simple workbook workbook dot write workbook do write and this particular workbook this particular workbook we are going to write into the file this is the command okay so this workbook we can write into the file so this file output stream you can also write here
74:01
Speaker A
no problem okay you can also put file output stream here because first we need to create a workbook then create a sheet create rows and cells and finally we will write this workbook into the file that's the reason you can also put this file output stream here or you can put in the beginning
74:21
Speaker A
because even though if you create this in the beginning we are not using this file till end of the script so here we need to do it so once it is written then we need to close workbook so I can
74:35
Speaker A
say workbook do close I'll show another examples just hold on this we have written simple static data if you want to write a dynamic data into the Excel then we have to go with the looping statement I'll show you another example just hold on workbook do close then we need to close a file
74:52
Speaker A
also file doose okay okay and finally we will return return some confirmation message I can say simple file is created that's it so this is the final script which will create a new file new workbook new sheet new rows new cells and new data everything we created newly nothing is present so
75:19
Speaker A
now currently I don't have any file inside my test data but I'm great this one my file. XLS which contains all these data now let us execute and see run as Java application yeah now you can see file is got created now just refresh the folder if you don't refresh you cannot see the file just
75:43
Speaker A
refresh now you can see my file. exls now if you cannot open this file directly copy this file and put somewhere here okay now I'm opening the same file my file. XLS now you can see this is the data
76:00
Speaker A
which we have add data three rows three columns okay clear everyone so that file name what is the sheet name we have given data so you can see sheet name is also updated as a data okay so this
76:20
Speaker A
is how we can write data into the Excel so very simple simple to remember the methods very easy so suppose if you want to open the file in the reading mode file input stream you have to use if
76:33
Speaker A
you want to open the file in the writing mode file output it which is clear and if you want to get existing sheet we have to use get sheet method if you want to create a sheet we have to use create
76:43
Speaker A
sheet method similarly if you want to extract the row we have to use get row method earlier we have used but if you want to create a new sheet create a new row we have to use create row
76:56
Speaker A
okay like this so similarly create cell get cell if cell is already exist we can use get cell and if you want to create a new cell we have to say create cell so this is how we can read the data
77:12
Speaker A
so now I'll show you another example suppose here I have added static data now this data I want to pass it run time once I executed my program then I will pass data dynamically and same data I will
77:28
Speaker A
add to the Excel file okay so we'll see how we can add Dynamic data at runtime using for Loop now let me close this everybody's understood this example because based on this example the next one is dependent everybody is clear what are all methods we have to use you need some practice
77:51
Speaker A
okay so initially you can't remember all these methods but once you start prac in two to three times you will able to remember this reading and writing only methods are different and also it is very easy to remember all the methods all right so here I have written some static rows three rows
78:09
Speaker A
and three cells in every row now I want to create dynamically n number of rows n number of cells and even data is also some different so how we can do that let me create another example so same class
78:25
Speaker A
I'm taking one more time and writing data into the Excel writing Dynamic data Dynamic data into the Excel okay now uh same process but I'm just removing this part Dynamic data we want to do everything is same so only thing is now this time we have to accept the data
78:55
Speaker A
from the user dynamically even number of rows number of cells also automatically create okay now we have to create a new file so my file is already exist now I can say my file one okay I'm taking another or else I can say my file uncore Dynamic new file name I'm giving
79:17
Speaker A
my file _ Dynamic now we need to create a new workbook new sheet the sheet name is data or I can say Dynamic data I'm changing the sheet name Dynamic data so this is my sheet now randomly we have to provide number of rows number of cells and number of data and everything so
79:37
Speaker A
dynamically we have to capture the data and what I'm saying here is dynamical say the user has to provide the data so we have to accept the data from the user at the runtime how can we accept the data from the run in the runtime how we can take the from the user using scanner class okay
80:00
Speaker A
so now here after creating the file workbook and Sheet I'm trying to use one scanner class here scanner SC equal to new scanner of new scanner of what is the parameter we have to use here system do in now because we want to take the data from the user so this scanner class we will import
80:25
Speaker A
from java. so take this this SE will be used to get the data from the user later now I'm writing one F Loop this is for number of rows and this is for number of cells outer for Loop is for
80:39
Speaker A
creating the rows inner for Loop is for creating the cells in the particular row now in this I will start from inter r equal to zero row number are representing the row number we are going to create everything newly and how how many rows you want to create that you want to decide here
80:58
Speaker A
how many rows you want to create so even that value also if you want to accept from the user you can accept the user will enter number of rows and columns okay for example here I'm asking user system.out.print enter how many rows do you want to have how many rows do you want to have or you
81:27
Speaker A
can say enter how many rows very simple message how many rows and that also we can accept how to accept SE dot what is the method what is the method to accept integers s dot next in right this will get and that I'm storing in a variable in number of rows number of
81:55
Speaker A
row similarly I will also accept number of cells from the user Dynamic enter how many cells enter how many cells in every row that also I'm capturing storing it the number of cells okay so rows I'm expecting from the user number of cells also I'm expecting from the user now how many
82:20
Speaker A
times the outer for Loop should repeat depends on the number of rows so r equal to 0 R less than or equal to number of rows R ++ and inner for Loop is representing the cells even cells also we have to
82:36
Speaker A
start from c equal to Z but C less than we have to use less than number of cells how many cells we want to create and then C++ okay now we have to create new rows and new cells then we will update
82:53
Speaker A
the data everything we have to create newly so before inner for Loop is representing the cells outer for Loop is representing the rows so before starting inner for loop I will create a row first first we need to create a row inside the row we can create multiple cells so how to create
83:13
Speaker A
a row here let's take a sheet object whatever we created here sheet dot create row which row we want to create r r is representing no row number so this will create a new row that I'm going to
83:29
Speaker A
store in a variable called current row and what is the type of this variable xsss F row object now this will create a new row based upon the number we provided and in this particular row we have to start creating the cells now come to Inner for Loop in this particular row how to create a
83:51
Speaker A
new cell dot create cell cell which cell c c is representing the cell number after creating the cell okay this will just create a cell and inside the cell we have to update the value right so this cell I'm going to store in a variable called cell and what is the type of this variable ex
84:13
Speaker A
is f cell now we created Cell and inside the cell we have to add some data even that data also we need to accept from the user right user has to provide that value so we need to provide
84:28
Speaker A
some value so how we can provide the value so in this particular cell dot set cell value set cell value here normally we will hardcode the value right like this but this time we want to get this value from the user from the user we want to get this value so user can provide any value it can be
84:55
Speaker A
number Boolean whatever so now what I will do is instead of hardcoding this value I can simply say s dot next yes C dot next so whatever the value we provided at the runtime that will be captured by this command and that will be entered into the newly created Cell okay so this is how we
85:22
Speaker A
need to do so first we have create created a new row here and inside the row we created a new cell inside the cell we added data and this data we have to provide at the run and like this
85:36
Speaker A
inner F Loop will repeat multiple times because depends upon the number of cells we provided so it will create so many cells once it is done again it will go up depends upon the number of rows it will create multiple rows in every row it will update the number of cells okay after
85:54
Speaker A
after completion of this we will close the file we will close the workbook and first we need to attach the file to the workbook then here this is the command attach workbook to the file and then we need to close workbook and file and then confirmation message I have written so this is
86:17
Speaker A
how and here I have used next in here I have used next why is it so because the number of rows and number of celles should be integer value that we already know so I have taken next int but here we
86:33
Speaker A
don't know what value the user can enter user can enter any value number string bullion anything so common method is next so next method can accept everything as a string form okay can user jump the row or cell number example if the user want to store data directly into the row number three
86:56
Speaker A
skipping yes yes but you cannot use looping statement in that case in that case you cannot use looping statement in the previous example uh are you talking about reading or writing storing data storing data means is writing in that case you can create like this by create by passing any
87:17
Speaker A
index number suppose if I don't create like this so if I say one first row will be created if I two two second row will be created inside this again in which cell you want to write the data the cell
87:29
Speaker A
number you can specify so randomly also you can put I'll show you that another example no problem so randomly in whichever cell and whichever row you want to update the data you can still do that okay so here go back so here this particular F Loop will dynamically get the data dynamically
87:47
Speaker A
get the number of row dynamically get the cells from the user and accordingly it will create number of cells and rows and even data also we're expecting from the user okay now we will see that and what is the file name I have given my file undor Dynamic let me execute and see run as Java
88:09
Speaker A
application yeah so now it is asking for how many rows how many rows you want three rows I want how many cells I want let's say five cells I want 3 into 5 15 values we want to enter totally
88:26
Speaker A
15 values we want to enter now it is asking for data so I'm giving some data let's say 1 2 3 4 5 integer data now I'm giving some string data a b c XY J again A B C M1 34 pqr some string type of
88:49
Speaker A
data now this time I will give uh some decimal numbers 10.50 10.12 56. some random data I'm providing some decimal number data okay and yeah you can also give combination let's say 12 ABC 13 XY Z some combination also you can give like this or else a b c 1 2 3 XY Z 1 2 3 so total 15 values
89:22
Speaker A
we have to give like this again I'm saying 1 2 3 4 5 6 7 yeah done 15 values are done all 15 values we provided so file is got created now we'll see the data is properly updated not randomly
89:36
Speaker A
I have given so go to test data refresh now we can see my file dynamic. XLS created just copy that file somewhere in the drive so I copi this actually you can directly open from Eclipse but currently it is not opening so so I'm just copying somewhere here now this is a file I just created
90:00
Speaker A
so let me open it yeah now we can see this is the file see this is the data which we have created so how many how many rows are created four rows are created how many cells are created Five cells
90:17
Speaker A
are created in every row okay so this is how we can also dynamically enter the data in the Excel sheet using for Loop this is an example suppose if you want to update data randomly whichever row whichever cell you want randomly you want to update so that also we can simply do that so let
90:40
Speaker A
me show you another [Music] example uh writing data in specific row and cell specific row and cell specific row and cell so in that case you can just remove this part okay first we need to create a new file new workbook and I can call it as a my file random okay different name I gave it
91:12
Speaker A
my file random so now we open the file open the workbook open the sheet now in the sheet randomly we can create a new row new cell and update that randomly you have to do how we can do
91:24
Speaker A
first let's create a random row sheet dot create row which Row in which row you want to enter suppose in the third row I want to enter create a row that I'm storing in a variable row so row
91:41
Speaker A
number three I'm giving or you can just row xssf row so third row in the third row in which cell you want to update the data that you can specify row Dot create cell in the particular row create
91:57
Speaker A
cell in which cell you want to create let's say in the third row third cell for example or fourth cell let me give four create cell right this will return return the cell object so xss F cell okay
92:13
Speaker A
this is a row and this is a cell which we created third row and fourth cell now in this I want to update data so cell dot set cell value Val set cell value and whatever value you want to put you
92:29
Speaker A
can put I can say some value here that's it so we created random row random cell and update the data so now execute this run as Java application file is got created now refresh your folder yeah now my file random. EXL has got created now now just open the file I copy here and open this perfect
93:01
Speaker A
here it is got created see according to the row and the cell number so which row it is third row fourth cell and if I look at the Excel file this is a zero Row first row second row third row so
93:16
Speaker A
third row which cell fourth cell a b c d so count is starting from zero so here it is got update so this is how we can also randomly create a row create a cell and then update the value so these
93:34
Speaker A
are all few examples but actually in the real scenario you don't write all these scripts so we will create a utility file we will create a utility file which contains a reusable methods for creating Excel writing the data reading the data applying the formats colors and everything
93:54
Speaker A
thing we have some utility file user defined methods so in data driven testing we will try to use that utility file because you have multiple data different test cases in every test case if you want to write this piece of code suppose if this piece of code if you want to write in
94:10
Speaker A
every test case it will be very complicated so that is the reason we will create a separate utility file which contains a user def methods and we will read those methods and do the data driven testing that is act ual real scenario in the tomorrow session I will show you how we can
94:29
Speaker A
create that utility file by using that utility file how we can create data driven test cases so that concept we will see tomorrow session but before that you need to be more familiar with this Concepts how to read data how to write data how to work with the cells and rows from an Excel
94:47
Speaker A
sheet okay clear everyone so tomorrow session we will continue this and how to create utility files I will show you that yes in the DDT data D test case we will have test data in Excel files test cases anyway you will write before itself but test data is always maintained in the Excel
95:11
Speaker A
files and the test data we will read it and use it in your application for our automation testing so most of the times we don't write anything into the Excel because data driven testing is a one- side concept so data we will prepare manually right we don't create data through automation
95:42
Speaker A
we will create data manually so always use the data for data testing so most of the time we will focus only on the Reading part we don't do even writing part okay but very rare cases suppose I already read some data from Excel and use it in our application I have done some testing and the
96:02
Speaker A
results if you want to write back into the same Excel then I can write a results back into the same Excel so in that case writing is coming to picture but that is also very rare case we don't
96:13
Speaker A
write any results into the Excel file the results we will populate in the reports UI reports if any test cases got passed or failed we will populate the results in the report not in the Excel sheet that is very R case but still if you want to write results back into the same Excel sheet
96:29
Speaker A
we can still do that but most of the times we will do only Reading part not on the writing part okay so we don't create anything newly we just use existing one existing data which we created manually in the Excel sheet we will read the data use it for automation that is a real use case
96:54
Speaker A
so what what table you are talking about Shadi scenario where the user need to create a table what is the table you are talking about can you please elaborate what table you are talking about so report generation and all still we have not discussed
97:12
Speaker A
so in test NG we will discuss about report generation we can generate test NG reports we can generate extern reports so that's a different part everybody everybody's understood this concept it's very very most important concept you will get a
97:31
Speaker A
lot of questions also and you need to remember few methods and then definitely ask to write a code how to read data from Excel can you write few statements then you should able to write even though if you do some small mistake that's still fine but the process the procedure you
97:46
Speaker A
have to know so my file. Exel we have just I have just shown you how to write data into the Excel that's it it's not a table or something nothing okay if you want to write the data in Excel how
98:02
Speaker A
we can do that so that I have shown you just just an example okay but most of the times we don't write anything into Excel we all will prepare the test data manually and that data we will read it and use it in your automation testing that is a use case
98:26
Speaker A
okay so just practice this for today's session reading and writing get familiar with this and once you play familiar tomorrow session we will continue with the data driven testing so writing data into Excel multiple examples have shown you writing data into Excel so this m is enough apach
98:47
Speaker A
POI is very huge API we can do lot of things but for automation prospective this is more than enough how to read data how to write data into the Excel and even this is also not much important how to write just if you know how to read that is but tomorrow session I will show
99:03
Speaker A
you how to create a utility file which contains all user defin methods and uh even if you have a 10 to 20 data test cases we will try to use the utility files so that we can minimize our
99:15
Speaker A
effort and time and everything while performing data driven testing so that use case we will see tomorrow session okay yeah so I'll stop here for today's session tomorrow we will continue
Topics:SeleniumJavaData Driven TestingApache POIExcel AutomationMavenSelenium WebDriverTest AutomationExcel Reading WritingQA Automation

Frequently Asked Questions

Why do we need Apache POI for data-driven testing in Selenium?

Selenium WebDriver does not support reading or writing Excel files by default, so Apache POI, a third-party Java library, is used to handle Excel file operations in automation.

How do you add Apache POI to a Maven Selenium project?

You add Apache POI dependencies in the pom.xml file by searching for the required libraries on the Maven repository and including them under the dependencies section.

What types of Microsoft Office files can Apache POI handle?

Apache POI supports multiple Microsoft Office formats including Excel, Word, and PowerPoint, allowing Java applications to read and write these file types.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →