Friday, July 3, 2020

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


Selenium WebDriver, Locators...

In Part-3 we built a project using Maven, which created a wonderful project structure, if you see properly it has ‘src/test/java’ and ‘/src/main/java’ folders. We will normally use ‘src/test/java’ to write our test-cases inside it and ‘src/main/java’ to write the miscellaneous/helper classes[we will discuss this further].

So for now, we start our first automation script and we will keep on enhancing it further.  Go to your eclipse IDE to ‘GoogleSearchTest’ project that we created in Part-3. Right-click on ‘src/main/java’ >New-Class->IntialTest[Give this as the class name]



Once you click ‘Finish’ a template of your class will be formed. Paste below lines  as mentioned in the screenshot:

System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");

Webdriver driver = new ChromeDriver();


Now go to the link https://chromedriver.chromium.org/downloads and click ‘ChromeDriver 83.0.4103.39’, because this is my current Chrome browser version.




Now select what OS version you have. Since I have Windows machine, I will click the appropriate one  as below, to download it to my drive:




Now go to the location where it is downloaded and move it any location of your choice[if required] and unzip it, to get the .exe file. Copy the location where it is unzipped, for me, it is ‘C:\Software\drivers\chromedriver_win32 (1)’: 


Now update this path in your code as below:


You must be still wondering, why there is still an error even after so many steps. These are repeatable steps, occurring in almost every scripts, so now after gaining the understanding of these steps, you won't find it lengthy at all. 

Secondly, there is one more magic Maven which we will see, is that now, for the above error line we don't need to download Selenium jar, but instead of we doing it,  we will ask our friend Maven to do it for us, how? Just type ‘selenium maven dependency’ in google, and now navigate to the link suggested and click on ‘Selenium Server’ link:


And select the latest stable version, never select ‘Alpha version’ as below:


Now just click/select the text available inside the box:


Go to your eclipse>double-click pom.xml file[it’s the heart of your project] :


Copy text after  ‘dependency’ tag as mentioned below and save it:


Now go to your code and hover on the first error, you will find some solutions now to resolve your issue, which was not present earlier:


One issue resolved:

Now again hover mouse on another issue, it will suggest corrective measures to be taken and select the option mentioned below:



The code now after resolving the issues, is free from errors, because Maven provided all the corrective implementations that the code needed, without you doing anything, other than just hovering and selecting appropriate suggestions:



What do the above two lines do?:

System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
-This basically sets the system property to key named ‘webdriver.chrome.driver’ and value is the path  where the chrome driver is downloaded on your system.

Webdriver driver = new ChromeDriver();
-This is basically used to create an instance of the chrome driver.
Now you have the power of  chrome-based driver with you and you are all set to do whatever actions you want your chrome browser to do. 

Remember our test-case was to open Chrome browser, then open Google.com and then type the word ‘selenium’ in Google search box, it should display various options Google provides as per its search criteria. So now add ‘driver.get("http://www.google.com");‘ line in above code > save > right-click > Run As > Java Application:



Magic: The moment you run this, another chrome browser instance will open and it will run your desired test-case



There are 2-3 points by which you can guarantee that the browser opened was as an output of an automated test-case:
  1. In your eclipse IDE console, it will show message: ‘ChromeDriver was started successfully.
  2.  On newly opened browser it will show message ‘Chrome is being controlled by an automated test software’
  3. And the third is the desired URL being opened.


Yes, this confirms you have successfully automated a simple browser-related test-case. Congratulations!!

Now, let's move to locators. Selenium has the following locators:


But what are locators?

Selenium provides a number of Locators to locate a GUI element precisely on a web-page, whether its a text-box, radio button, or any other element. So using these locators we can find the web-element and do our required set of actions on it. We will see it’s usage in due course. Simple, right!!

So now for our test-case, we need to send a string/set of characters called ‘selenium’ in Google search box. So to find locator for this, right-click the element on which you want to perform the action, in our case, right-click inside the search box.


And select ‘Inspect’, you will see below[highlighting the part of the code on which we inspected]:


Here we can see inside the highlighted portion a tag starting from ‘input’ having various attributes and associated values for it, like attribute ‘class’ has value ‘gLFyf gsfi’ and many others following it. We will use ‘CSS selector locator to uniquely identify our element and how to implement it is, given in below code:
driver.findElement(By.cssSelector("input.gLFyf.gsfi")).sendKeys("selenium");

You can use below cheat-sheet for finding elements using CSS:

No.
Tag
Comment
1.
tagname[attribute=’value’]
To be used when you have tagname, attribute and value
2.
[attribute=’value’]
Above can also be written like this, excluding tagname
3.
tagname[attribute*=’value’]
To be used when using a regular expression, in case the value is too big, you can use part of it.
4.
tagname#idValue
To be used when you have ‘id’ attribute and value, a shortcut
5.
tagname.classValue
To be used when having ‘class’ attribute and value, a shortcut

So in our example, we used tagname.classValue like ‘input.gLFyf.gsfi’ , ie tagname is ‘input’ and attribute ‘class’ has value ‘gLFyf gsfi’. The small catch here is if you see properly in our eclipse code we have mentioned class value as ‘gLFyf.gsfi’ and on Google inspect page, it is ‘gLFyf gsfi’.

So whenever using class as an attribute and if the value of the class has space we need to replace that space with a dot(.). As we have successfully found the element, we need to send out characters or string ‘selenium’ into it. We do that by writing ‘.sendKeys(“selenium”), to the element we accurately found.

Now again run the code as ‘Java Application’ as mentioned earlier:




Now you will find below:

Wooo... You have successfully automated your required manual test-case as mentioned in Part-2.

Isn’t it easy, as I told you earlier? Do comment and let me know your feedback, if this was helpful to you or you would like to refer this to your friend or colleague who wants to learn automation.

In the next section Part-5, we will improvise this further and use all the locators and other things like TestNG, Screenshot, Logs, ExtentReporting, Jenkins and many-many other automation features to build a robust framework.



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