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


No comments:

Post a Comment

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...