Playwright with Javascript | How To Create and Run Tests | Part 3 — Transcript
Full transcript
- 0:01hi everyone welcome you all so in the today's video I'm going to show you how
- 0:06we can create a flavorite test and also we'll see how we can run Playboy test
- 0:11so the first thing is uh in playwright we can create tests in two different ways and the first
- 0:17approach is by using Code gen plugin which is provided by playwright and which is also called
- 0:22test generator and we can automatically record all the steps and the second approach is by writing
- 0:29our own script so these are the two different approaches by using Code Generator without
- 0:34using Code Generator so we have two approaches we have in a playwright to generate a code so first
- 0:40thing we will see how can we write our own script without using code generator and how can run the
- 0:46test okay so that we are going to see in the video so for that what we need to do is let us go to the
- 0:52vs code editor and we already created this project in the previous video so playwright automation
- 0:58project and we also installed we already installed playwright on this particular project and this
- 1:05is a folder structure so which we can this folder contains all the node modules whatever is required
- 1:10for playwright test all the modules are created and uh you can see test test is a folder so in
- 1:17this the sample test is got created so this is a demo for demo purpose they have given example spec
- 1:23dot j so this simple test they have given and even in test examples also they have given one more
- 1:29test which is basically when Advanced test they have given for example so we will write our own
- 1:34test in the playwright so for that what we need to do is inside the test folder so all the tests we
- 1:40have to create inside the test folder so the first thing is we need to create our own test so for
- 1:46that we need to create a new file so right click on the test or you can select the test and you can
- 1:51click on this icon a new file and we are going to create a new file so all the test files should be
- 1:57having extension is packed.js the file name can be anything but the extension of the file should
- 2:03be speaker.js okay so here I'm creating my first test so my test is I want to verify the home page
- 2:11let's say I will launch my application and then I will verify uh something on the web page like
- 2:16title is correct or not the URL is correct or not I can verify so simply I'm creating one a
- 2:21new test uh my test name is home page desk dot uh spec dot Js so this is how we need to create
- 2:30a new file under test folder so homepage test dot spec.js in this I am going to create some test
- 2:37so before that let me show you the application what I am going to automate
- 2:42fine so if you go to this application this is demoblast.com so this is a home page
- 2:47now as soon as you launch my application on the browser I want to verify uh the title of
- 2:53the pages store or not you can see this is the title of the page and even if you right click
- 2:58on the page inspect and you can see the title on the header tag so here you can see the title
- 3:04so I want to verify the title is exactly the title is given correctly or not and the second thing is
- 3:09I want to verify this URL is also correctly opened or not so these are the two tests I want to do as
- 3:15part of my file so for that what you need to do is as soon as you created a new file the first
- 3:20thing is we need to create or we need to import required modules so for our testing okay so in
- 3:29the if I look at the project here so there is a node modules are automatically created
- 3:33so in this you can say at the rate play right inside this there are so many modules you can see
- 3:40so we need to import this playwright test module in our file and then we will able to access all
- 3:46the functions and features and so on so for that we need to First import it to import we have to
- 3:52use one function called require and this is comes from node.js and here we need to specify the which
- 3:58model you want to import so here the model is what the model name is at the rate playwright
- 4:04slash test so this particular module we have to import so in this module again there are so many
- 4:12uh packages are there if I look at here playwright slash test so there are so many packages are there
- 4:18in this so again in this package we don't need all the packages we need only two packages and they
- 4:25are expect and test so for that what we need to do is we need to uh in the curly braces we need to
- 4:34write so what are the packages we have to import a test is one package we required for writing
- 4:38the test play right test other one is to add validations we need one more package called expect
- 4:43so these are the two things we need to get from this require so here you can simply say equal to
- 4:50okay and this test and expert should be constant you can use a lit or you can use where or you can
- 4:57use constant some variable is required so what exactly we are doing in the first step so we
- 5:03are importing the model playwright slash list from this particular module we we have to get
- 5:08test and expert so these are the two packages we required to create our playwright test so test is
- 5:15required to create our test expert is required to add validations for the test and those two
- 5:21are available inside at the rate play right slash test okay and once imported this now we can start
- 5:29writing our test so how to start writing our test so to start writing our test we need to
- 5:35use something called test block so we need to write something called test block so in this
- 5:41we need to write some Anonymous function and two parameters we need to pass so first parameter is
- 5:46what the title of the test so you can provide some name to the test so here I am providing something
- 5:52called let's say home page and here and the second parameter comma second value should be anonymous
- 5:59function we need to pass so Anonymous function in the sense Open Bracket and arrow function so
- 6:05this is called Anonymous function so this is the block first we need to create whenever you want
- 6:10to create a test in the playwriter okay and one more important thing so when you create this block
- 6:16when you create this block so this particular Anonymous function so whatever Anonymous function
- 6:22we created so this Anonymous function will use some fixture which is provided in playwright
- 6:29so in the playwright whenever you're automating any application the pages will be represented with
- 6:36the picture that is called page so this picture this picture we have to pass inside the anonymous
- 6:42function and this we need to keep inside the curly braces okay this is the mandatory so whenever you
- 6:48create Anonymous function we have to pass a fixture called page so what is this picture
- 6:53means which contains so many functions and through them we can automate web application suppose when
- 7:00you interactive with some element or if you want to click on element or if you want to get the
- 7:04text from the page or if you want to check some element is present on the page or not or if you
- 7:08want to launch your web page so all these commands are available in page that is a fixture which is
- 7:15available in playwriter okay so this is mandatory required that is one thing and inside the function
- 7:21so now we pass this page right so inside the test function and the second thing is what
- 7:29by using this page picture we have to access all the commands
- 7:34right suppose I want to launch my application in the browser so for that what you can do is
- 7:38take this page dot so in this page picture we can access all the methods so for now I'm using one
- 7:47method called go to and this method will be used to open application URL on the browser and here
- 7:54we need to pass the application URL so I'm just passing the URL of application in the go to method
- 8:01okay go to Method so this particular command will open this URL on the browser okay this particular
- 8:09command will open the URL on the browser and one more important thing we need to understand
- 8:15so whenever you are referring this particular feature page right so in front of this in front
- 8:21of this page or in front of this function we need to specify something called async
- 8:29okay async should be specified async and before the bracket before the function start I think we
- 8:36have to specify similarly whenever you are accessing a method from the page picture
- 8:42we have to specify something called a weight so this is magically required
- 8:48okay so whenever you are writing the test Block in the anonymous function we have to pass
- 8:53page picture in the curly brace and before that function we need to write one keyword called async
- 8:59and similarly whenever you are accessing a methods from the page we have to specify
- 9:04something called away so these two keywords will keep on continue throughout our automation test
- 9:11but why this we have to specify async and a beta so basically JavaScript is asynchronized
- 9:18programming language JavaScript is a synchronized programming language so what does it mean
- 9:24suppose we have written some steps in your automation let's say I have written 10 steps
- 9:29so normally how execution will happen so line by line is Step number one step number two step
- 9:34number three and so on but in asynchronous programming languages what happens is
- 9:39so the execution will happen asynchronously that means it will not go execution step by
- 9:44step that means if you want x equal to one step right so suppose that step is based on the or
- 9:50depends on the previous step right so normally in asynchronous programming languages what happens
- 9:56the steps will be executed asynchronously it will not depend on the previous step
- 10:02so that is the behavior of asynchronous programming languages okay so normally what
- 10:08happens is you have multiple lines of code like this so if you want to execute this particular
- 10:12step the first step should be completed and then you need to go with the next step and this should
- 10:17be completed and then you need to go with the next step right but in JavaScript what happens
- 10:21is these steps will executed asynchronously that means parallely all the steps will be executed so
- 10:27no step is depends on any other step but in our automation first we need to x equal to one step
- 10:34and then only we can continue with the next step because every step is depends on the previous step
- 10:39okay to make it happen we have to handle something called Promise JavaScript promise
- 10:47so what is Javascript promise so here uh a single keyword we have used what does that mean async
- 10:54keyword says uh this is we have to use before the function right it makes the function written
- 11:01a promise so this async keyword will make the function written a promise and await keyword avoid
- 11:08keyword makes the function wait for the promise so what does it mean the async keyword will make
- 11:15the function written a promise and Avid keyword will makes the function wait for the promise what
- 11:20does it mean this particular statement page dot go to statement will wait till the page is loaded
- 11:28so once the page is loaded then only the next step will be continued that means what a single
- 11:33keyword will make sure the function will return the promise and await keyword will make sure
- 11:39it will wait for the promise so once the prom is completed that means once the page is loaded then
- 11:45page dot go to command works okay so every command in the page will work after loading the page
- 11:53so we are going to handle this Promise by using async and avoid keyword so for that reason
- 11:58whenever you are using this page before that as Anonymous function you have to specify async
- 12:04keyword and whenever you're accessing the commands or method from the page you have to specify await
- 12:10in front of this function okay these things will keep continue in your play rate automation
- 12:16test scripts because JavaScript is a synchronized language so to make it synchronized we have to use
- 12:23these two keywords okay so now so by using this command page dot go to so which will launch our
- 12:30application on the page Now what is my test here I want to do two different tests so one is I want
- 12:36to verify the title of the webpage and also I want to verify the URL of the webpage is correct or not
- 12:41okay so how to capture the title of the webpage so to capture the web page title so again we have
- 12:49one method in the page object we can say page dot title simple page dot title so this function or
- 12:55this method will return the title of the web page I can store in a variable I can say page title
- 13:02and the variable type should be you can specify where or you can specify late or
- 13:07you can specify cons whatever you want in the JavaScript these are the three different ways
- 13:12we can Define the variable so constant means what that value is not changing the value is constant
- 13:18so if you want to capture the title of the webpage we can use page dot title so that will
- 13:23return the title of the web page and if you want to print the title simply you can use console.log
- 13:32console.log and here simple I can print page title is and here
- 13:42concatenate with or you can say simple comma and page title okay so like this we can capture
- 13:50the title but how we can validate whether the title is correct or not so we need to add some
- 13:56validation Point by using expected so far we have use only test here so now we need to use
- 14:01expect to add some validation so this is not mandatory to capture the page title because we
- 14:06want to do validation just not printing the title right so to apply the validation we have to use
- 14:12something called expector so expect is a function inside this we need to pass page and that page
- 14:20uh URL we have to validate right so for that we have another function called to have URL
- 14:28and what is the URL we are expecting or what is the title you if you are expecting if you want to
- 14:32verify the title Dot uh to have title similarly if you want to verify the URL to have URL so multiple
- 14:43methods are available in the page that we can access so to have title and in this we can specify
- 14:50what is a title we are expecting so my title I am expecting is called store so this is my title I
- 14:56am going to expect so this is exactly validation point so what exactly we have done we have just
- 15:01launched our web web application and capture the title and just we have printed the title
- 15:06this is optional this is mandatory not required just if you want to know the title you can just
- 15:11capture and print but this is the validation Point expect the page to have title store
- 15:19okay and after that if you want to close my page you can say page Dot close you can just access you
- 15:26can just call close function from the page page Dot close so all the functions all the methods
- 15:33are available in the page itself okay so this is simple script we can create like this okay
- 15:39and one important I told you at the beginning so whenever you're referring this page and the
- 15:45anonymous function we need to use async so that it will make sure this will return the promise
- 15:50and avoid is a keyword which will make sure it is waiting for the promise so whenever you are
- 15:57referring the methods or functions from the page you have to use a weight keyword so here also it
- 16:04is applicable for expect also it is applicable and here the page is also it is applicable so in every
- 16:10statement this event is applicable so that it will make sure that particular function is waiting for
- 16:16the promise to complete okay so this is how simply we can create playwrite test so now how we can so
- 16:25we have verified only title right so if you want to verify the URL you can still verify the URL so
- 16:30to verify the URL you can say expect and page Dot to have so to have URL and that URL we can specify
- 16:44in the double quotation so whatever we have we send this URL the same URL I'm expecting here
- 16:51and another thing so whenever again you are using expert page we have to use something called await
- 16:59right so now we have done two validations as soon as you launch your web page we are just
- 17:04verifying the title and we are verifying the URL of the current page and finally we are closing
- 17:09the browser so in every statement if I look at here avoid keyword should be used before the
- 17:14function it will make sure the promise it will wait for the promise to complete means it will
- 17:20wait for the page to complete further actions okay and again here we are directly verifying
- 17:26the current URL of the web page but if you want to capture and if you want to print in the console so
- 17:32just like a page title we captured right similar if you want to capture the URL from the web page
- 17:36simply from the page you can capture it you can say page Dot URL okay so simply you can say page
- 17:45URL page dot URL simply small case character so please URL so this will return the URL of
- 17:51the page so I can say page URL I'm storing in the variable called page URL and this is
- 17:58variable okay so if you want to just print this page URL you can write one more console.log
- 18:06console.log and here you can specify the page URL page URL
- 18:17page URL is comma page value so this is a variable in the JavaScript okay so simple
- 18:25script you have created what we have done is we have just open application URL capture the
- 18:30title and print it in the console window and then I have done validation on the
- 18:33title and then we captured the URL so this is not page transition it is simple page
- 18:40page dot URL and which will return the page URL and then we are just printing it and after that
- 18:46we are validating the page URL and finally we are print closing the page so you need to make sure
- 18:52before writing the anonymous function we need to use a sync and before accessing all the methods
- 18:57from the page we have to use await okay so now let us try to execute this particular code so to
- 19:05execute this playwright test what we can do is we need to just go to terminal let me save the file
- 19:11and open the terminal new terminal in the new terminal we have to execute one command called npx
- 19:18playwrite and test so when execute this command npx playwright test by default
- 19:23this will execute all the tests which are present inside the test folder So currently
- 19:28I have another test also example.spec.js right so both the tests will be executed so let me
- 19:34try to execute here when I press enter so by default all the tests will be executed
- 19:40say this it is basically executed in the headed mode so by a headless mode by default playwright
- 19:46will execute all the tests in the Headless mode so what you're saying total in nine passed so 9 means
- 19:51one test we have created our own and register of the a tests are part of the uh previous one
- 19:58example dot spec dot JS and also it is executed in multiple browsers parallel it is executed so
- 20:04totally nine okay so if you want to see the report you can see the report uh here so by default
- 20:10report will be generated if you want to see the report you can just execute this command it will
- 20:14automatically show up the reports I can say npx playwright show iPhone report so when you execute
- 20:25this command this will automatically opens the report now you can see this is the report which is
- 20:29created so all the tests are executed in all the browsers you can see example.spec.js you can see
- 20:36so these are the tests they are already there in the example spec.js and they are executed in the
- 20:41chromium Firefox and webkit and similarly homepage test dot spec.js this we have created and home
- 20:47page test is executed on three different browsers chromium Firefox and webkit okay so this is how
- 20:53we can simply create a test and we can execute so by default so this particular command S7 is
- 20:59a control C it will exit from the command so when execute this command npx playwright show report so
- 21:05this will give you the report and when execute npx playwright test this will automatically
- 21:10execute all the tests which are part of this test folder right so but I want to execute my own test
- 21:17I don't want execute all the tests I just want execute only homepage test dot speaker.js so
- 21:22simply what you can do is you can specify the name npx playwright test and here you can specify the
- 21:28name of the test whichever you want to execute home page test dot spec dot Js homepage test dot
- 21:38spec dot JS so this time it will execute only the test which we have created so now we have create
- 21:45it is executing one test but on three browsers it is executing if I look at here so three got
- 21:51passed so what is 3 because we have written only one test right but how come it is three executed
- 21:56means same test is executed on three different browsers chromium webkit and Firefox right so
- 22:02if you want to look at the report once again you can just execute this command npx playwright show
- 22:09report and that will give you the report now we can see the same test is got executed on chromium
- 22:14Firefox and webkit okay and suppose I don't want execute on all browsers I want execute only on
- 22:22the Chromium browser or I want the execute only on the Firefox or webkit so how it is possible
- 22:28so to execute the test on the specific browser we need to slightly customize the command so Ctrl C
- 22:34now just observe here npx play rate test and home pasters dot spec dot j so now here by using this
- 22:43command I'm executing my test on three browsers by default it will execute on three browsers
- 22:48parallely but I want to execute the test only on the Chromium browser only one browser I want to
- 22:55execute okay so then what we can do is we need to add one parameter here home page test spec.js and
- 23:02here if an iPhone project iPhone project equal to chromium hyphen hyphen project equal to chromium
- 23:13so when you specify this hyphen iPhone project equal to chromium so my test will be executed
- 23:19only on Chromium browser so now press enter so this is executing on only Chromium browser now
- 23:27you can see one time only it is executed one test is executed only one browser right so if you want
- 23:34to see the report once again you can say npx playwright npx playwright show iPhone report
- 23:43so now we can see one test is executed only on one browser that is Chromium browser
- 23:49okay so like this we can execute the test in whichever browser you want just by specifying
- 23:56the command so let me just put all the commands here so to run all the tests which are there in
- 24:02the test folder the command is what nbx playwrite test so this will execute all the tests on all
- 24:10browsers by default in headless mode by default in headless mode so this is a command Okay so test
- 24:19so it is just a test it's not test so npx playwright tested this particular command runs all
- 24:26the tests on all browsers in Endless mode and the same command npx playwright test if you want to
- 24:36execute specific test case here we have to specify the test case name let us say my test dot spec dot
- 24:42Js so this command will run specific test case File so whichever you created under test folder
- 24:49only that will execute so you need to just specify the name of the test so npx playwright test
- 24:56and specify the name of the test and this will exactly runs the specific file this is one command
- 25:04and if you want to execute the test on specific browser you need to write this command npx
- 25:09playwright test and which test you want execute I want to execute mytest.spec.js and which browser
- 25:16you want to execute you can specify that option iPhone project equal to chromium or project
- 25:23equal to Firefox or project equal to webkit you can specify whatever you want so this command
- 25:28runs on runs the test on specific browser so these are the three options so far we have
- 25:37seen so now we want to execute our test in headed mode so by default all the tests will be executed
- 25:44on the Headless mode so we are not able to see the UI right so if you want to execute our test
- 25:49in headed mode so then what you can do is we can exude the same command so npx playwright
- 25:58test npx playwright test and if you want to execute the test in headless
- 26:05mode headed mode and which test you want to execute I want execute my home page test
- 26:11so I want to execute my home page test so just specify that name home page home page
- 26:20test dot spec dot JS so this particular file I'll execute if I execute this command so by default
- 26:28all three browsers will be executed same test will be executed on three browsers so three
- 26:34or got pass now I will just try to customize this command C hubs are here npx play rate test
- 26:40I want to execute this particular test on specific browser on which browser you want to execute
- 26:44I want execute Chromium browser so I can say iPhone iPhone project equal to chromium and also
- 26:52I want to execute this test in headed mode so by default it is executed in headless mode we can't
- 26:57see any UI but I want to execute the same test on headed mode so you can say iPhone iPhone and data
- 27:04this is another option which we have to provide hyphen hyphen hidden so this command will execute
- 27:11my test on Chromium browser one single browser in headed model okay now when I execute this enter
- 27:20now You observe it is launching my Chromium browser and it is done validation and one test
- 27:25is got passed so this is how we can achieve the headed mode execution so we have seen how to run
- 27:33all the tests in the test folder by default if I give this command so this will run all the tests
- 27:39which are available in the folder and the next command which we have seen if you want to run
- 27:44specific file we can just specify the file name and that will execute on three browsers again and
- 27:50again if you want to execute the test on specific browser you can just specify iPhone iPhone project
- 27:55equal to chromium this will run test on the particular browser and again one more command
- 28:00we have learned so npx playwright test so I want execute mytest.spec dot file so this file I want
- 28:09to execute spec dot JS file on particular Chrome browser so Chromium browser I say iPhone iPhone
- 28:16project equal to chromium or one single test I want to execute on Chromium browser in headed
- 28:25mode so I can have hyphen iPhone head data so this option will make our test header model
- 28:31okay and one more option suppose if you want to run your test in the debug mode if you want
- 28:36to run each and every step one by one step by step execution if you want to do then you
- 28:41can run the test in the debug model so for that what you need to do is you need to just Ctrl C
- 28:48exit and the same command npx playwright test homepage.spec.js so this is the file I'm going
- 28:56to execute on the Chromium browser in headed mode and in the debug mode also I want to add so that I
- 29:04can see each and every step so if you want to run this in the debug mode you can say space iPhone
- 29:09debug iPhone iPhone debug so when I execute this enter now you can observe here as soon as you run
- 29:18your test by default the browser just open it gets top here and also it will automatically open one
- 29:25more new window called playwright inspector okay playwright inspector here you are able to see same
- 29:32script whatever you have created and it is get stopped at line number five here it gets stopped
- 29:38you can just notice here so it is just launch my browser and it is still not open any application
- 29:45it is waiting at the line number five so from here we can go step by step execution so to go step by
- 29:51step execution what we can do is we can simply use this option so this is for running at the entire
- 29:56code this is for pausing and this is for executing the next step step over so when you press this key
- 30:03or press F10 your functional key it will go to the next step now observe here what is happening when
- 30:08you click on this now you can see line number five is got executed now if you go to the next step it
- 30:13has got printed the title also and then it go to the next step and again click on this button then
- 30:19it go to next step and then close and then one more step finally the close so here we got the
- 30:26uh title of the web page and URL of the web page in the console window so one test is successful
- 30:33execute this is the title is not properly printed you can see and URL is got printed so
- 30:39why title is not printed here because we haven't specified let's put a weight here okay have it
- 30:48fine so now let us save the file and then execute one more time
- 30:56with this command and takes private test homepage test Dot spec.js and on the Chromium browser I
- 31:03want to execute in the headed mode and also the debug mode right now again press enter now again
- 31:08it turn into debug mode of execution now again you will get this window play rate inspector
- 31:13along with this browser now we need to go to step by step execution just click on this button so
- 31:19reload button so you can just click on this this is basically a step over button so you can just
- 31:25go here title and then URL and the finally closed so now we can see you got the page title and also
- 31:32you've got the URL fine so this is how we can simply write your playwright test and we can
- 31:39execute in multiple ways so if you want to execute in the debug mode what we can do is same command
- 31:45index playwright test and I want execute and you can customize these options according to your
- 31:50requirement okay NPS playwright test and the same test file I want execute this is spec file and
- 31:57Dot JS and same Chromium browser I want execute header mode in the debug mode hyphen iPhone debug
- 32:04so these are all customized options okay you can provide the option whichever uh based on your
- 32:09requirement so you can just specify these options so the default command is one so npx playwright
- 32:15test this is the command will actually runs your playwright test and if you want to execute this
- 32:20command by default it will execute all the tests which are created under the test folder if you
- 32:25want a good specific test you need to provide the test name or if you want to run the test on only
- 32:31Chromium browser so we can specify the project equal to chromium also and if you want to run your
- 32:36test on the specific browser and also in headed mode you can say additional option called iPhone
- 32:41iPhone header and if you want to run your test on the Chromium browser in headed mode and debug mode
- 32:47you can add additional parameter iPhone debug so all these options are optional so it depends
- 32:53on your requirement you can add these options along with this command npx play right test
- 32:59okay so this is how simply we can create clear write test in vs code editor and we can run it
- 33:06so in the next video I will show you what are all other commands are available in this page picture
- 33:11So currently how many commands we have used in the page go to is one command which will open this URL
- 33:16on the browser and the other one is a title is one command which will return the title of the current
- 33:21webpage and there is another command URL we have used which will return the current URL of the web
- 33:28page but we are just used it for only printing purpose but we have not added any validation on
- 33:34this but to add validations we have to use expect function expert page dot to have title is this
- 33:41one expect page dot to have URL is this one so these are the new commands which we have learned
- 33:47which are coming from expected and if you want to close the page you can simply say page Dot close
- 33:52okay so this is how we can simply create playwright list and we can run it in multiple
- 33:57ways so in the next video we will see how to interact with the different type of elements
- 34:02like how we can interact with the input box how can you interact with the buttons and links right
- 34:07how can automate the test what are other commands are available in the page so that we will see
- 34:12in the next video so that's all for this video thanks for watching we'll see in the next video
About this transcript
This page contains the full transcript of Playwright with Javascript | How To Create and Run Tests | Part 3 by SDET- QA, generated from the public captions YouTube serves with the video. The transcript has 6,016 words across 325 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.