Change language

Using TypeScript in Nightwatch js UI automated test suites

Using TypeScript in Nightwatch js UI automated test suites

alright welcome everyone in todays testing tutorial im going to show you how to write nightwatch.js automated tests in typescript programming nightwatch tests and typescript is a newer ability that i feel at this time has been baked out enough to use when writing my tests using typescript has many advantages over javascript such as intellisense when writing your tests and the ability to catch many mistakes as youre writing your tests instead of when youre running them i recently figured out all the steps needed so in todays video im going to show you how to convert an existing nightwatch test suite originally written in javascript into typescript alright so if you want to follow along im going to put my source code here on github.com forward slash reallymello under nightwatch tutorials so im going to basically convert what i have currently under pom example and put that under pom typescript example for the finished product so you can just do a gh repo clone of reallymello/nightwatch tutorials to go ahead and get that on your system and follow along as i continue here alright so since the code is already on my system im going to open up the POM example folder and show you what we already have in there so its a simple nightwatch javascript project with a google test.js file with the simple test uses the page object model and instantiates a google page up here and then it uses that google page object to go ahead and navigate and assert the title and do some searching and then verifies that nightwatch appears on the search results so using this example suite will be a good example of what youll need to do to convert an existing javascript test suite that you have in nightwatch over to typescript so lets get started by taking the tests and page objects folder and moving those into this POM typescript example folder and you can put that wherever you want on your system but thats where im going to go for today so when youre working in typescript the convention usually is that you put your source files in a folder called source so im going to dump these under source all right so now we have our page object and our test under the source folder so now we can go ahead and set up the rest of the test project so now to create our package.json file since we dont have one yet im going to run npm init dash y to use the default configuration and so now if we open that up we should have a package.json file and now we need to fill that with our dependencies that we need for this so now would be a good time to install nightwatch globally on your computer so to do that were going to type in npm install nightwatch dash g for global so now lets bring in the rest of our packages and save those into our package.json so were going to type in npm install nightwatch type typescript were bringing @types nightwatch and these are going to be all the nightwatch typescript types from the definitelytyped library and lastly lets install geckodriver and chromedriver were going to save those as developer dependencies or development dependencies all right so now that we have our dev dependencies we can create a nightwatch configuration file so to do so im just going to type nightwatch here without one existing so that should error out and create a default configuration file for me so if i open that up we have our source folders and page objects path for source folders were going to specify a new directory that doesnt exist yet called dist and inside there were going to look under source and inside there we look under tests so when we compile our or transpile our typescript its going to go ahead and move everything into a folder called dist and mirror our existing folder structure here so its going to be under source tests and thats where were going to look for our tests similarly were going to do the same for our page object so well change that to page objects under dist source spelled correctly so well save that and since i like using chrome on this system im going to scroll down to the default section under desired capabilities im going to change the browser name from firefox to chrome and if you havent installed typescript globally lets do that as well so lets type in npm install typescript dash g and now that typescript is installed i can run tsc -init to create our tsconfig.json file and here it is over here so in here lets scroll down to outdir and were going to uncomment that and were going to change that from dot slash to dist so that it moves all of our transpiled source code into the dist folder and then the next option were going to do is allowjs so go down find that on mine its on line 40 and were gonna leave that as true and as the name implies this will allow you to work with javascript files within the same project as your typescript files which is great because if you have a very large project like i do you dont need to do a big bang approach to actually convert everything at once you can have your javascript and typescript nightwatch test living side by side along with your page objects so that you can just kind of convert at your own leisurely pace alright so now we have all of our packages installed and our configuration set so now we can go ahead and start working in our code so lets start off by converting the google page.js file so the first thing well do is rename the file extension from js.ts to let the ide know what were doing and it should start to give us some errors so the first thing well need to do is add an import statement at the top to bring in the types were going to use so thats simply saying import were going to take the page object model type and enhanced page object model type from the nightwatch definitely typed directory and that came in from that at types nightwatch import that we did earlier so all these types come out of here alright so now that we have our types we can use them alright so the first thing im going to do is get rid of this module exports pattern and im going to name this as a constant of google page and im going to give it the type page object model and the page object model now has all these different types on it so if i try and change something like url to something that does not exist its going to complain about it and gives you a nice little suggestion did you mean to write url well yes i did so the page object model types actually even extend into the various elements so if im defining elements in here im not quite sure all the different options that are available to me again if i type in something wrong like select tor then it tells me that that does not exist so i can fix that but it also reveals to me the other properties on here so if i want to specify things like the locate strategy i can do so and it lets me know exactly what locate strategies are valid as well so i can say this is a css selector if i want which keeps me from making silly mistakes so if i said css selectors thats going to error out as well and thats pretty awesome especially if youre getting started with nightwatch and youre not sure of all the syntax so lets just save that up and now youll notice that we have some squiggly errors here thats because its expecting a type for the this object that gets passed in and that is going to be an enhanced page object and that should resolve all of the problems in here so the last thing we need to do is now export this as an interface so to do so were going to go down to the bottom and were going to say export default google page and now were going to export the interface so were going to say export interface google page extends enhanced page object and the enhanced page object type takes up to three different inputs the first one being your commands the second one being your elements and if you have any sections that is a third optional one so this one doesnt use any sections so were going to start off by just saying type of commands or google commands and then the next one were going to say type of google page dot elements and close it out with the double curlies and thats all you need to do to create a nightwatch.js page object using typescript so now that weve converted our page object to typescript lets do the same for our test so similarly were going to change the file extension first so lets type that as dot ts so now in google test.ts we start to get our squigglies letting us know that typescript isnt happy so lets make it happy so in our tests were going to use nightwatch tests in nightwatch browser so lets import those at the top and lets get rid of this module exports and well call that const google test give that a type of nightwatch tests and now that weve named google test we can export it down here at the bottom so lets do that before we forget export default google test and save that so im going to change the way i do this a little bit so im going to take off the function call and use arrows and now we just need to fix this so typescript doesnt like it when you leave things untyped so that right now browser has the any type so its giving you a warning so were going to go ahead and type that as a nightwatch browser so now that we put a type on browser we have all this nice intellisense here thats exposing the night watch api so now you can see that we have keys and assert base url and all this stuff that you might not be totally familiar with unless youve read the night watch documentation very thoroughly like i have so lets stop being silly for a moment and back that out lets uh change the let to a const on google and lets type the google page object to google page and so that adds the import statement here at the top import google page from page objects google page so now that we added the google page object model type to the google constant similar to when we added that to the browser object well have all the intellisense available to us so we know if were making any mistakes and whats actually available to us so we know that the click search options here now and if it took any uh kind of inputs here it would let us know but it lets us know that the signature here is just a empty uh method so now we can just call it just like that and if we you know made a mistake it would let us know that it doesnt accept that all right expect zero arguments but we put one in there so lets get rid of that since this was a valid test before it should remain a valid test after right so that we dont have any squiggles or underlines here and click search is available like it always was but again if i typed a mistake here it would let me know about it so lets save that up and just quickly recap you just need to bring in your imports at the top so that you have access to your types to add types to any variable you just go ahead and use this colon and then the type name and it adds it in otherwise its going to default to the any type which is wide open just like javascript is and then once you have that all wired up you should get rid of any errors that you might be seeing or warnings from the typescript compiler and lastly when you switch over from module exports you need to make sure that you actually export const google test here down at the bottom so export default google test all right so at this point we should have a working night watch test suite written typescript but theres some final things we need to do here to get it all running lets go ahead and open our package.json file youll notice that by default it just has a not specified for the test command so what were going to do is specify one and typescript adds a little bit of complexity in that you need to transpile your test mean ing that typescript needs to transform your typescript files into javascript and when it does that since we specified it in that configuration at the top of the tutorial its going to dump those into the dist source folder so the most pain-free option ive found to do that is to specify everything that you need to do as like a chained test command here so the first thing we do is type in tsc which is the typescript compiler then do a double ampersand sign and then run nightwatch but basically this is the only prefix you need to worry about that is going to be different than anything else that you were doing so if youre adding extra flags here to nightwatch to run certain environments or certain reporters or whatever you just add that here and it will work as as normal you can also break these out and do this separately if youre running it manually on the command line but this makes it a little bit easier so enough talking lets do npm test all right we have a success beautiful at the top here you can see once we typed in npm test it ran tsc and nightwatch heres our test output down below you saw the browser pop up and if you look we have a surprise here under typescript example we have our dist folder that got created with source page objects and our page object and google test youll notice that theyre back to js extensions because they were transpiled by the tsc command and if we open it up you can see what that turns into in javascript and again what makes this all possible are the type declarations that we brought in when we did npm install at type nightwatch and if youre curious is where all this comes from under definitelytyped in the types nightwatch folder you can go ahead and look here through index.d.ts to find out everything thats available to you as far as the types concerned the maintainers are doing a really good job building this out for nightwatch 2.0 but if theres anything thats missing you can go ahead and add an issue or submit a pull request and theyre very good about adding it in alright so thats how you convert your existing night watch tests and page objects into typescript check the video description down below for a link to the accompanying source code on github and while there please click on the like button if you enjoyed the video and consider subscribing for more testing tutorials thanks for watching

Using TypeScript in Nightwatch js UI automated test suites TypeScript: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method