YouTube2Text

Playwright Python 4 | Project Structure for Test Framework — Transcript

by Automation Step by Step · 3,198 words · 433 segments · language en · Watch on YouTube

Full transcript

  1. 0:02Hello and welcome. I am Raghav and in
  2. 0:05this session we are going to create our
  3. 0:07project structure for our automation
  4. 0:10testing framework. This is going to be
  5. 0:12very easy and very interesting. So we
  6. 0:14will create our project structure where
  7. 0:16we will add the required files and
  8. 0:18folders and we will see what is the use
  9. 0:21of each file and folder and then after
  10. 0:23setting up everything in our existing
  11. 0:26project we will add these files folders
  12. 0:28and whatever uh scripts code is needed
  13. 0:31we will add that and then finally we'll
  14. 0:33run and verify that everything is
  15. 0:34working fine. So this will be very
  16. 0:37interesting and I will start from
  17. 0:38scratch. So do not worry if you are a
  18. 0:40complete beginner please do hands on
  19. 0:42along with me and whenever you face any
  20. 0:44issues you can let me know. So let's get
  21. 0:47started and let us see step number one
  22. 0:51is let us check the required libraries
  23. 0:53are already installed. So we are going
  24. 0:56to use our existing project that we have
  25. 0:59been creating in the last session. So
  26. 1:01just in case you are completely new and
  27. 1:03you have not yet created the project you
  28. 1:05can watch the earlier session. I will go
  29. 1:08and open my VS code and this is my
  30. 1:11project already added here. Okay. And
  31. 1:14here uh let me also because we had used
  32. 1:18a Python environment I will activate the
  33. 1:21environment by giving the environment
  34. 1:23name scripts and
  35. 1:25activate. So this is on Windows. I have
  36. 1:27already shown you other commands if
  37. 1:29you're on Mac what you should use. So
  38. 1:31all that we have discussed earlier. So
  39. 1:33now I'm on my environment. Let me also
  40. 1:35expand this terminal. Now I can say pip
  41. 1:39list or I can also say pip freeze to
  42. 1:42check all the libraries that we have
  43. 1:45added. So we have playrite, we have uh
  44. 1:48pi test playright is there and
  45. 1:50everything that we have already done
  46. 1:52until now. That's all fine. We will
  47. 1:54continue from there. You can also use
  48. 1:56pip freeze to see the libraries added.
  49. 2:00Okay. So we already have all these
  50. 2:04libraries added to our project.
  51. 2:07Now on the root project folder we are
  52. 2:12going to create these subfolders. So you
  53. 2:17can do this manually or you can go to
  54. 2:19the command line or terminal and first
  55. 2:22go to your project folder and then using
  56. 2:25the mkdir command we will make these
  57. 2:27folders that is tests pages utils and
  58. 2:33reports. So you can either do it from
  59. 2:36command line using these commands or let
  60. 2:38me just do it manually in my
  61. 2:41project. So this is my
  62. 2:44project. Okay. So I will create a new
  63. 2:47folder using this new folder button and
  64. 2:49I will say we have pages. Then you can
  65. 2:54see the tests folder we already created
  66. 2:57in the last session. So this I do not
  67. 2:59need. I will create another folder
  68. 3:01called
  69. 3:03uh
  70. 3:04utils and I think this is going inside
  71. 3:07test. So I'll have to select my root
  72. 3:10folder and say
  73. 3:13utils. Okay. So this is created. Util is
  74. 3:16there. Tests is there. And then we have
  75. 3:19pages created. Okay. So we
  76. 3:24have tests, pages, utils. Let me also
  77. 3:27create a folder for report. So I will
  78. 3:30again select my root project folder and
  79. 3:33say reports. So that all my reports will
  80. 3:36go inside this. Okay. Now you can
  81. 3:41uh for now let us just continue here. So
  82. 3:44don't worry about anything else. Don't
  83. 3:46worry about all everything that is
  84. 3:48already there. We will fix that. We will
  85. 3:50clean that up. So as of now we have
  86. 3:52created these folders. Now we are going
  87. 3:55to create a file called conf test. py in
  88. 3:59the root folder. So make sure that this
  89. 4:00is this goes in the root folder of the
  90. 4:02project that is just below the project
  91. 4:04folder just inside the project folder.
  92. 4:06And I will tell you in a moment what it
  93. 4:08does and how to use it. So let us first
  94. 4:11create the file. So I'm going to my
  95. 4:13project folder here and create a new
  96. 4:16file. I will call it as conf test.
  97. 4:21py. Okay. And hit enter. And the file is
  98. 4:25created here. Okay. And all these things
  99. 4:28in case you want you can just do it
  100. 4:30manually from the
  101. 4:32uh file explorer. So if you go inside
  102. 4:36your project folder. So this is my
  103. 4:38project. If you want you can also
  104. 4:40manually add these folders like pages,
  105. 4:43tests, reports, utils, all that is okay.
  106. 4:46Okay. Now we have created our confest py
  107. 4:51file. So let us first discuss what this
  108. 4:55file is and how to use it. So let's take
  109. 4:57a scenario. Let's imagine that you have
  110. 4:5910 tests or you are you are testing 10
  111. 5:01different websites and for every test
  112. 5:05you have to do some common things. For
  113. 5:07example, you have to open the browser.
  114. 5:09You have to open a new tab of the
  115. 5:11browser and then you will have to you
  116. 5:14know close the browser, close the tab
  117. 5:16and close the browser at the end. So
  118. 5:18instead of doing this every time again
  119. 5:21and again for each and every test you
  120. 5:23can put this common code inside a file
  121. 5:27called conf test. py and we can actually
  122. 5:30create a fixture. I will show you in a
  123. 5:31moment what is fixture and how to create
  124. 5:33that and then you can use this in your
  125. 5:36test. So instead of repeating the code
  126. 5:39you can add this in conf test. py and we
  127. 5:43can define that once as a fixture and
  128. 5:45then use it in our tests. Okay.
  129. 5:48So here let us see what to do. Let us
  130. 5:52first add this code. Now here you will
  131. 5:54see this is basically two functions
  132. 5:57here. This is one function which is
  133. 6:01called browser. I will keep this code in
  134. 6:04the description or once I upload my
  135. 6:06project on GitHub, I will also keep a
  136. 6:09link to my project. So you can copy from
  137. 6:11there as well. Okay. So this is a
  138. 6:14function called browser. In this
  139. 6:17function, we are opening a new browser.
  140. 6:21We are opening a Chromium browser in a
  141. 6:24headit mode. When we say headless false
  142. 6:26that mean it is headed that is the
  143. 6:28browser will be shown physical browser
  144. 6:30will come up and then we are saying
  145. 6:32yield browser that means we will let
  146. 6:35this to be used by the test and then
  147. 6:37after that we will close the browser.
  148. 6:40Okay. So all this command that is after
  149. 6:42yield browser will be executed after the
  150. 6:44test is over or after the session is
  151. 6:47over. Okay. So this scope is session.
  152. 6:49Okay. Now this is just a function named
  153. 6:53browser. But when we add this annotation
  154. 6:56at piest dofixture at the top of this
  155. 6:59function, this becomes a fixture. And
  156. 7:01now this can be used by any of our
  157. 7:04tests. Okay. Similarly, this is again a
  158. 7:08function called page. Now this page
  159. 7:11function calls the browser browser
  160. 7:14fixture. You can see it is calling the
  161. 7:15browser fixture here. And then after the
  162. 7:18browser is opened here it is opening a
  163. 7:22new tab or a new page in the browser.
  164. 7:25And then when we say yield page it will
  165. 7:27let the test use it and at the end it
  166. 7:30will close the tab. Okay. So in our test
  167. 7:33if we call this this is again a fixture
  168. 7:36because we have added this annotation
  169. 7:37here. So now this becomes a fixture. So
  170. 7:40in our test when we call this page
  171. 7:42fixture it will first call the browser
  172. 7:45fixture which will open the browser with
  173. 7:46all these settings that we have done and
  174. 7:49then it will then open a new page and
  175. 7:51then it will close the page at the end
  176. 7:52of the test and then at the end of the
  177. 7:54session it will close the browser. So
  178. 7:56now we do not have to add all this code
  179. 7:58again and again in every test. We just
  180. 8:00have to call the page fixture which and
  181. 8:02which I will show you in a moment. Okay.
  182. 8:05So first let us just copy this and add
  183. 8:07it to our conf test.
  184. 8:09py. So I will go to my file here and
  185. 8:14paste this thing. Okay. So this is now
  186. 8:16added here. And just in case you find
  187. 8:19anything difficult here or if you're not
  188. 8:22getting anything, you can let me know in
  189. 8:23the comment section or just wait by the
  190. 8:26end of this session. You should get most
  191. 8:28of these things. So we have added
  192. 8:31this code here which is actually two
  193. 8:34fixtures. The page fixture and the
  194. 8:36browser fixture. The page fixture is in
  195. 8:39turn calling the browser fixture also.
  196. 8:41You can check this. So if I hover over
  197. 8:43this browser fixture and
  198. 8:46I I press control on my keyboard, you
  199. 8:50can see this turns into a link and when
  200. 8:52I click this link, you can see it is
  201. 8:54highlighting this browser. Let me again
  202. 8:56show you. So when I click this link by
  203. 8:59after pressing control, it is
  204. 9:00highlighting this. That means it is
  205. 9:02calling this function here or this
  206. 9:04fixture here. Make sure that you save
  207. 9:06your file and project. Okay. Now this
  208. 9:12gives this fixture here. The browser
  209. 9:15fixture starts the browser once per
  210. 9:19session and then yield lets the test use
  211. 9:22the browser and after the tests are done
  212. 9:24it will close the browser as I had I
  213. 9:26have already explained and then the page
  214. 9:28fixture which uses the browser fixture
  215. 9:32and then the browser fixture after it
  216. 9:34opens the browser. This page fixture
  217. 9:36will open a new page or a new tab in the
  218. 9:38browser and then yield page gives the
  219. 9:41test a fresh tab and after the test is
  220. 9:43done it will close the tab or close the
  221. 9:46page. Okay. So in our test we just have
  222. 9:49to call this fixture like this. So you
  223. 9:51can see we are just calling the page
  224. 9:53fixture in our test function. We just
  225. 9:56pass this page fixture and then all
  226. 9:58these things will be taken care and we
  227. 10:00do not have to repeat it for every test
  228. 10:02and then we can continue with our test.
  229. 10:04We can directly then say page.go instead
  230. 10:07of opening the browser, opening the page
  231. 10:09and any other settings you want to do.
  232. 10:10For example, you want to set a timeout
  233. 10:12for your browser or for your page uh or
  234. 10:14whatever you want to do, you can add it
  235. 10:16here instead of adding it in our tests.
  236. 10:19So the our code becomes re reusable. We
  237. 10:22don't have to add it again and again in
  238. 10:24our tests. Our test becomes very clean
  239. 10:26and everything becomes very
  240. 10:28efficient. Okay. So this is done and as
  241. 10:32we have already seen what's a fixture? A
  242. 10:34fixture is a reusable piece of code that
  243. 10:37sets something up before a test runs and
  244. 10:39can clean it up afterwards. So now in
  245. 10:42this contest py whatever you want to add
  246. 10:45maybe at a global level you want to do
  247. 10:47something or you want to do something
  248. 10:49before starting every test after clo
  249. 10:52ending every test before starting the
  250. 10:54browser after you know closing the
  251. 10:56browser or whatever is your requirements
  252. 10:59you can add it in your conf test. py
  253. 11:02file. Okay, so we are done with step
  254. 11:06number four. Step number five is we will
  255. 11:08keep all our test files inside the tests
  256. 11:11folder. So if you have any of the tests
  257. 11:13created like we have created some tests
  258. 11:16earlier, you can keep your tests inside
  259. 11:19the tests folder. Okay. Now in the last
  260. 11:23session we already had created this test
  261. 11:25and which is already inside the tests
  262. 11:27folder. If you want the other tests you
  263. 11:29can also put them. However, in our
  264. 11:32project in the first session, we had not
  265. 11:34added piest and the first test that you
  266. 11:37see here first test. py was not a piest.
  267. 11:40So if you even if you want to leave it
  268. 11:42outside or delete it or just keep it
  269. 11:45somewhere so that it is still available
  270. 11:47you can do that and still uh this also
  271. 11:51test sample. py these two tests were
  272. 11:53there. You can try putting it this
  273. 11:55inside test and then try but I'm not
  274. 11:57sure if it is they are not pi tests they
  275. 12:00we created them before we added the pi
  276. 12:02test plug-in. So let's keep them for now
  277. 12:04as it is and after we test everything we
  278. 12:07can then move these files.
  279. 12:11Okay. The next step is we are now going
  280. 12:13to create a file called
  281. 12:16pyest.ini in the root folder and then
  282. 12:19add the options. So I will tell you what
  283. 12:21it is and what to do with pyest. But for
  284. 12:24now make sure that you add this file in
  285. 12:26the root project folder like we have
  286. 12:28done for
  287. 12:29confest.py. I will create a new file. I
  288. 12:33will call this as
  289. 12:37piest.ini. Okay. And it is created
  290. 12:41here. Okay. Now this piest.ini you can
  291. 12:45see and you can guess what it does.
  292. 12:48These are the command line options that
  293. 12:52we use with our piest command and we
  294. 12:54have seen this in the earlier session.
  295. 12:56So you can see for all these options
  296. 13:00like if you want to run our browser in a
  297. 13:03headit mode the browser we want to run
  298. 13:05the HTML reports. Uh if we have to
  299. 13:09create the reports what is the directory
  300. 13:12or where should we keep the reports?
  301. 13:14Okay. uh test path where should we see
  302. 13:17the tests and then slow motion 200
  303. 13:20milliseconds. So it will run the steps
  304. 13:23in a slow motion or slow down by 200
  305. 13:25milliseconds just so that you can see
  306. 13:27everything running. So whatever is the
  307. 13:29need you can put all these options in
  308. 13:32this file and put piest at the top. That
  309. 13:36means all this will be used with the
  310. 13:38piest command. Okay. So this is the
  311. 13:41configuration file used by piest to
  312. 13:43control how your tests run without
  313. 13:45having to type long command line options
  314. 13:48again and again. So all this will be
  315. 13:50taken from here you just have to run the
  316. 13:53piest command without adding all these
  317. 13:55options and then suppose if you have to
  318. 13:58override something for example instead
  319. 14:00of headed you want headless. So you can
  320. 14:02then say piest and you can add this
  321. 14:05command while running the command and
  322. 14:07that will be overrided. That means this
  323. 14:10will be overrided by whatever you use
  324. 14:12with the command otherwise it will be
  325. 14:14taken from here. Okay. So we just have
  326. 14:17to run piest now and don't worry about
  327. 14:20all these
  328. 14:21options. Okay. So this auto sets headful
  329. 14:25mode chromium browser generates HTML
  330. 14:27report or whatever other options you
  331. 14:29want. You can add them here. So let us
  332. 14:31just take it and put it in our file. So
  333. 14:35I'll copy it
  334. 14:37and paste it
  335. 14:40here and save the file. Okay, make sure
  336. 14:43that you save all your files and
  337. 14:47project. All right, we have done this
  338. 14:49step. Now next step is optional. We can
  339. 14:52create a requirements.txt txt file and
  340. 14:55we can uh put all our packages that we
  341. 14:59are using along with their versions in
  342. 15:01this file so that it will be easier for
  343. 15:03others or for ourselves whenever we have
  344. 15:06to see what are the packages there what
  345. 15:08are what are the the versions added in
  346. 15:11this project we can see that and if
  347. 15:13somebody else wants to use the same
  348. 15:16packages with the same versions they can
  349. 15:19also use this file okay so what we are
  350. 15:22going to do is we are going to run the
  351. 15:24pip freeze command. Now pip freeze
  352. 15:26command will give us all the all our
  353. 15:28packages along with their version. So if
  354. 15:30I show you let me expand this terminal
  355. 15:34window and I say
  356. 15:36pip freeze. So you can see all all that
  357. 15:40which is available in this environment
  358. 15:42that we are using for our project python
  359. 15:44environment is shown here. And then if I
  360. 15:47say pip freeze and use this conical
  361. 15:52bracket that means I want to output the
  362. 15:54result of this command in a file called
  363. 15:58requirements.txt. Okay. And even if it
  364. 16:01is not created it will create the file.
  365. 16:04So this is
  366. 16:07requirements.txt and I hit enter. And
  367. 16:09you will see the file is get got created
  368. 16:11here. Okay. So if
  369. 16:15I see this is the file and everything
  370. 16:20has come here. Okay. Now if somebody
  371. 16:23wants to copy all this and create a new
  372. 16:26project by using the same packages and
  373. 16:28the same version, they can just create a
  374. 16:30Python project and then run this command
  375. 16:34pip install-r
  376. 16:36requirements.txt. So they can just take
  377. 16:38this file from our project or from us
  378. 16:40and then in in their Python project run
  379. 16:42this command pip install- r
  380. 16:45requirements.txt. So it will install the
  381. 16:48same packages with the same version
  382. 16:49which are mentioned in this file. Okay.
  383. 16:52So this is an optional step. If you want
  384. 16:54you can keep this in your project. Okay.
  385. 16:56Now we are done with everything. Make
  386. 16:58sure that you save everything. And let
  387. 17:00us now run
  388. 17:03our project. And now we just have to run
  389. 17:07one command that is pi test and hit
  390. 17:09enter. And this should run with all the
  391. 17:12configurations and settings that we have
  392. 17:13done. So it is going to the test folder
  393. 17:16and running our test as per all
  394. 17:20the settings we have done. Also if you
  395. 17:23see your tests folder and see the test
  396. 17:25that we are using. This is the test we
  397. 17:26created in the last session. And if you
  398. 17:28see it is using the page fixture. Just
  399. 17:31to verify that it is using our fixture.
  400. 17:33You can hover over this page and press
  401. 17:36control on your keyboard. It will turn
  402. 17:38into a link and click it. You can see it
  403. 17:40is taking us to this page fixture. So
  404. 17:43this is using now our fixture and our
  405. 17:46test our command is running with all
  406. 17:49these options that we have added in our
  407. 17:54pyest.ini. All right. So here in our
  408. 17:59project
  409. 18:01structure we have the tests folder where
  410. 18:04all all our test scripts will go. We
  411. 18:07have a pages folder. Now in this session
  412. 18:08we have not added the pages file. We
  413. 18:11will see that in the next session. How
  414. 18:13do we create page object model design
  415. 18:16for our framework by adding all our
  416. 18:19objects in a separate files under pages
  417. 18:21folder. So this we will see our conf
  418. 18:24test. py all our settings browser
  419. 18:27settings fixtures will go here. Pyest
  420. 18:30for all the configuration that we have
  421. 18:32to run from command utils is there. So
  422. 18:35if you add any helpers in future you can
  423. 18:38put them in utils folder and then report
  424. 18:41screenshot etc. It can be autogenerated
  425. 18:43by command or you can keep this folder
  426. 18:46in your project structure. Okay. So this
  427. 18:49is how we can create a project structure
  428. 18:51for our testing framework. I hope this
  429. 18:54was useful. If you face any issues while
  430. 18:56doing hands-on, please let me know and I
  431. 18:59will see you in the next session. Thank
  432. 19:01you for watching and never stop
  433. 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.