Friday 10 August 2018

Use JavascriptExecutor to do many cool things in Selenium

Javascript is a very powerful language to interact with DOM. So it would make sense to use Javascript to run in browser using Selenium whenever there is no other way to work on particular element using Selenium.  

JavascriptExecutor is an interface. 
By creating object   JavascriptExecutor jse=(JavascriptExecutor)driver; 
we can use method executeScript to execute anything in browser using java script. 

Firstly, some of the basic things like scroll, get URL, Title can be performed using java script with following code. 

window.scrollBy(0,400); // x, y --> x is for horizontal scroll. y is for vertical scroll. And if you pass negative value it scrolls in other direction. 

document.URL;  // get URL of current browser
document.domain; // get domain of browser
document.title; // get title of the browser page. 

Now this code we can execute in selenium using... 

JavascriptExecutor jse=(JavascriptExecutor)driver; 
jse.executeScript("window.scrollBy(0,400);");

it would scroll down by 400 pixels from current position. 

You could also javascript returning some data such as in boolean, long, String, WebElement and using in your selenium code. 

Eg:
String currentURL= jse.executeScript(""return document.URL").toString();

Similarly you can write javascript to work on any element for that matter and call it in Selenium. 

Other approach you can use with JavascriptExecutor is to use elements created to execute in Javascript. 

Eg:

WebElement button=driver.findElement(By.name("Insert"));
button.click();
jse.executeScript("arguments[0].click();",button);


Why we declare WebDriver driver = new FirefoxDriver(); and not FirefoxDriver driver = new FirefoxDriver();

Why do we declare WebDriver driver = new FirefoxDriver();
and not FirefoxDriver driver = new FirefoxDriver();
To put in other words, why List<String> list = new ArrayList<String>();
and not ArrayList<String> list = new ArrayList<String>();


Here, List or WebDriver is an Interface - a contract or set of rules created for implementing class.
So, in Java, we cannot create an instance of interface. ie
I cannot say List<String> list = new List<String>();
But classes can be instantiated. Hence,
FirefoxDriver fd= new FirefoxDriver(); and
ArrayList list=new ArrayList<String>();
are meaningful. Because both are classes in Java.
Now, coming back to the same question. When we say FirefoxDriver fd= new FirefoxDriver(); or ArrayList list=new ArrayList<String>();
This is nothing but using specific implementation. Arraylist or FirefoxDriver. If we have coded like this, then in the future, if we want to switch to
other implementations of List or WebDriver... then we need to modify our complete code as there is no guarantee that the rest of the code doesn't make use of methods specific to the particular class. However, if given like this:
List<String> list = new ArrayList<String>();
WebDriver driver = new FirefoxDriver();
It gives the flexibility to switch implementation any time with minimal rework.
For eg: changing ArrayList to LinkedList you can change the code to List<String> list = new LinkedList<String>(); from List<String> list = new ArrayList<String>();.

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...