Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Sunday, July 19, 2020

Part-5: Want to quickly become an Selenium Automation Engineer and build framework right from scratch?



In Part-4 we saw a sample test-automation script which automated what we thought of automating as per Part-2, but it is a rough script which doesn’t hold any value as per industry standard because we are not following smartness, intelligence, framework. 

So in this part, we will modify the last script bit-by-bit and slowly convert it into a more optimized and smart code that follows a rule which every team member of your will follow[as the same rule will be laid for every member], let's jump into it.



As we saw above, when we search the word ‘selenium’, it does search it, but after it keeps the search text-box in dangling state. So let us relieve the text-box from this, as it is bad programming practice, once our work is done, we should make it a point, whatever we open we should close it as well.

So we will put our text-box into a WebElement and call the submit() method which acts as an enter key and removes the dangling state of the textfield, and update the last few lines of the code as below:

WebElement textField = driver.findElement(By.cssSelector("input.gLFyf.gsfi"));
textField.sendKeys("selenium");
textField.submit();

So now it will send the text as ‘selenium’ also and also search that text in Google by hitting enter[by using submit() method] as below:


Now we will add one more requirement, when I open the browser it should be in full screen and not part of a screen and also once the required work is done it should also close the browser[consider an example where you have 100 test, so it will keep 100 browsers open and reduce the performance of the system]. So we will just add two small steps as below:



First is ‘driver.manage().window().maximize();’ what this does is, it tells the chrome browser to manages its window to maximize it. So now when chrome browser is initialized before Google could open it maximizes it.

Second is ‘driver.close()’ , here it tells the chrome driver to close the browser of the current session and ‘driver.quit()’, here it tells chrome driver to close all of its open windows. You will understand it better when we have a child window open example further. So when we run above code it opens a browser, maximizes it, searches text ‘selenium’, hits enter, and closes the browser.

Give it a try!!

Now we will update our code in the form of methods, instead of all the code lying in a single class. Its always better to have methods calls to required methods of a class to fulfill its requirement rather than having all code bunch at one place, this what industry follows.

Maven has miraculously already helped you in creating two folder structures viz., 'src/test/java' and 'src/main/java', where 'src/test/java' is the folder where we write our test-cases and 'src/main/java' is the folder where we place our helper classes. So in order to create our test-case,  right-click on folder ‘testSearch’ under 'src/test/java' and create a new class ‘FirstTest’ in it. Hope you got why we created this class in 'testSearch' folder, the simple answer is all test-cases related stuff will lie under this directory only!! So slowly you will envisage more live benefits of Maven further.




PS: in the above screenshot please check the box ‘public static void main(String[] args)', I missed to check it while taking the screenshot. It is required because our execution will start from this test case and earlier class will only act as a helper class and we will remove the ‘public static void main(String[] args)’ method from ‘InitialTest.java’ class.

Let’s start updating our code now to formulate into a step further, towards a formalized way of framework pattern. Go to class ‘InitialTest.java’ and add whatever code it had earlier in ‘public static void main(String[] args)' into a method called ‘InitializeDriver()’, then create one more method called ‘closeDriver()’ after this method and cut two lines ‘driver.close(); driver.quit();’ from it and put in ‘closeDriver()’ method, ie now your code will transit from:


To something like this:


So this ‘InitialTest.java’ class will become your helper class, to provide you with helping methods like browser initialization and closing the browser tasks. Now let’s move back to class ‘FirstTest.java’. 
You just need to add below three lines of code and run it:

InitialTest initial = new InitialTest();
initial.initializeDriver();
initial.closeDriver();

The first line, creates the object of the class InitialTest, second line using recently created object ‘initial’ calls the method ‘initializeDriver()’ present in that class to initialize the browser and the last line, as done earlier closes all the browsers opened by the driver.
Once done, right-click and as ‘Java Application’:


This will again give the same result as earlier, of opening the browser, searching closing the browser, but now it’s in more tuned and proper industry format.

In the next session, we will update this with the new paradigm of 'TestNG' and further important test-cases and learn further. Till this point, it was very important update our code from simple few lines to a certain standard industry accepts, here on we will write everything in TestNg and make our tests very powerful than earlier, where instead of running as ‘Java Application’ we will run it as TestNG test-case, which will also give us options to run few test-cases out of the whole, ordering test-cases etc..etc.

Thanks for reading and don’t miss the next part TestNG!!

~Sanjay Nikalje


Monday, May 25, 2020

Part-1: Want to quickly become an Selenium Automation Engineer and build framework right from scratch?


   Hello everyone, I know you have clicked this link and opened this page with lot of hopes, to upgrade yourself from Manual Test Engineer to Automation Engineer, or to enhance your current automation skills. I know you have tried hard in this direction, but it was not that fruitful.

You have tried your level best, but not getting complete-end-to-end solution and now you have given-up. I promise that I wont let you down and strive to fulfill your requirement to best of my knowledge and provide step-by-step guidance. I will not use huge wall of paragraphs to bore you, or deviate from your requirement of learning, will talk straight to point quickly and will try make this a great and fast learning platform.

   I will be using Java-Selenium to continue my discussion, no problem even if you use C#, Python[concepts remain same]. Below are the topics that I will cover to build framework from scratch[don't worry, we will cover every unknown word]:

1.  Java
2.  Selenium Webdriver
3.  TestNG- Cucumber[BDD]
4.  Maven
5.  Log4j
6.  Extent Reporting
7.  Screenshots on Failure
8.  Jenkins
9.  Saucelabs/BrowserStack[Cloud/Grid executions]

   Just a bit of elaboration on above topics:
1.Java: We will be using 'Java' as the programming language[you can use C# or Python if you are comfortable with it]

2.Selenium: We will be using Selenium Webdriver. It is an open-source automation testing framework, for validating web elements on a web-application across different browsers and platforms[operating systems].

3.TestNG- Cucumber[BDD]: We will be using TestNg as an Assertion Framework and in later part we will also use Cucumber at last, which is a tightly coupled framework with Behavior Driven Development[BDD],and how it makes our work easy.

4.Maven: We will use Maven as build tool, how it helps in getting jars acts as central repository etc.

5.Log4j: We will also use Log4j, a very helpful tool, for logging our logs of execution, it can be helpful, in knowing the flow of application, finding an error and can also be used if management ask to produce just the logs of yesterday night execution.

6.Extent Reporting: We will be using this tool, to produce effective presentable reports of our framework[in simple words Test-cases execution report].

7.Screenshots on Failure: In later part of discussion we will be working on taking screenshots on failure[where the test exactly failed], because as the number of test-cases increases day-by-day, it will become very difficult to know what failed and where, so a failure screenshot will help you to directly get to the place where it failed.

8. Jenkins: This is a very-very powerful tool, it is also called as Continuous Integration Tool or Build Tool.We will see how with help of this tool we ease our deployment, provide triggers of execution[means at what time and on which day execution should happen], provide execution mail etc.

9. Saucelabs/BrowserStack[Cloud/Grid executions]: At last we learn how to deploy your execution on cloud with parallel testing . I know many of you in your interview question have answered 'NO' to this question, 'Have you worked on Grid?', but after this you will always say 'Yes'.

   So lets start this wonderful journey of Upgrading YOURSELF!!
 
What is a test-case:A Test-case is a set of conditions or rules which a Test-Engineer will determine whether a system under test satisfies the laid requirements or works correctly as per given conditions.

Who is a Test-Engineer: An Engineer who test above scenarios is an Test Engineer[I never use words Resource or Tester, because the person has taken formal years of education to be at this position, so respect is inevitable.]

What test-cases needs to be automated: Those test-cases which forms part of repetition or regression or can be done without manual intervention needs to be automated.

Who is an Automation Engineer: An Test-Engineer who automates test cases using above list of buzz-tools is an Automation Engineer, ie You. And eventually SDET[Software Development Engineer in Test]

You need to have Java installed in your system. Do below steps to install it:
1.Download the latest version of Java, hit Google with simple term 'java jdk download' , open the link and only down load the 'JDK Download' and not 'JRE'. On clicking 'JDK Download' search for 'Windows x64 Installer' as almost everyone now has 64 bit systems. Click the '.exe file' and download will start.

2.Once download is over go to the location where download is done,double-click the .exe and just click following next->next->Finish. Just jot-down the location where installation is done, in my case it is 'C:\Program Files\Java\jdk1.8.0_201'

3.Now whenever your application runs,it needs Java, so how will it come to know whether Java is installed or not, it will check it through the Environment Variables.
So we need to educate our environment variables about our Java installation path. Just right click 'My Computer' and select and navigate to 'Properties->Advanced System Settings->Environment Variables'.


4.Over here out of the two parts mentioned , see the below part under 'System variables', click 'New' and in then next small window enter 'JAVA_HOME against 'Variable name'  and
'C:\Program Files\Java\jdk1.8.0_201;' against 'Variable value' and click 'OK'.



5.Then scroll below again under 'System variables' to find variable called 'Path' select it and click on 'Edit', click 'New' it will navigate to the location for editing , enter %JAVA_HOME%\bin.


6.Here you are just mentioning where your java executable's are present, ie
JAVA_HOME\bin = C:\Program Files\Java\jdk1.8.0_201\bin and click OK.






7.Now open Command prompt, to check whether your installation and mapping is correct or not. In search write 'cmd' and hit enter. The command prompt should open.
Enter command: 'java -version' and hit enter, it should show you your installed java-version.



If it throws error like 'java' is not recognized as an internal or external command'
it means there is some issue with installation or mapping. Check and redo the mapping.

Now lets write our first ever java program. Open Notepad and write below simple code.
Note: 'FirstProgram' is the class name of the program:

public class FirstProgram
{

    public static void main(String args[])
   {
     System.out.println("My first Java Program");
   }
}

Save as 'FirstProgram.java' in your directory, sometimes you wont have rights to store in any of the 'C-drive' other than your drive.




Now go to command prompt and navigate to the location  where you have saved the file  using 'cd command', for me it is 'cd C:\Sanjay\temp'.
Now type command: 'javac FirstProgram.java' like mentioned below:



'javac' is where you are asking your compiler to compile your program. If your program doesn't have any issue, it will compile without throwing any errors, if errors are there then you need to rectify those first.

Once compiled, you need to run, the program using  command 'java FirstProgram' and your program runs  successfully, giving output 'My first Java Program'






Do let me know if you have any comments.

Continued in next release...Part-2: Selenium WebDriver[most important part]



Click below links for releases till now:

Did she really loved me....? Part-1

Did she really loved me....? Part-2

Did she really loved me....? Part-3

Did she really loved me....? Part-4

Did she really loved me....? Part-5

Did she really loved me....? Part-6

Did she really loved me....? Part-7

Did she really loved me....? Part-8

Did she really loved me....? Part-9

Did she really loved me....? Part-10

Did she really loved me....? Part-11

Did she really loved me....? Part-12

Did she really loved me....? Part-13

Did she really loved me....? Part-14

Did she really loved me....? Part-15[The End]

Want to own your own start-up? Want to be Richie Rich?

Hey Corona, you should go now!!

Part-1Want to quickly become an Selenium Automation Engineer and build framework right from scratch?

Part-2Want to quickly become an Selenium Automation Engineer and build framework right from scratch?

Part-3:Want to quickly become an Selenium Automation Engineer and build framework right from scratch?

Part-4: Want to quickly become an Selenium Automation Engineer and build framework right from scratch?

How to create any mathematics tables with excel...

  Generate any tables with excel... 1.        Go to excel sheet, in first cell ‘A1’ enter the number whose table you want to generate, for...