5 important Tricks/Methods in selenium automation

Test Automation

In test automation using selenium, there are a couple of methods which are effective and useful.

Some of these important tricks/Methods are mentioned below.

1- getPagesource-Reading page source of a web page, using getpagesource method, which brings full page source, it brings same page source value which brings on right clicking mouse for seeing page source manually.

It is possible that a test developer requires some page text which does not have a specific locator.

How to do it-

String str ;
str = driver.getPageSource

now using str in conditional statement test automation developer can perform a various action.

2- Switching to a web frame-(frame with random id) using custom sync handling

usually switch to frame can be done by below syntax-

a- to a specific index- driver.switchTo().frame(0);
b- to a specific locator- driver.switchTo().frame("id of the element");
c- if no of frames are known but locator id is not static, then switching to that frame can be done using below piece of code-

int i=0
Int size = driver.findElements(By.tagName("iframe")).size();

3- Refreshing of a web page

An already opened webpage can be refreshed by using driver.navigate.refresh()

4- Using try-catch statement as if/else-

It happens sometimes that a particular webobject has 2 different values and there is no nested condition available to incorporate for e.g. a child hyperlink inside a tree which has 2 properties. which can be handled as
catch(exception ex)

5- Use of JavaScriptExecutor– Use of javascriptexecutor for interacting with webelement.

WebElement button = driver.findelement("Xpath");
JavascriptExecutor js = (JavascriptExecutor)driver;
//Perform Click on LOGIN button using JavascriptExecutor
js.executeScript("arguments[0].click();", button);

There are multiple ways to fix any issue or to create script however it depends on the situation when to use what. Above mentioned tricks can be helpful in many cases.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Triggering SoapUI from selenium

The software industry has adopted Service Oriented Architecture-SOA, which consist of Microservices consist of SOAP and REST. Selenium is an API which helps in Web automation, Selenium does not help in API testing although SoapUI scripts can be invoked from selenium/Java classes. Using below 3 steps SOAPUI project can be …

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …