Localization Testing with Selenium + Page Object Model - Mini Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is naveen welcome back naveen automation labs so in this video i'm going to talk about how to handle multi-language support application and how to automate them for example let's see this is application which is un.org and um this is supported with multiple local like it can you can see this in english also in french also this is i guess russian this is spanish this is uh chinese and you can check in arabic also like this you just need to click on multiple languages link over here and everything will be changed here how will you handle that do you need to create multiple frameworks for that for the different languages or in the within the same framework we have to adjust that there are different multiple approaches so we will talk about the easy one and the very famous approach that i was using in a couple of projects earlier so if you notice one thing let's see this is a by default english first of all url also getting changed after un.org it's en when you click on french it is coming like fr over here ignore this uh you know notification just this is coming from the browser then you click on english over here and then what you do is that let's inspect anything for example let's say you want to inspect peace dignity and equality or something like this so you right click on it and go to inspect and try to inspect this guy here you can see that the locator and other properties will remain same the only the title value will be different in case of a new language let's see if i'm clicking on the french and you see this title and this text will be changed see this i'm clicking on this french now and here you can see that it's coming in the french now right same thing if you see in other language in russian so it will be according to title equal to russian language and this one so only the text is getting changed it's not like that html tag or other properties will be changed or their attributes will be changed it's not like that the template of the html dom will remain same the only thing is their values their keys will be different as compared to on the basis of that language specific so back to english for example let's see if you uh slightly scroll it down let's go to the footer of the page on the footer of the page you can see contact i want to verify the contact is there or not so here if you inspect this guy here it's you can see that yeah buy dot link text and contact is available if you really want to use it in selenium but same thing if you go to the french the everything will remain same the only thing is now it is doing something like this you inspect over here like this and then it's giving you contacts something like this in french language but their href property the a the parent tag the main tag html tag everything will remain same here right so here you have to understand first this is a layout this is a basic template this is how all the application works the html dom and everything will remain same only and only the search string keys will be different specific to the local or specific to the the language okay so how will you automate that so to automate this let's simple design a basic design that what exactly we're going to do that so for every language specific i'm going to maintain some properties file over here so let's see this is for french this is for english and let's say this is for russian this is for arabic or something like this right okay let's see this is for russian and this is for arabic so these are my config.properties file so this is i'll write this is my config file config file in the form of properties file in java we will be using it you can maintain some json file also or xml file also but config.properties file will maintain the data in the form of key and value pair format so better we use it this and easy to maintain and easy to read and write also so you maintain these things and then define their respective keys over here i'll show you some examples when we design it so let's see uh this is my url is equal to what un.org french and then the contact key so key will remain same here for all the line uh you know for english also contact will remain same contact will remain same like this but their respective values will be according to the french language in english the key will be contact only for the contact link but the respective value will be according to the english same thing for russia and same thing for other languages also after that what we have to do is uh we will create some other utilities for example we will maintain one driver factory which will or whatever the name or util class or base class whatever you want to use it so let's see this is my driverfactory.java class in this class i'm going to initialize my driver here so this is for initializing of the driver after that i'm going to maintain uh another class let's see this is my util class for element so this is my element util class and then here i'll be writing my by locators method and find element some utilities i'll be writing it over here by locator on the basis of xpath on the basis of css and so on so this is a separate utility this is my driverfactory.java class and this is my element util.java class after that i'm going to maintain now we have to follow the page object model so we have to create one page over here so let's see this is my homepage.java and we know that for every page we have to create a separate java class according to the page object model so this is my homepage.java that i have created under this homepage.java what we have to maintain that we have to see now i cannot use at the rate find by over here i'll tell you why so we will not use at the red find by we will not use a typical buy locator also i'll tell you why because there is a limitation and we need to get the values dynamically on the basis of the language that you are using it then we will create some page actions over here right these are the page actions they will be calling some methods which method which is going to use the find element method we are going to use it so we will create one find element method on top of that we will create a wrapper and then which is actually internally calling this element util method which is actually saying okay fine i want to pass the x path or give me the find element or something like this and then i'll be performing some dot click or dot send keys whatever that you want to use it over here i'll show you practically okay so this is a homepage.java and then i'll be creating my test class because uh test is separately returned so let's see this is my hometest.java file we will be using it and here this is we can create one base test over here this is the parent of uh where parent of all the test classes where we will be maintaining our let's see before test and then after test we will be using it and here we will start writing our test cases over here multiple test cases we will be writing it like this okay so this is a simple design we have to maintain the page object model design pattern as well and then along with different utilities we have to use it now what we have to do is we have to create one more separate class over here this class is called the config plus config.java class we will be using it this config.java class okay we'll interact with these files because we have to get the properties class object on the basis of which language that you are using it so we will create one method over here which is used to initialize a property so method name will be initialize property method and we will tell okay fine we want english so if i'm passing english over here so it will connect with english and then load all the properties from english properties file right if i'm passing french then it will connect with french and then load all the properties from the french language and so on like that so this is what we have to use it so we need to pass this parameter this language parameter in this particular method this initialize property method okay so what we can do maybe we can use it from the maven so we will pass the command line parameter like this when we install with uh when we run the test cases from the maven so we will write maven clean install write something like this minus d and i'll say okay language is equal to what language is equal to french so what will happen it will start running your test cases french so we will read this parameter this language parameter we will read it over here and then on the basis of that we will connect to the french language and then we will load all the properties from the french configuration.properties file so this is the basic design at very high level so let's start writing the code this video will be slightly long guys so please watch till the end and i'm pretty much sure that will be a great learning for you guys okay so uh we will be using the maven project this maven project i have already created so that we won't waste our time unnecessary and then what exactly i'm gonna do that under this srt main java i'll be creating all my utilities factory and everything under src test java i'll be writing my test cases and then we have to maintain some properties also so for maintaining the properties i'm going to create some resources so right click on your project and go to new and let's create some source folder here one source folder let's see i'm writing this is my src main resources and then click on finish and then under this as a resources we have to create a file let's see just like we create for the environment specific file same thing we have to create it for the language specific file so for example let's see i'm going to write that this is my language dot which language let's see language dot french dot properties file okay and here i'll be maintaining all the french properties related keywords over here and let me maintain one more english also so i'll be writing that's okay find lang dot english dot properties file i'm going to maintain there is a spelling mistake one second it's the properties actually okay okay now how will you maintain so i'll go to my french dot property now see this carefully this is very important thing i simply write one key over here that okay this language is equal to french what will be the url so for french language the url is this so this is i'll just copy this so at the runtime i'll pick this url to launch my url directly on french so that we don't need to click on the french language or english language link on the page we will directly launch the url on the basis of the language parameter and then let's see i want to verify that this header is available or not pick something like this i really don't know how to speak french but let's see i'll simply copy this first text over here something like this or maybe i'll be using in xpath contains or maybe buy a dot link text something i'll be using it so i'll do one thing this is my what this is let's see page header some header key i'm maintaining it and this is the header i'm not using the second header also let's see i'm just using this one okay then i'm doing what i want to capture the title of the page also see title of the page also coming on the basis of the language so see this is a title so what we can do is right click on it and go to uh view page source and let's capture the title in french language so this is the title copy this and this is my title which is equal to this okay other than that if any specific element that you are looking for for example let's go to the footer and uh let's see this one this is my contact and i'll be using this one okay so the key is in english see this guys this is what you have to notice this key will be in english because this is my reference key and the respective language specific keywords we have to write it over here so contact will be visible in french like this contacts knows or something like this okay so let's see these are the fourth five keywords we have used and later on if you really want to interact with some more elements you can keep adding it over here i'm using it one simple poc so let's go to the english one and for english one also i'll just copy paste language is equal to what the language is equal to english in this case and it will be en instead of that you can cross check you simply go to let's see english and observe the url after slash it is en so i'm writing en over here and what will be the header see header in in this case it's coming in english only so i'll copy this header and i'll simply paste it over here so this is my english header see the key is remain same lang key will remain same header and what will be the title the title is what right click on it and go to view page source and english title is this i copy this entire thing and this will be met okay title and what will be the footer contact page contact link uh see this is contact so i'll inspect this guy okay and then see i'll be using buy dot link text or maybe xpath so i'll be using this contact here okay so i'll remove this french keyword and pass it to english so likewise you can create multiple properties one is for japanese one is for english french chinese and arabic and russian keep creating it that you can do it later because concept and template will remain same for everyone now this is our properties got created now i quickly go to src main java and i'm going to create a separate package for that let's see this is my config reader package or let's see config reader package here whatever the package name guys that you want to use it right now i'm not focusing on the naming convention or something like this that's the config reader package i have created under this config reader what exactly i'm going to do that i'm going to create a class and my class name is let's see config reader.java class that i'm going to use it okay and in this config.reader.java we are going to use what we are going to uh config reader or let's see config uh prop something like this config prop reader class i'm going to use it here okay let's write a better name property reader okay rename it now what exactly i'm going to do that here i'm going to maintain one private properties reference here first at the class level so let's see this is uh properties and import your properties from java.util package i'm going to maintain one private file input stream class object also because we need this uh stream so that we can use it okay in when reading the properties and then i'll start creating a simple method i'm pretty much sure most of the people they are aware of how to read the properties so properties file so i'll write that okay initially let's see this is my initialize language prop something like this that i'm using it okay and then what exactly i'm gonna do that i'll say find that you give me what you give me one let's see initially you give me one language whatever the language that okay uh you want to use on the basis of that we will switch it so i'll write one system dot order pentalin i'll say that okay that language is whatever the language that you are passing it over here and then i'm going to create this prop object i'm going to initialize with the properties okay like that and then i'm going to use one switch case statement here and what will be my key the key is this language that you are passing over here now anyone can pass the language in any language right it i mean in any cases it could be a small case also it could be a uppercase also so i'll do one thing whatever if you're passing capital french or capital english or small english or whatever every time i'm going to convert that into a lowercase here perfect and then i'm going to maintain different lowercase values so if you are giving me english then what exactly i'm going to do that here this prop file input stream class object that i have created which i'm going to write which is equal to new file input stream and where exactly your properties file path for english so what exactly i'm going to do that this is my english properties right click on it go to properties and copy this path from src okay and i'm going to use it over here like this in double quotes put a dot forward slash it means go to your current project directory and from src main resources it means you have to create the stream with language.english.properties like that okay once this is done you just break it and then i'm going to create multiple cases case number two and let's see case number three and let's see this is my for french and this is for russian although russian properties file we haven't created but later on if you really want to create that you just keep adding the number of cases over here like this in this case make sure there is no spelling mistake that you are going to write so lang dot i'm going to write this is my french properties and if tomorrow you want to create some russian properties so a simple write russian dot properties like that you can create that and in the default case i'll be printing one system dot order ventalin this a language not found or something like this along with that whatever the language that you are passing i'll be printing it over here like this and see this now this statement will give you what surround with try catch blocks do not use add throws declaration otherwise this will throw the exception and the method which is going to call this method that also has to pass the throws exception so what i'll do i'll do one thing that this entire switch block i'll write inside the try and um another thing is that and just closing it over here one more thing that once you make the connection the stream over here now you have to load the property so i simply write prop dot load method i'll be using it and i'll passing the respective input file input stream class object here right so let's see if language is equal to english uh key is english now it will go to this case i ip is equal to pointing to this particular properties and then load the properties from english ip only like that so at a time only one properties will be initialized like that okay so this is the property.load and this is my try block is getting ended over here and then i'm going to write one catch and then i'll write let's see one exception e here so this class is ready now okay i'm not going to touch this class only thing is that once this function is ended instead of void this function should return what the respective properties so i simply write prop object you have to return so instead of void what should i write instead of void i have to write properties over here because this function is giving you properties class object reference okay guys so this is ready now okay now uh we will go back to the main java once again and this under this main java we are going to create one driver factory class also so let's create one more class and then i'm going to write one this is my driver factory class in this class i'm going to initialize my driver let's see i'm going to maintain my public web driver first like this initially and i'm going to create one method here that is public void and initialize a driver method and this initialize driver method says okay fine you give me what you give me that which browser that you want so i'll just add one switch case for the browser and then you give me the properties also so let's see i'm passing the prop object over here as well like this okay import the respective things after that what should i do after that i'll quickly say that on which browser that you want to run so let's see i'll just print it first of all that your browser name is plus browser name and then i'll be writing either if else or you can write switch case also so let me write one switch case statement this is my browser name key you can pass any browser in lowercase or uppercase whatever you are passing i'll be converting into the lower case every time and then the value will be what the first value let's see i'll be writing a chrome over here and then i'll be using initializing my web driver manager as a bonnie ratio let's see chrome driver or you can write system.set property and then give the path of the chromedriver.exe file also that also you can do that and then i'll be initializing my driver is equal to new chrome driver here i'm not using a remote webdriver right now so just let it be simple here okay after that i'll be creating multiple cases case number two case number three let's see this is for firefox and this is for safari and for chrome driver i'll be using firefox driver and instead of this i'll be using firefox a driver over here for safari we don't need any safari driver executable we just simple directly right safari driver over here to launch with safari if nothing is happening it will just simply come to the default and then system.org i'll write that okay please pass the right browser name okay and whatever the browser that you are passing it i'll be appending it over here and now see this carefully what exactly i'm going to do that i'm going to launch the url after that so i'll write driver.get and where exactly the url is available the url is available in the properties file see we have maintained this particular key url and this is also a url key written over here right guys okay so what we have do we have to read this particular url so how will you read that so for reading this i'll simply come over here that's why i'm passing this prop object over here so how we use that prop dot get property method and the key is what the key is url i'll be reading it at the runtime so i'm not going to use a hard coded value over here after that let's say i want to use driver dot manage dot delete all the cookies and driver dot manage dot window dot i'll be using the maximize window and then finally i'm using the return driver over here later on if you really want to apply for a parallel execution point of view you can apply the thread local concept also later on so i'm just using the normal driver and see this will return the driver because i need the driver so instead of void what should i write i have to write webdriver here because this method is giving you driver and driver is what driver is webdriver type so we have to write webdriver over here okay so let's see this method is also ready now we don't want to touch any other thing in this matter very plain very simple method that i have created now what we have to do after this after that we have to create uh one more class so i'll do one thing i'm going to create another package over here okay let's do one thing right click on it and the let's move this create a package and let's see my package name is the factory package okay and move this driverfactory.java actually to this package just click on ok ok so i have one factory package i have one config reader package and then i'm going to create a separate one that is my util package so something like this little utils package and under this utes package i'm going to create one element util class okay guys elementor dot java class see this design i told you right we have to maintain one element util class also okay and you will get to know why we are creating all these things and we will we are just right now creating multiple components and then we will integrate all the components with each other okay now element util class is doing what element utility class is important that which is having some utilities with respect to creating the web element creating the buy locator and something like this so i'll do one thing element noodle is having its own private web driver okay and then i'm going to create one public constructor of this class let's see element util and this guy will say okay fine you give me the web driver and the same driver driver with the help of this keyword i'll be using and giving to this guy okay and you simple import this one okay because when someone is calling this element ut class object creating the object this constructor will be called and then we have to give the driver i don't need to write the logic for web driver once again the driver equal to new chrome driver and some people what they do the mistake is that element util is extending driver factory no they should not be extending the driver factory actually there is a spelling mistake it should be driver factory okay let's rename it rename to driverfactory.java okay now we will see okay in some time how to call this element util now i'm going to create a method over here the first method that i'm going to create that public void get element method here it means you give me the by locator and then i'll be returning what i'll be using my driver.find element and then i'll be using the same by locator that you are passing and instead of web void i'll be using a web element here why because driver.find element will return will return what it will return a web element perfect then i'm going to overload this method once again so let's see public void uh get element method this time i'm saying okay fine you give me the locator type and then you give me its locator value also right guys it means you give me let's see x path comma x bar value css comma css value like that when you call this function and then we will create the element at the runtime so how will you do that okay so we need what so i'll be writing okay fine i want to return driver dot find element but we need a buy locator so for that how will you get the buy locator so for that what exactly i'm gonna do that this will help me to yeah right this will help me to create the xpath for the different languages i'll show you practically how to do that but let's create the utility first i'm going to create a simple utility which will return the buy you must have seen these method like get by and having two parameters the first parameter is located type and the second parameter is locator value so let's see those these two parameters i'm going to use it and this will give you the by locator initially what i'm gonna do that i simply create one by locator which is pointing to null and then i'm going to maintain my switch over here switch case and this will become my key locator value convert that everything into the lower case because someone can pass the capital x path also capital css selector also capital id also whatever you are giving to giving and then we are converting everything into the lower case and then i'm going to maintain my cases over here let's see my first case is equal to id so how will you do that then i'm going to initialize with this locator which is equal to what which is equal to by dot i'll be using id and which value whatever the value that you are you are passing see this this value same value from the function i'll be using it here right so likewise i can create multiple cases case number one for id then name then let's see x bar then css and the link text that you can use it here or tag name also you can use it so i'm just using couple of one so let's see this is for name so i'll be using buy dot name here and then i'm using let's see this is for expert then this is my xpath then i'm using this is for css then i'm using in this case css selector and then let's see i'm using one more let's see which is for link text is a linked text here and then i'm using link a text and whatever the link text value that you are passing so i'll be creating the by locator on the basis of that and then do what right and then finally i'll simply say you return the same locator it means return this particular by locator so that's why the return type of this method will be by only perfect now i'll come over here in this method back to line number 20 so now i need a buy locator so can i call this method get buy method this method of course i can call it and passing the locator value and the located type and the value from here and then it will create the find element and then this method will return the entire format okay element over there so let's see instead of void i'll be using web element simple so i can see i can maintain my by locator in my page class that approach also i can use it if you really want to maintain in the form of strings that approach also you can use it so that's why i have overloaded this method as such a nice example of method overloading in selenium like this both methods are giving web element on the basis of this method is creating the by locator by its own with the help of this and in this this method is actually asking for the by locator in advance so that i can give you the web element from this function simple so this function is also ready now what we have to do is we have to start creating the page now okay so according to this diagram element util is ready factory is ready config.property is ready and these properties two files we have created and then we have to create now homepage.java and then hometest.java we have to create that so let's quickly homepage.java should be created under src main java i'm going to create a separate package for this so let's create a pages package we are going to automate only one single page and rest of the things exactly will remain same for other pages also so let's create a class over here and let's see this is my homepage.java under this homepage.java see this carefully what exactly i'm going to do that first i'm going to maintain i'm going to create the constructor of this class so let's create one public constructor public home page and this saying okay fine you give me the web driver a driver over here okay like this and import this and if you really want to use any private driver over here in this particular homepage class that also you can use it and the same driver you supply with the help of this dot driver is equal to driver see guys we have to maintain and flow of the driver from one method to another method from one class to another method we have to maintain the single driver this is what you have to understand majority of the people that do the mistake here do not write web driver drive equal to new ground driver once again that is done already with the driver factory over here now we have to circulate the same driver and i'll tell you when you connect all these components together then you will get to know why we are doing this so i'll go to my uh this method once again and i'll do one thing i'll be maintaining one my private element util also okay and this is my element util reference that i have created and then import this okay this class from the utility and element util class object also immediately i'll initialize otherwise if you perform any method with this reference it will give you null pointer exception so we have to initialize it so let's initialize that is equal to what new element util and then what pass the same driver over here because element newton also needs a driver to create the web element right so what will happen when you create the object of this class that's why we have created the constructor so that we can pass the same driver this is a nice technique to pass the driver from one page to another page with the help of constructor you got the driver and the same driver will be given to this guy and the same driver will be used over here to create the web element okay now go back to this homepage.java under this homepage.java what exactly i'm gonna do it now i'm going to create see this carefully i'm going to maintain one private method first or let's start with the public and then we will see that later so let's see public void and the method name is c i cannot maintain my at the rate find by over here because if you write at the rate find y right and then let's see you're maintaining the x path for this application or let's see for this contact i'm going to create one x path where a contains a text is equal to contact but this contact is actually is a string which is coming from the from this keyword over here so that is i cannot maintain with the help of at the rate find by right guys because at the red find by page factory if you are using it how will you get that value how will you pass the properties reference over there that i cannot use it same thing some people what okay some people what they do let's see i'm going to want in contact is equal to i want to use buy.xpath and then i want to create the xpath over here double slash a and then i'm going to use let's see contains or let's say i directly use text function over here text is equal to what the text is equal to this whatever the text is written contact but don't you think it will be a hard-coded value this will work only for english but for french it will not work so this contact should come from where this contact should come from this french language dot properties file so that's why i'm not going to maintain by locator strategy i'm not going to maintain any at the rate find by also so let me remove that so that's why what we have to create we have to maintain the methods here so that's why i'm going to create my method my method name is let's say i want to interact with the header element remember this map this one we have to check this header peace dignity and equality or something like this right so i simply say okay fine you give me the header value like this starting bracket ending bracket and then i'm quickly create the header x path over here so let's see this is my header x path value which is equal to what x bar should be written within double quotes and let's see i want to create the x path of this guide right this one and see this is a and where the piece dignity and the equality is return so i'll simply create one xpath over here double slash a where i'll be using contains text is equal to what the text is equal to this and uh sorry text comma so it is pointing to this guy right so i have to pass this english value over here this english value is coming from where this english value is coming from the properties file that we have already maintained so you can see that under english dot properties this header key is already maintained and this is the value that we want to use it so we want this header value from the properties file right guys otherwise there is no chance that you can create the xpath because i don't want to create this hard coded expert if you use this same expert over here like this then this is only for english right guys so what we have to do you give me whatever the language is specific header and then i'll be appending over here right so i'll be appending with single quote double quotes like that and append it over here like this so this is the x path at the run time i'll be creating it and then i'll be doing what i'll say okay fine element util dot then i'll be using this get element method which element method i'll be calling second method okay and then i'll be passing okay fine what is your locator type locator type is xpath i want to create the expert and this is your expert value so you take this expat value over here so what will happen when you call this method get element method this method will be called located type is expert and locator value that we have already given and then it will call this get by method it will go to a locator type is equal to what xpath it will come over here and buy xpath will be created here and then it will return the buy locator okay and then when you come back here so what will you get here you will be getting your so i'll simply say okay return the same element so instead of void what should i write i have to write a web element here so instead of creating your address find by or by locator directly we are using this approach over here perfect so let's see this is a get header element that i have created but on this map this method will just give you a web element but after that what do you want after that i want to perform some action let's see dot get text or whatever so i'll write okay fine i'll write public void uh get header a value like this so i'll simply say okay fine get header value and this header value we will be passing it okay so that also we will check we will be passing this header value from the test class so head string header value and first of all that i'll be calling this method can i call this method get header element so that i can get the element and this is the same header value i'll be giving to this guy it will create the x path and then creating the by locator give me the web element and then i'll be using a dot get text over here and the moment i get text i'll store in a string variable so let's see this is my header is equal to this and then i'll be printing it on the console see you have to avoid all the hard-coded values perfect and then what exactly i'm going to do that i simply say okay fine then i'll write if whatever header i can put a condition here that first of all you print it if header is not equal to a null then do what then what you do you simple write return again you call this method get header element method okay with the header value and then you check okay fine this method is displayed or not like this so the instead of this void i have to write a boolean here right and if nothing is getting satisfied then you finally return a null over here from this function like this also you can do that or directly what you can do is that instead of passing the text okay or let's remove this null what you do you simple return remove this if condition simple return whatever after printing the text you can return this header also or let's see i'm just simple returning that element is displayed or not over there okay this i'm doing just to show you guys that okay this is how we are printing it on the console and then i'm just checking it is displayed or not after that okay so let's see this method is ready right so likewise so first of all with this method this according to the page object model we have to use the encapsulation also encapsulation in the form of that okay this method i have to make it private actually because this will this is just to create my web element it should not be exposed to the end user or to the test client test client will always call this method okay fine you give me the header value that's it or check that okay header is displayed or not so let me i'll simply write is header exist or not that is a better method i'll be writing it okay i'll be giving this header value and check if this header is there or not on the page so this is should be private in nature and should be called over here so we can see yeah something like that we can do it and it should not be exposed to the test line okay now same thing i'll be creating another matter so you have to keep creating private public combinations for different locators as well so let's see this time i'm going to create what i'm going to use that okay let's see this contact this one this contact so i say that okay get contact element then contact elements i'll give you the contact value so this is uh the contact value that i'll be giving to you and the same contact value that uh i'll be creating one xpath over here like this and let's see this is my instead of header expert value this is my contact x path value right and the same contact x path value i'll be giving to this guy so what will happen again get element method will be called and you will be getting your contact over here right guys perfect okay so this contact value we have to use it over here okay and the same contact x path value we have to pass it over here like this now i'll do what i'm going to create another method to perform some action on it so i'll do one thing this time once again that i'll check that okay is contact exist or not perfect so this is the contact value that i'll be giving over here and the same contact get text and this is your just printed on the console whatever the contact is available like this and the same contact value i'm going to check uh is a displayed or not here or not okay so return and i'll be calling this method actually here perfect guys so again i have created one private method and then public method so from the test ng i'll be calling this method and from my side i'll be giving the contact value in english or french okay okay so this is let's see homepage.java that's enough i mean these two methods are enough right now we are not using driver so you can avoid this line right now we are not using it and now last thing is that we have to create the final thing is the test class so let's go to src test java go to new and create a package let's see this package name is my test package under this package i'm going to create a simple test class and this is my home page test class and this test will be we will be writing either junit or test ng so we are going to use test ng now so how will you write it so in test ng we have to first of all that uh create couple of things because now we have to initialize our driver and everything so we have to call the method this method initialize driver so how will you call it with the help of driver factory class so what we need we need to maintain one driver factory reference over here so let's see this is my df reference we have to initialize the properties also so i'll be writing that okay fine this is my config property reader reference also i have to create that so let's see this is my cp config property reference that i have created and then both the classes you have to import so let's import that okay then what exactly i'm going to do that i'm going to write my at the red before test annotation here and then i'll be writing public void this is my setup method and the teardown method i'll be writing it in setup method what you need first of all you need a properties you have to initialize so can i create the object of cp over here i mean config properties reader cp is equal to new uh config property reader that i have created right guys and then this cp dot see this method that we created initialize language prop right so initialize language prop it means you have to give me the language here okay so this method will be called if i'm passing language is equal to french so this case will be satisfied and then it will connect with the french dot properties and it will load the properties from the french language from the french property language right guys so let's see i'm saying that you have to get it from the french language so this is a hardcore value right now i'm writing it later on we will replace with the maven command line after that what we have to do this initialize language property is giving you properties class object reference so can i store inside the properties class reference let's see properties prop here and i simply say this is my prop is equal to this perfect now the moment i get my prop this is what i want so i'll do one thing after that i'm going to create the object of driver factory so simple create df is equal to new driver factory object okay see i'm going to create this it's getting loaded once again driver factory class object that i have created okay and this with the help of driver factory i have to call this method initialize a driver on which browser so let's see here right now i'll simply say okay let's see only one browser i want to use it for my poc and the property reference that i have already captured and then you give it to this guy it means all the french properties you give it to this method so what will happen initialize driver method will be called and this initialize driver method browser equal to chrome and the prop is what the properties class object and chrome is equal to this chrome will be launched and then it will come over here and then see this this prop is pointing to the french property so french property prop dot get property and give me the url which url it means this property url will be picked this url property will be picked okay so that's it and this initialize driver method will give you what this method is giving you the web driver so i'll do one thing and maintain my a separate reference over here like this okay and then maintain the same driver over here is equal to this this is what i want and then i want to create the object of home page class so i'll simply say okay fine this is my home page class object i'm going to create it so that i can call all the methods of homepage class right so this home page i'll be using it is equal to what new home page right and then i'll passing the driver over here remember because inside my home page i have created one constructor here you have to give me the driver and the same driver will be given to the element util so that element util class constructor will be called and the same driver will be given to this guy and the same driver i can use it in my utility so you don't need to write the dougly code driver.find element again and again like this perfect so now i got my homepage class reference this is how we design the page object model and then i'll be using this is my add the rate after test annotation and let's see public void this is my a teardown method and then i'll be writing my driver.quit over here and import your at the rate after test now i just need to maintain my test over here the first test that i'm going to write public avoid and the method i'm going to write this is the method header test method i'm going to write right guys and then how will you call this method so i simple i got my reference home page dot see i have created only two methods other two methods are private in nature so i'll be calling is header exist or not see this i'm calling this method now which is going to call this private method internally to get the element and then i'm going to check okay fine you print the text on the console or you can return the text also but i'm returning it is displayed or not if it is returning a boolean display so can i assert it with the assert dot as a true okay i'll be writing sorry assert dot assert true i'll be using it and this but the question is that how will you pass the header value this header value i'll be using this now so i'll be using this prop dot get property and which key that you are looking for i'm looking for c that's why i'm using the same key see language url header title contact same key you have to maintain language url header title and the contact so i'll be using this header and the respective values will be different in the respective properties class that doesn't matter i just need to get the key header because this property is already initialized with the french language so that's why this property is pointing to the french properties only so when i capture this header i'll be getting the french header only right guys is header exists then you immediately assert same one more test i'll be writing for the contact as well so let's see i'll be writing this is my um contact this is my a footer contact at the footer page or whatever footer test and i'll write that okay is a contact exist so i'll simply write prop dot get property and for contact do we have any property yes contact property i'll be using it so simple go to home page test key will remain same doesn't matter you are picking it from english or a french so what will happen is contact exit method will be called and i'm passing the contact from the french so french is this see this something like this nose or something like that and then what will happen in this case it will be given to this guy so i'll be calling this get header element okay so no we have to pass is a get contact element actually okay so i'll be calling this get contact element and the same contact value that coming from the french will be appended over here so i have created the xpath and the same xpath value i'll be giving to this guy with this expert key so get element method will be called and then the locator type and the locator value will be called with the help of get by method and uh that's it i'll be getting the web element and then i'll be returning what an element over here the moment i get my element i'll apply the get text printed on the console and then immediately i'll check okay fine it is displayed or not as well i can use it okay so instead of calling this two times you can call it one time store in the web element and then check is displayed or not like this also you can do it anyways so that is optimization of the code that we can do it later so these two tests are ready now will this work let's see so let's see i'm going to run this class and uh if it is any error is coming we will see that so let's run as test ng okay so browser is launched so we haven't executed yet from last half an hour we are just writing the code see this is a french language awesome you can see the url is coming with the french it means it read the property properly and two test cases are absolutely working fine awesome and here you can see the console output it's printing the contact in the language french language and it's printing that header also in the french language this is super awesome and if you really want to check it in english i simple change my english language over here so i'll write okay fine i want to test my application with english now let's run it and let's see so it should launch the application with english and the contact in the header should be printed in the english language only see you can see the url is coming in english see forward slash en and awesome you can see that on the console two test cases got pass contact peace dignity and equality like that perfect so this is the first thing amazing thing that we can do it but still this is a hardcoded value what we can do is that now we can uh do one thing we will pass this value from the command line right because this is the hardcoded value right now so let's remove it or you can maintain your testng.xml file from there also at the sweet level in which language that you want to pass that also you can do it i'll do one thing i'll just simply remove this guy from here okay and i'll go to this method initialize language property and remove this also from here right and then i'll do one thing from the maven i'll be using certain command like this maven clean install right guys and minus d e n v or minus d language environment variable i'll be using and the language is what let's say i'm passing french over here directly from the command line so this language is what is an environment variable from the command line so i have to read this so in java how will you read that i simply write system class here system dot get property method i'll be using it and what is my key this is the key guys this is the language lang key i have to use it over here okay so this lan key will give you what it will give you a string okay let's see i'm storing in the same variable here which is equal to this that's it so this language i'm using it now which is actually coming from the command line from the user side okay so here you don't need to maintain any hardware values you have removed it now but when you run this program now so if you run it from here then it will not work why because it needs one parameter see it will launch the browser but it's giving you the test cases got failed here you can see that on the console see this is giving you null pointer exception because null value is their url equal to null because nothing got initialized because what will happen when you initialize this lang property you are not passing anything from the command line so you have to pass the command line argument now how will you do that so for this we have to execute from the maven why we are doing it because exactly same thing you can do it with the jenkins also so i'll do one thing go to properties and let's go to the command line copy this entire path of the project okay of this project and go to the terminal and let me just clear it and change the directory okay and see this this is my project here you can see in my workspace multi-language project that i have already created this is the project i'll simply write maven clean install minus d env is equal to to which language you want to execute your test cases i simply say i want to execute in my french language simple pass it over here not env you have to pass lang because the same line we are using it here as well can you see that this and we are reading this property this is your command line property okay so let's run it and let's see it is working or not okay command started may 1 started browser is getting launched awesome launching in french perfect and okay let's let it run application is still loading and then done awesome see two test cases got passed and they both are coming in the french language over here this is super awesome right guys and here you can see that language is french and browser is chrome right now let's quickly check on different language also so i want to start in english so let's run it and let's see it means your application should launch in the english local with the english browser url so here you can see english everything is come in english and then it will quickly check with english perfect here you can see two test cases are absolutely working fine and the things are coming in english only perfect and here you can cross check language is english lang is english and browser equal to chrome and let's see i want to execute in russian but for russian we don't have any property right guys so see if i'm passing a russian but the bug in bug means here we are maintaining one case for russian but here it will check okay russian is not available immediately it will throw the exception here and will come inside the catch block it will come inside the catch block and it will throw file not found exception or something like this and then i'll be printing my own custom message over here that okay that uh properties file not found something like this okay for which language so whatever the language that you are passing right i'm just appending over here so that we will get a nice message over here language plus uh properties file not found okay so if i'm running with the with russian because russian properties are not available okay see it's saying that test cases got failed null pointer exception is coming entry is null and before that uh see russian properties file not found like this okay i know that we could be um in a better way also we can improve it because uh unnecessary why to launch the browser if language is not found that check we can put it uh beforehand instead of launching the browser like that but anyways this is absolutely working fine so what you have to do now what you just need to do maintain multiple properties file guys one is for french english russian arabic chinese and whatever the language that you prefer according to your system i mean according to your application and then keep maintaining the keys over there like that okay now if you really want to enter the value on the search the same search will be available on the french also like this so for that you can maintain a separate by locator right guys because here it doesn't matter if you really want to click on this here the text is not involved so you have to maintain the properties file only for those elements which are having some text because text will be different according to the different language right for buttons and all such things let's say you really want to click on some link over there also maybe some button without using the text in that case you just need to maintain in your home page you need to maintain your buy locator right so that you can guys you can do it for that so this is a just a small project i would say kind of poc which is absolutely working fine and then you can extend this framework to the next level i'll do one thing i'll just upload this entire code or push this entire code to my git repository share the url please practice and let me know if you have any issues and please suggest if you have any other approach that you are already using in your framework or in your company feel free to uh comment in the comment section there thank you so much guys i hope you got to learn some really good stuff over here if you really like it please subscribe and like and share with others so that people will get benefits out of these videos thank you so much and god bless you all
Info
Channel: Naveen AutomationLabs
Views: 8,924
Rating: undefined out of 5
Keywords: Handle Multi Locale (Language) with Selenium, handle multiple locale in selenium, different language automate using selenium, multiple language ui automation, selenium with localization, localization testing using selenium, naveen automation labs
Id: mbCBsHsSDnw
Channel Id: undefined
Length: 60min 48sec (3648 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.