Showing posts with label How to introduce Javascript in Selenium. Show all posts
Showing posts with label How to introduce Javascript in Selenium. Show all posts

Thursday, January 11, 2018

Javascript in Selenium Web-driver

Hello All,

Welcome to Software Testing Made Life Easy blog. In this post, we will see how we can introduce Javascript in Selenium Webdriver.

What is Javascript?

Javascript is one of the programming languages of the Web and It is a lightweight programming language and most commonly used as a part of web pages.

All modern HTML pages are using JavaScript.

Use of Javascript in Selenium Webdriver:

We have used Java in our script and we have implemented almost all features but some features can’t be handled by Java. So we need scripting language which can control server side & client side scripting. That's why JavaScript comes into the picture.

Implement Javascript in Selenium Webdriver:

To implement Javascript in our selenium script, We don't need to write the separate code because we have a predefined interface available in package org.openqa.selenium.JavascriptExecutor.

Inside this, We have some predefined method called executeScript() - Whatever script you will pass as a 'String', those will be executed by JavascriptExecutor.

Example:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JavaScriptTestCase
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
System.out.println("Launching Browser");
driver.manage().window().maximize();
driver.get("https://software-testing-made-lifeeasy.blogspot.in");
((JavascriptExecutor)driver).executeScript("document.getElementById('s').value='Balaji'");
}
}