#5 - Regular Expressions in #Cucumber 6 - Cucumber Expression vs Regular Expression (Latest 2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is navina welcome back to naveen automation labs so in today's cucumber video guys we are going to talk about what do you mean by regular expression and the cucumber expression in cucumber bdd framework so cucumber supports two types of expressions one is a regular expression and second one is the cucumber expression which is actually introduced in i think in cucumber two or three in 2017 and before that if you have used cucumber one in my previous videos also we were using uh a typical regular expression in the feature file and sorry from the feature file to step definition file regular expression means like this between zero to 9 characters i want and then numeric digits i want the string digits numeric actors or alpha numeric characters i want like that but now they support a cucumber expression also but there are certain rules are defined the first rule is that a step definition will be generating by default with cucumber expression only it means let's see i have written this particular search dot feature over here right this particular apple macbook pro as a string and then price is equal to thousand when you create the step definition file from the console when you run it from the console it will generate the step definition file for you right and then if you go to the search steps over here okay so by default earlier you have seen that let's see for this it's creating bracket like this a string and a string so let me write it once again over here that by default it will create a string and then for integer parameters it will create integer in a bracket over here like that okay or sometimes you will see end also both are same thing so what does it mean it means whenever you see over here that for apple macbook will be converted as a cucumber expression so this is called cucumber expression this is not a typical regular expression same thing the price is denoted by one curly bracket and with integer so automatically this particular step will be created by cucumber and then you can do that for example let's see i'm writing one more uh then over here and then i simply say that your order order id is something one two three four five and i want to create a step definition so just try to see that okay your order id is one two three four five and i simply say that username is in double quotes i'm writing let's see naveen over here right so for this it will create integer and for this it will create a string so i'm gonna run it and then on the console you will see that what kind of suggestion it will give you in the form of the step definition so just simple go to run configuration and uh from select this particular quick kimber configuration give it by practice search dot feature we have to select and click on run you see the console output that it's saying this implementation is missing and you try to copy this implementation go back to your step definition and then you simply paste it over here and you see this it's saying your order id is integer and the user name is string so that's that's why i'm saying that the first point i've written the step definition will be generating cucumber expression by default right it does not generate a regular expression by default okay second thing is that but you can use the regular expression also in your step definition file it means in the same step definition file for some methods or for some steps i can use a cucumber expression over some other methods i can use a normal regular expression also i can do that i can combine both the things together in my step definition file but the thing is you can mix both the regular expression in the step definition file but you cannot mix both expression in single step definition method in a particular step definition method you cannot use cucumber expression and the regular expression together it what i'm saying is let's see in this particular example here you are using cucumber expression and let's see for integer you cannot use regular expression over here for example the regular expression let's see i'm writing for this guy like this let's see for digits i'm writing d and then plus over here okay the moment i write it immediately it will start giving you error over here and then you go to search feature this line got flagged over here it means you are doing it wrong and the moment i make it to back to it means this is the cucumber expression and this is a regular expression both the things in the same definition method i cannot use that remember very important point over here that is what i have written over here in my notes that you cannot mix both expressions in the same step definition method but you can do let's see for this particular method i want to use regular expression and for this particular method i want to use cucumber expression that you can do it but for the same method both the things you cannot do it so let me remove this d and back it to integer save it error is gone and then from the search feature also again it's absolutely fine it is not flagged over here right okay then this example is string is the cucumber expression other example like you can write like integer also double float if you really want to create a floating value you can create floating value also like that for more cucumber expression you can go to their official documentation and you can check it more cucumber expressions over there and we will see in the upcoming chapters also now regular expression so regular expression is nothing like specific to kikimba if you're using java so you can use java regular expressions and it's a standard uh things to fetch out some value from the from the different strings and they are very very powerful in terms of getting the values from a long string let's see from the email body content i want to capture only the links let's say i want to capture that okay my transaction id from a particular string message i can give a regular expression that okay your transaction id could be six digit or eight digit or ten digit or order id or username or something like this i want to fetch out so you can create a strong regular expressions over there and then you can accordingly get it so to create the regular expression couple of things guys you have to understand that these are these brackets are called capture group can you see that and this is a range of the group 0 to nine let's see that i have given what do you mean by this so this is the regular expression it means in within this particular capture group i want zero to nine digits okay so it will tell that okay yeah zero to nine digits will appear within this particular capture group now there are different quantifiers are available so you have to understand what are the different quantifiers in a regular expression quantifiers means there are four types of quantifier generally we use it plus then we use a script then we use a question mark and then we use put a bracket and total number of occurrences we have to pass it over here for example i'm writing 0 to 9 in this particular capture group this curly i mean this normal parenthesis i'm writing right these two brackets i'm writing 0 to 9 in a square bracket with plus it means between zero to nine digits it might occur more than once or more it means once or more it might occur so what do you mean by quantifiers in regular expression define how many times a character needs to be occurred so let me write it for if someone is asking you what do you mean by quantifiers quantifier means how many times okay a character needs to be occurred okay so this a define like this so if i write this is called plus plus is the quantifier it means i want once or more than once between this 0 to 9 digits will appear for example what do you mean by this if i write 0 to 9 4 means 4 times four digits will be appeared okay n means total number of digits that you want so it means between zero to nine generate four digits it could be any digits like zero zero zero triple double line double nine one two one two three four five six double eight double it but any four digits between zero to nine range then we have star star being zero or more it means nothing and more it means it will generate the zero uh zero count or zero occurrence or more than zero occurrence it will do that same thing question mark zero or once it will generate it means at least nothing and then maximum it will generate once only that's it okay so these are the quantifiers are available and you don't need to understand you don't want to learn the complete full flash a regular expression guys but minimal knowledge about regular expression that is more than enough okay so that's why i have written my notes over here so that you can refer and then you can learn accordingly same thing let's see i have one more these are called a shortened characters okay for every regular expression which is defined with a shorthand characters so i would say these are my shortened characters let's see a d is for numeric digits so if you search this term on google i'll take this particular google i'll simply go to google let's see and i simply say that shortened characters in regular expression so here you will see that you can pick any particular website and here you will see that a different shortened characters are available over here so for example let's see backslash d black select d means adds all the digits to the character class like this so this is backslash d backslash w backslash s is available over here so backslash d means it matches a single digit character backslash w means it matches a word character it could be alphabetic word it could be numeric word it could be underscore also it will match all right backslash s means all the white spaces to that actor including tabs end of line etc you can do that right so these are the three three uh i would say shortened characters generally we use backslash d for digits backslash w for all the words and black slash s is uh for the white spaces attacks and end of line characters we can do that so you don't need to remember each and everything whenever it's needed you can simply come over here and you can check it you can check other websites also they have better uh uh websites you can go to orelly.com as well uh you can check it over here backslash d means a digit between zero to nine like that okay capital d means a non-digit number and backslash w means a word character over here like that so i'll just refer this particular site url over here you can refer this for all the short end characters you can get it from this particular website now as i told you this is for numeric digits and if you write plus and plus over here it means shorthand operator if you're writing it means uh anything any numeric digits will appear uh more than once once or more than once it will generate right okay another example let's see if i'm writing that this is my capture group a bracket and then i'll write that okay i want a character between a small a to small z capital a to capital g and then between zero to nine and how many characters you want i simply write a plus over here as a quantifier so my quantifier will be plus over here plus means once or more then more than once so it will generate the characters between small a two small z capital a to capital z and zero to nine more than once okay it means so like that so accordingly you can guys create your regular expressions and then along with the quantifiers and use in your step definition file now i'll go back to my search steps and let's try a couple of things over here as i told you cucumber expression and the normal regular expression cannot be together in the same step definition method so if you really want to use let's see i really want to use that any kind of a digit i want any kind of numeric digit i want right so i simply say that i'll put a bracket over here and that is denoted by slash backslash d and then more than once i want but here you are getting an error it means java does not understand javascript does not understand that okay what exactly this is called invalid escape sequence so you have to introduce one escape character over here with backslash d over here like that and the error is gone so this is the regular expression it means java does not understand that and repeat once again java does not understand what exactly the meaning of this backslash at d right so for this to escape this character you have to use backslash over here now this so backslash b is for any numeric digit plus means this is my quantifier that is ones or more than once and then this is for escape character and this is my capture group over here so for example if i'm passing thousand over here the thousand will be iterated as a with this particular regular expression but again we are getting error because of this string we cannot make them together this is cucumber expression so let's remove this particular string and write a typical regular expression over here so for typical regular expression for the string what i want that first of all you see this particular search dot feature and it's saying that apple macbook pro it could be any string tomorrow it could be samsung it could be any product but this product is written within double quotes so we have to write a regular expression within double quotes first of all so i'll write within double quotes but again java is assuming that okay you have already started this particular string and which is ended over here so this string will be treated like a ended string no so that's why we have to introduce one more escape character as backslash for both the strings for both the double quotes then i'll create a capture group like this and in this particular capture group what do you want i'll put a square bracket and then i want that other than what other than double quotes i want to ignore the other okay double quotes it means give me any set of characters which is other than double code right because in my apple macbook we don't have any double quote available over here so i want to ignore that particular i want to escape that particular double quote also so that's why i have to put one more backslash over here right it means other than double quotes so to doing this we have to use one carat spatial character over here as well so if you have seen cucumber two three years back we used to use this particular a regular expression over there and any kind of character we want so let's say i'm putting a star over here like that okay and then you go to your search dot feature uh let me check it once again this is the right expression or not i'll write let's see plus over here instead of star let's see plus simple save it let's check it once again am i missing anything this is fine this is fine and uh yeah this is has to be this has to be written over here okay and let's see yeah see this is not flagged now it means it got captured it mean this is the right step definition that i have written now if you run this particular service dot feature let's see it is a taking apple macbook with price as thousand or not so run configuration and simple run it and let's see and see the console and it's saying somewhere one error over here i'll tell you what you mean by this error it's saying cucumber exception we are getting it's saying that this step is defined with two parameter and some error we are getting so and it's saying however the gherkin step has zero argument so how to solve this particular problem guys whenever you want to use the regular expression what you have do in your step definition you have to start with carrot and end with a dollar over here remember this thing if you remember in my previous video two three years back that i prepared in cucumber one and cucumber two you used to use these regular expressions but starting with this carrot and ended with this particular dollar now let's run this particular search dot feature and let's see it is working or not so run as cucumber feature sorry run as configuration run configuration select this particular feature file and then run it and let's see the output this time and this time it's saying there are no errors and absolutely working fine although it's giving you pending exception because the last step we haven't given any okay we are throwing the pending exception over here so let me let me remove this guy and i'm not writing any step over here right now let's run it again you will get the clear picture right click on it run configuration and simple run it again and now so you see the console output all the four steps are absolutely fine without giving any error and you see the step number two guys it's absolutely taking apple macbook pro and the thousand over here it means this apple macbook pro is replaced by a regular expression in my search step with this regular expression and the price thousand replaced by this digit over here now if we really want to change something else let's see apple instead of apple macbook pro i simply write apple macbook air instead of thousand let's say i'm writing a 200 in that case let's see it is picking the same value or not so that is the advantage of regular expressions you will be getting it you run it and let's see you can see the console it's absolutely working fine with apple macbook pro air sorry macbook air and price is 200 fine so this is how guys we can write one expression if we notice both are regular expressions which is allowed according to the rules now here i'm using one integer and one string they both are cucumber expression this is also absolutely allowed but the moment i make this particular string let's see i take this particular string over here with this guy right and then i simply write something then it will not work because one is integer and one is a regular expression so this won't work so this is not allowed so i'm not running it unnecessary like that so the rule is remember these four rules guys the step definition will quickly recap will be generating by default with cucumber expression in the latest cucumber we are using cucumber six it means whenever we are generating the step definition from the cucumber feature file it will generate the cucumber expression not the regular expression but you can use regular expression also in your step definition file you can mix both regular expression and the cucumber expression in your step definition file but you cannot mix both the expressions in the same step definition method and these are the basic expressions capture group quantifiers and the shorthand characters i have explained so that's all for this particular video guys and i'll see the next video with some new examples and new concepts of the cucumber please share this video with others who are learning the actual use case of cucumber and we will be doing a lot of cucumber features my major concentration initially on the cucumber features we can easily write cucumber with selenium in upcoming chapters that is definitely we will do that but before that these small small things you really need to understand with respect to cucumber okay thank you so much guys please subscribe to the channel if you're learning something from these videos and let me know if you have any issues till then take care and thanks for watching naveen automation labs
Info
Channel: Naveen AutomationLabs
Views: 22,772
Rating: undefined out of 5
Keywords: cucumber 6 bdd, cucumber BDD framework, Cucumber 6 Latest features, Cucumber by Naveen AutomationLabs, Cucumber By Naveen, Cucumber with Selenium, Cucumber with Regular Expressions, Regular Expression in cucumber, Cucumber Expressions, Regular Expression in Automation, Naveen Automationlabs
Id: AxhfQV-6iqw
Channel Id: undefined
Length: 21min 25sec (1285 seconds)
Published: Wed Nov 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.