Contact for Pricing => KulFiy.com@gmail.com

Guest PostBookmarkingForumListingOnline Store
Selenium Scripting ...
 
Notifications
Clear all

Selenium Scripting Tips and Tricks

  

2
Topic starter

If you are just ongoing to learn Selenium training then the below tricks and tips will be useful. These tips and tricks have all the basic things element of using Selenium that you might forget and it will help you in remembering them. Let’s have a look at all the tricks and tips one by one.

Create a Web Driver Instance

You can make your tests generic. The first step will be initializing the browser bypassing the value from the configuration file. You can have one key as the browser and the value as Chrome, Firefox or IE. You can then initialize the browser according to the value passed there. Let’s look at the code which will be involved.

Check Whether an Element Exists or Not

If you have to check whether an element exists in the web application or not, you can easily do it by getting the size of the web element and then checking if it is larger than zero or not.

Boolean isElementExists = driver.findElements(By.xpath(“”)).size()>0 

How to Wait for Page to Load Completely

Sometimes, you have to wait for a page is to load completely. For this, you have to first check whether it is loaded properly and then you can do any other operation on it. You can use the Javascript executor to check whether the page is loaded completely or not.

How to Take a Screenshot Using Selenium WebDriver

Sometimes, it is very important to take screenshots while execution to know what went wrong. You can have Selenium Driver and then you can cast it to takes Screenshot interface. Make an instance of it and then use it to get the screenshot. You can use kits method. getScreenshotAs and then you can specify in which format you are expecting your screenshots to be.

Get the HTML Source of a Web Element 

Selenium certification WebDriver offers you a method called getAttribute which will allow you to get the internal HTML of a web element. You can do it by getting the element first. Afterward, you can use getAttribute to get the innerHTML of a web element.

How to Select Dropdown Options Using the Select Class

In Selenium, if you encounter a dropdown then you can select any option in it using the Select class. There are many ways by which you can select an option, which includes selecting by text, index, or values.

Refresh a Web Page in WebDriver

If you want to refresh the page then you can use navigate() the method, then use refresh() method to actually refresh the page so that all elements will be initialized from the beginning.

How to Switch to A New Tab

The first thing to do is to use .getWindowHandles() and then you can switch to the first index of the array list with all the window handles.

Set the Driver Executable Path

For browsers, you have to see the path for the driver executables. For setting the path you have to use the System. setProperty method in Selenium. Let’s see how it looks.

Switch to A New Popup Window

If you want to switch to a specific window which is opened then you can first use getAllWindowHandles()  and then switch using the method driver .switchTo() .

But if you want to specifically switch to a particular window, you can get a web element and see the visibility of it in a particular window. If it is present, then you can switch to it.

How to Click on A Checkbox Which Isn’t Visible

There are many instances when the checkbox you want to click isn’t visible and unfortunately, you won’t be able to click on it. You have to first enable it so that you can actually click on it. If you don’t enable it then you will get an exception like, “Element is not currently visible and so may not be interacted with."

 ((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", checkbox);

JavaScript executor can be used to enable it so that Selenium can click on it. The code snippet is below:

Go through this Javascript training to get a better understanding of the concept.

How to Do a Mouseover Action

If you want to mouse over a web element, you can use the Actions() class to handle it. You can use function moveToElemenet() and then build() and perform() in a sequence to perform mouse hovering action on a web element.

Delete Cookies Before Running a Test Script

Sometimes it is very important to clear all the cookies before running your test scripts. You can use a method DeleteAllCookiesfunction of  driver.manage() .

The code snippet is as below:

 this.driver.Manage().Cookies.DeleteAllCookies(); 

Also, if you have a cookie with a particular name, then you can delete it using the method DeleteCookiesNames of manage() class.

 this.driver.Manage().Cookies.DeleteCookieNamed("CookieName");

 How to Maximize the Window in The Test Script

Sometimes, you need to test the scripts in full window size and resolution. You can easily do it by using the method of driver instance. You can then use the method followed by maximize()to take the window to the maximum size.  

Code snippet is as below:

 The software development life cycle is an iterative process, meaning that the development of a single software component may often result in several code change/deploy/test cycles. Distributions/restarts to circulated application servers can be very time intense.

JRebel helps more than 35,000 Java developers instantly see the impact of their code changes, without restarting their applications or losing state. With JRebel, developers can:

  • Instantly load 95% of typical code changes, including complex Java EE updates
  • Write code 17% faster by excluding claim or server restarts
  • Increase predictability of releases by +8%
  • Avoid distractions and stay focused on writing quality code
  •  
    3.7K
    Shares
  • 1.1K
  •  
  • 1.5K
  • 1.1K
  •  
  •