Playwright Python 4 | Project Structure for Test Framework — Transcript
Full transcript
- 0:02Hello and welcome. I am Raghav and in
- 0:05this session we are going to create our
- 0:07project structure for our automation
- 0:10testing framework. This is going to be
- 0:12very easy and very interesting. So we
- 0:14will create our project structure where
- 0:16we will add the required files and
- 0:18folders and we will see what is the use
- 0:21of each file and folder and then after
- 0:23setting up everything in our existing
- 0:26project we will add these files folders
- 0:28and whatever uh scripts code is needed
- 0:31we will add that and then finally we'll
- 0:33run and verify that everything is
- 0:34working fine. So this will be very
- 0:37interesting and I will start from
- 0:38scratch. So do not worry if you are a
- 0:40complete beginner please do hands on
- 0:42along with me and whenever you face any
- 0:44issues you can let me know. So let's get
- 0:47started and let us see step number one
- 0:51is let us check the required libraries
- 0:53are already installed. So we are going
- 0:56to use our existing project that we have
- 0:59been creating in the last session. So
- 1:01just in case you are completely new and
- 1:03you have not yet created the project you
- 1:05can watch the earlier session. I will go
- 1:08and open my VS code and this is my
- 1:11project already added here. Okay. And
- 1:14here uh let me also because we had used
- 1:18a Python environment I will activate the
- 1:21environment by giving the environment
- 1:23name scripts and
- 1:25activate. So this is on Windows. I have
- 1:27already shown you other commands if
- 1:29you're on Mac what you should use. So
- 1:31all that we have discussed earlier. So
- 1:33now I'm on my environment. Let me also
- 1:35expand this terminal. Now I can say pip
- 1:39list or I can also say pip freeze to
- 1:42check all the libraries that we have
- 1:45added. So we have playrite, we have uh
- 1:48pi test playright is there and
- 1:50everything that we have already done
- 1:52until now. That's all fine. We will
- 1:54continue from there. You can also use
- 1:56pip freeze to see the libraries added.
- 2:00Okay. So we already have all these
- 2:04libraries added to our project.
- 2:07Now on the root project folder we are
- 2:12going to create these subfolders. So you
- 2:17can do this manually or you can go to
- 2:19the command line or terminal and first
- 2:22go to your project folder and then using
- 2:25the mkdir command we will make these
- 2:27folders that is tests pages utils and
- 2:33reports. So you can either do it from
- 2:36command line using these commands or let
- 2:38me just do it manually in my
- 2:41project. So this is my
- 2:44project. Okay. So I will create a new
- 2:47folder using this new folder button and
- 2:49I will say we have pages. Then you can
- 2:54see the tests folder we already created
- 2:57in the last session. So this I do not
- 2:59need. I will create another folder
- 3:01called
- 3:03uh
- 3:04utils and I think this is going inside
- 3:07test. So I'll have to select my root
- 3:10folder and say
- 3:13utils. Okay. So this is created. Util is
- 3:16there. Tests is there. And then we have
- 3:19pages created. Okay. So we
- 3:24have tests, pages, utils. Let me also
- 3:27create a folder for report. So I will
- 3:30again select my root project folder and
- 3:33say reports. So that all my reports will
- 3:36go inside this. Okay. Now you can
- 3:41uh for now let us just continue here. So
- 3:44don't worry about anything else. Don't
- 3:46worry about all everything that is
- 3:48already there. We will fix that. We will
- 3:50clean that up. So as of now we have
- 3:52created these folders. Now we are going
- 3:55to create a file called conf test. py in
- 3:59the root folder. So make sure that this
- 4:00is this goes in the root folder of the
- 4:02project that is just below the project
- 4:04folder just inside the project folder.
- 4:06And I will tell you in a moment what it
- 4:08does and how to use it. So let us first
- 4:11create the file. So I'm going to my
- 4:13project folder here and create a new
- 4:16file. I will call it as conf test.
- 4:21py. Okay. And hit enter. And the file is
- 4:25created here. Okay. And all these things
- 4:28in case you want you can just do it
- 4:30manually from the
- 4:32uh file explorer. So if you go inside
- 4:36your project folder. So this is my
- 4:38project. If you want you can also
- 4:40manually add these folders like pages,
- 4:43tests, reports, utils, all that is okay.
- 4:46Okay. Now we have created our confest py
- 4:51file. So let us first discuss what this
- 4:55file is and how to use it. So let's take
- 4:57a scenario. Let's imagine that you have
- 4:5910 tests or you are you are testing 10
- 5:01different websites and for every test
- 5:05you have to do some common things. For
- 5:07example, you have to open the browser.
- 5:09You have to open a new tab of the
- 5:11browser and then you will have to you
- 5:14know close the browser, close the tab
- 5:16and close the browser at the end. So
- 5:18instead of doing this every time again
- 5:21and again for each and every test you
- 5:23can put this common code inside a file
- 5:27called conf test. py and we can actually
- 5:30create a fixture. I will show you in a
- 5:31moment what is fixture and how to create
- 5:33that and then you can use this in your
- 5:36test. So instead of repeating the code
- 5:39you can add this in conf test. py and we
- 5:43can define that once as a fixture and
- 5:45then use it in our tests. Okay.
- 5:48So here let us see what to do. Let us
- 5:52first add this code. Now here you will
- 5:54see this is basically two functions
- 5:57here. This is one function which is
- 6:01called browser. I will keep this code in
- 6:04the description or once I upload my
- 6:06project on GitHub, I will also keep a
- 6:09link to my project. So you can copy from
- 6:11there as well. Okay. So this is a
- 6:14function called browser. In this
- 6:17function, we are opening a new browser.
- 6:21We are opening a Chromium browser in a
- 6:24headit mode. When we say headless false
- 6:26that mean it is headed that is the
- 6:28browser will be shown physical browser
- 6:30will come up and then we are saying
- 6:32yield browser that means we will let
- 6:35this to be used by the test and then
- 6:37after that we will close the browser.
- 6:40Okay. So all this command that is after
- 6:42yield browser will be executed after the
- 6:44test is over or after the session is
- 6:47over. Okay. So this scope is session.
- 6:49Okay. Now this is just a function named
- 6:53browser. But when we add this annotation
- 6:56at piest dofixture at the top of this
- 6:59function, this becomes a fixture. And
- 7:01now this can be used by any of our
- 7:04tests. Okay. Similarly, this is again a
- 7:08function called page. Now this page
- 7:11function calls the browser browser
- 7:14fixture. You can see it is calling the
- 7:15browser fixture here. And then after the
- 7:18browser is opened here it is opening a
- 7:22new tab or a new page in the browser.
- 7:25And then when we say yield page it will
- 7:27let the test use it and at the end it
- 7:30will close the tab. Okay. So in our test
- 7:33if we call this this is again a fixture
- 7:36because we have added this annotation
- 7:37here. So now this becomes a fixture. So
- 7:40in our test when we call this page
- 7:42fixture it will first call the browser
- 7:45fixture which will open the browser with
- 7:46all these settings that we have done and
- 7:49then it will then open a new page and
- 7:51then it will close the page at the end
- 7:52of the test and then at the end of the
- 7:54session it will close the browser. So
- 7:56now we do not have to add all this code
- 7:58again and again in every test. We just
- 8:00have to call the page fixture which and
- 8:02which I will show you in a moment. Okay.
- 8:05So first let us just copy this and add
- 8:07it to our conf test.
- 8:09py. So I will go to my file here and
- 8:14paste this thing. Okay. So this is now
- 8:16added here. And just in case you find
- 8:19anything difficult here or if you're not
- 8:22getting anything, you can let me know in
- 8:23the comment section or just wait by the
- 8:26end of this session. You should get most
- 8:28of these things. So we have added
- 8:31this code here which is actually two
- 8:34fixtures. The page fixture and the
- 8:36browser fixture. The page fixture is in
- 8:39turn calling the browser fixture also.
- 8:41You can check this. So if I hover over
- 8:43this browser fixture and
- 8:46I I press control on my keyboard, you
- 8:50can see this turns into a link and when
- 8:52I click this link, you can see it is
- 8:54highlighting this browser. Let me again
- 8:56show you. So when I click this link by
- 8:59after pressing control, it is
- 9:00highlighting this. That means it is
- 9:02calling this function here or this
- 9:04fixture here. Make sure that you save
- 9:06your file and project. Okay. Now this
- 9:12gives this fixture here. The browser
- 9:15fixture starts the browser once per
- 9:19session and then yield lets the test use
- 9:22the browser and after the tests are done
- 9:24it will close the browser as I had I
- 9:26have already explained and then the page
- 9:28fixture which uses the browser fixture
- 9:32and then the browser fixture after it
- 9:34opens the browser. This page fixture
- 9:36will open a new page or a new tab in the
- 9:38browser and then yield page gives the
- 9:41test a fresh tab and after the test is
- 9:43done it will close the tab or close the
- 9:46page. Okay. So in our test we just have
- 9:49to call this fixture like this. So you
- 9:51can see we are just calling the page
- 9:53fixture in our test function. We just
- 9:56pass this page fixture and then all
- 9:58these things will be taken care and we
- 10:00do not have to repeat it for every test
- 10:02and then we can continue with our test.
- 10:04We can directly then say page.go instead
- 10:07of opening the browser, opening the page
- 10:09and any other settings you want to do.
- 10:10For example, you want to set a timeout
- 10:12for your browser or for your page uh or
- 10:14whatever you want to do, you can add it
- 10:16here instead of adding it in our tests.
- 10:19So the our code becomes re reusable. We
- 10:22don't have to add it again and again in
- 10:24our tests. Our test becomes very clean
- 10:26and everything becomes very
- 10:28efficient. Okay. So this is done and as
- 10:32we have already seen what's a fixture? A
- 10:34fixture is a reusable piece of code that
- 10:37sets something up before a test runs and
- 10:39can clean it up afterwards. So now in
- 10:42this contest py whatever you want to add
- 10:45maybe at a global level you want to do
- 10:47something or you want to do something
- 10:49before starting every test after clo
- 10:52ending every test before starting the
- 10:54browser after you know closing the
- 10:56browser or whatever is your requirements
- 10:59you can add it in your conf test. py
- 11:02file. Okay, so we are done with step
- 11:06number four. Step number five is we will
- 11:08keep all our test files inside the tests
- 11:11folder. So if you have any of the tests
- 11:13created like we have created some tests
- 11:16earlier, you can keep your tests inside
- 11:19the tests folder. Okay. Now in the last
- 11:23session we already had created this test
- 11:25and which is already inside the tests
- 11:27folder. If you want the other tests you
- 11:29can also put them. However, in our
- 11:32project in the first session, we had not
- 11:34added piest and the first test that you
- 11:37see here first test. py was not a piest.
- 11:40So if you even if you want to leave it
- 11:42outside or delete it or just keep it
- 11:45somewhere so that it is still available
- 11:47you can do that and still uh this also
- 11:51test sample. py these two tests were
- 11:53there. You can try putting it this
- 11:55inside test and then try but I'm not
- 11:57sure if it is they are not pi tests they
- 12:00we created them before we added the pi
- 12:02test plug-in. So let's keep them for now
- 12:04as it is and after we test everything we
- 12:07can then move these files.
- 12:11Okay. The next step is we are now going
- 12:13to create a file called
- 12:16pyest.ini in the root folder and then
- 12:19add the options. So I will tell you what
- 12:21it is and what to do with pyest. But for
- 12:24now make sure that you add this file in
- 12:26the root project folder like we have
- 12:28done for
- 12:29confest.py. I will create a new file. I
- 12:33will call this as
- 12:37piest.ini. Okay. And it is created
- 12:41here. Okay. Now this piest.ini you can
- 12:45see and you can guess what it does.
- 12:48These are the command line options that
- 12:52we use with our piest command and we
- 12:54have seen this in the earlier session.
- 12:56So you can see for all these options
- 13:00like if you want to run our browser in a
- 13:03headit mode the browser we want to run
- 13:05the HTML reports. Uh if we have to
- 13:09create the reports what is the directory
- 13:12or where should we keep the reports?
- 13:14Okay. uh test path where should we see
- 13:17the tests and then slow motion 200
- 13:20milliseconds. So it will run the steps
- 13:23in a slow motion or slow down by 200
- 13:25milliseconds just so that you can see
- 13:27everything running. So whatever is the
- 13:29need you can put all these options in
- 13:32this file and put piest at the top. That
- 13:36means all this will be used with the
- 13:38piest command. Okay. So this is the
- 13:41configuration file used by piest to
- 13:43control how your tests run without
- 13:45having to type long command line options
- 13:48again and again. So all this will be
- 13:50taken from here you just have to run the
- 13:53piest command without adding all these
- 13:55options and then suppose if you have to
- 13:58override something for example instead
- 14:00of headed you want headless. So you can
- 14:02then say piest and you can add this
- 14:05command while running the command and
- 14:07that will be overrided. That means this
- 14:10will be overrided by whatever you use
- 14:12with the command otherwise it will be
- 14:14taken from here. Okay. So we just have
- 14:17to run piest now and don't worry about
- 14:20all these
- 14:21options. Okay. So this auto sets headful
- 14:25mode chromium browser generates HTML
- 14:27report or whatever other options you
- 14:29want. You can add them here. So let us
- 14:31just take it and put it in our file. So
- 14:35I'll copy it
- 14:37and paste it
- 14:40here and save the file. Okay, make sure
- 14:43that you save all your files and
- 14:47project. All right, we have done this
- 14:49step. Now next step is optional. We can
- 14:52create a requirements.txt txt file and
- 14:55we can uh put all our packages that we
- 14:59are using along with their versions in
- 15:01this file so that it will be easier for
- 15:03others or for ourselves whenever we have
- 15:06to see what are the packages there what
- 15:08are what are the the versions added in
- 15:11this project we can see that and if
- 15:13somebody else wants to use the same
- 15:16packages with the same versions they can
- 15:19also use this file okay so what we are
- 15:22going to do is we are going to run the
- 15:24pip freeze command. Now pip freeze
- 15:26command will give us all the all our
- 15:28packages along with their version. So if
- 15:30I show you let me expand this terminal
- 15:34window and I say
- 15:36pip freeze. So you can see all all that
- 15:40which is available in this environment
- 15:42that we are using for our project python
- 15:44environment is shown here. And then if I
- 15:47say pip freeze and use this conical
- 15:52bracket that means I want to output the
- 15:54result of this command in a file called
- 15:58requirements.txt. Okay. And even if it
- 16:01is not created it will create the file.
- 16:04So this is
- 16:07requirements.txt and I hit enter. And
- 16:09you will see the file is get got created
- 16:11here. Okay. So if
- 16:15I see this is the file and everything
- 16:20has come here. Okay. Now if somebody
- 16:23wants to copy all this and create a new
- 16:26project by using the same packages and
- 16:28the same version, they can just create a
- 16:30Python project and then run this command
- 16:34pip install-r
- 16:36requirements.txt. So they can just take
- 16:38this file from our project or from us
- 16:40and then in in their Python project run
- 16:42this command pip install- r
- 16:45requirements.txt. So it will install the
- 16:48same packages with the same version
- 16:49which are mentioned in this file. Okay.
- 16:52So this is an optional step. If you want
- 16:54you can keep this in your project. Okay.
- 16:56Now we are done with everything. Make
- 16:58sure that you save everything. And let
- 17:00us now run
- 17:03our project. And now we just have to run
- 17:07one command that is pi test and hit
- 17:09enter. And this should run with all the
- 17:12configurations and settings that we have
- 17:13done. So it is going to the test folder
- 17:16and running our test as per all
- 17:20the settings we have done. Also if you
- 17:23see your tests folder and see the test
- 17:25that we are using. This is the test we
- 17:26created in the last session. And if you
- 17:28see it is using the page fixture. Just
- 17:31to verify that it is using our fixture.
- 17:33You can hover over this page and press
- 17:36control on your keyboard. It will turn
- 17:38into a link and click it. You can see it
- 17:40is taking us to this page fixture. So
- 17:43this is using now our fixture and our
- 17:46test our command is running with all
- 17:49these options that we have added in our
- 17:54pyest.ini. All right. So here in our
- 17:59project
- 18:01structure we have the tests folder where
- 18:04all all our test scripts will go. We
- 18:07have a pages folder. Now in this session
- 18:08we have not added the pages file. We
- 18:11will see that in the next session. How
- 18:13do we create page object model design
- 18:16for our framework by adding all our
- 18:19objects in a separate files under pages
- 18:21folder. So this we will see our conf
- 18:24test. py all our settings browser
- 18:27settings fixtures will go here. Pyest
- 18:30for all the configuration that we have
- 18:32to run from command utils is there. So
- 18:35if you add any helpers in future you can
- 18:38put them in utils folder and then report
- 18:41screenshot etc. It can be autogenerated
- 18:43by command or you can keep this folder
- 18:46in your project structure. Okay. So this
- 18:49is how we can create a project structure
- 18:51for our testing framework. I hope this
- 18:54was useful. If you face any issues while
- 18:56doing hands-on, please let me know and I
- 18:59will see you in the next session. Thank
- 19:01you for watching and never stop
- 19:03learning.
About this transcript
This page contains the full transcript of Playwright Python 4 | Project Structure for Test Framework by Automation Step by Step, generated from the public captions YouTube serves with the video. The transcript has 3,198 words across 433 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.