Many times you lot necessitate to disable JavaScript In your browser to verify that component subdivision or validation Is working fine fifty-fifty If JavaScript Is disabled. Simple instance Is you lot necessitate to disable JavaScript of Firefox browser to banking concern fit server side validation of Input text field. Enabling or disabling JavaScript Is
simple draw of piece of occupation If you lot volition create It manually but how to create same affair In selenium webdriver test?
As you lot know, Selenium webdriver launch fresh browser every fourth dimension hence your electrical current browser's settings volition non operate In webdriver launched browser. So nosotros necessitate to write unopen to especial code In attempt instance to disable JavaScript run time. Earlier I create got provided instance of how to enable or disable JavaScript In selenium IDE on THIS POST. Now allow us acquire how to create It In selenium WebDriver.
WebDriver has really adept characteristic which allows us to create custom profile for firefox browser in addition to and hence nosotros tin bathroom set/update browser's settings run time. Earlier nosotros create got ready in addition to used browser's custom profile run fourth dimension to download unlike files every bit described In THIS POST. We volition purpose same affair hither to disable javascript.
Bellow given syntax volition create novel firefox profile in addition to ready JavaScript disabled.
FirefoxProfile profile = novel FirefoxProfile(); profile.setPreference("javascript.enabled", false);
in addition to and hence nosotros tin bathroom purpose that custom profile In webdriver launched browser using bellow given syntax.
WebDriver driver = novel FirefoxDriver(profile);
So directly your webdriver browser's JavaScript Is disabled. To verify It practically, Run bellow given instance In eclipse. It volition click on Show Me Alert push but due to the disabled Javascript, Alert volition non not prompted. Used checkAlertPresent() component subdivision to verify If alarm Is acquaint or non 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; world degree jscript { WebDriver driver; @BeforeTest world void setup() throws Exception { //Create novel firefox custom profile. FirefoxProfile profile = novel FirefoxProfile(); //Disable javascript for newly created profile. profile.setPreference("javascript.enabled", false); //Use custom profile which has javascript disabled In webdriver launched browser. driver = novel FirefoxDriver(profile); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(" "); } @Test world void getCoordinates(){ //Click on push to acquire Javascript alert. driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click(); //Check If alarm Is acquaint or not. if(checkAlertPresent()){ //If alarm acquaint in addition to hence bellow given code volition hold upward executed. Alert alarm = driver.switchTo().alert(); System.out.println(alert.getText()); System.out.println("Alert present"); alert.accept(); } } world boolean checkAlertPresent(){ try{ //It volition render truthful If alarm present. driver.switchTo().alert(); render true; }catch(NoAlertPresentException ex){ //It volition render fake If alarm non acquaint in addition to impress bellow given message In console. System.out.println("No Alert present. Verify If javascript Is disabled."); render false; } } }
This agency you lot tin bathroom disable javascript In your selenium webdriver attempt using custom profile.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM
http://www.software-testing-tutorials-automation.com/