SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Disable JavaScript Using Custom Profile For selenium WebDriver Test

Many times you need to disable JavaScript In your browser to verify that function or validation Is working fine even If JavaScript Is disabled. Simple example Is you need to disable JavaScript of Firefox browser to check server side validation of Input text field. Enabling or disabling JavaScript Is
simple task If you will do It manually but how to do same thing In selenium webdriver test?

As you know, Selenium webdriver launch fresh browser every time so your current browser's settings will not work In webdriver launched browser. So we need to write some special code In test case to disable JavaScript run time. Earlier I have provided example of how to enable or disable JavaScript In selenium IDE on THIS POST. Now let us learn how to do It In selenium WebDriver.

WebDriver has very good feature which allows us to create custom profile for firefox browser and then we can set/update browser's settings run time. Earlier we have set and used browser's custom profile run time to download different files as described In THIS POST. We will use same thing here to disable javascript.

Bellow given syntax will create new firefox profile and set JavaScript disabled.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);

and then we can use that custom profile In webdriver launched browser using bellow given syntax.
WebDriver driver = new FirefoxDriver(profile);

So now your webdriver browser's JavaScript Is disabled. To verify It practically, Run bellow given example In eclipse. It will click on Show Me Alert button but due to the disabled Javascript, Alert will not not prompted. Used checkAlertPresent() function to verify If alert Is present or not on page.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class jscript {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  //Create new firefox custom profile.
  FirefoxProfile profile = new FirefoxProfile();
  //Disable javascript for newly created profile.
     profile.setPreference("javascript.enabled", false);
     //Use custom profile which has javascript disabled In webdriver launched browser.
     driver = new FirefoxDriver(profile);     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
 }
 
 @Test
 public void getCoordinates(){
  //Click on button to get Javascript alert.
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  //Check If alert Is present or not.
  if(checkAlertPresent()){
   //If alert present then bellow given code will be executed.
   Alert alert = driver.switchTo().alert();
   System.out.println(alert.getText());   
   System.out.println("Alert present");
   alert.accept();
  }  
 }
 
 public boolean checkAlertPresent(){
   try{
    //It will return true If alert present.
    driver.switchTo().alert();
    return true;
   }catch(NoAlertPresentException ex){
    //It will return false If alert not present and print bellow given message In console.
    System.out.println("No Alert present. Verify If javascript Is disabled.");
    return false;
   }
 }
}

This way you can disable javascript In your selenium webdriver test using custom profile.

11 comments:

  1. Firstly thanks a lot for sharing your knowledge and really well explained in a simple view.

    ReplyDelete
  2. Hi Sir,Can you please provide the to disable JavaScript in Internet Explorer.

    ReplyDelete
  3. I found this error while setting preference:

    Exception in thread "main" java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

    ReplyDelete
  4. Even I am also getting the same Error Message.java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

    ReplyDelete
  5. Preference javascript.enabled may not be overridden: frozen value=true, requested value=false.how to solve this issue

    ReplyDelete
  6. profile.setPreference("javascript.enabled", true);

    ReplyDelete
  7. Or else u can make it as profile.setPreference("javascript.disabled",true);

    ReplyDelete
  8. or else make it as profile.setPreference("javascript.disabled", true)

    ReplyDelete
  9. I am getting the error as

    java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

    ReplyDelete