How To Enable/Disable Textbox In Selenium WebDriver On The Fly

We tin know element's enabled/disabled status really easily using isEnabled() method inward selenium webdriver software attempt every bit described inward THIS EXAMPLE POST. Now supposing you lot bring a scenario where you lot wants to enable whatever disabled text box or disable whatever enabled text box during software attempt execution in addition to therefore how to create it? Webdriver do non bring whatever built in method past times which you lot perform this action. But yes, Webdriver has JavascriptExecutor interface past times which nosotros tin execute whatever javascript using executeScript() method.

I bring posted many posts on how to execute javascript inward selenium webdriver software testing tool to perform dissimilar actions. You tin persuasion all those representative on THIS LINK. Same way, nosotros tin enable or disable whatever chemical cistron using JavascriptExecutor interface during webdriver software attempt representative execution. To disable text box, nosotros volition use html dom setAttribute() method in addition to to enable text box, nosotros volition use html dom removeAttribute() method with executeScript() method. Javascript syntax for both of these is every bit bellow.

document.getElementsByName('fname')[0].setAttribute('disabled', '');
document.getElementsByName('lname')[0].removeAttribute('disabled');

Now permit nosotros utilisation both these javascripts practically to empathize them better. VIEW THIS POST to encounter how to enable or disable text box inward selenium IDE software testing tool.

Copy bellow given @Test method purpose of enable or disable chemical cistron in addition to supplant it amongst the @Test method purpose of representative given on THIS PAGE(Note : @Test method is marked with pink color in that linked page).

@Test   world void attempt () throws BiffException, IOException, InterruptedException    {   boolean fbefore = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();   System.out.print("\nBefore : First Name Text box enabled condition is : "+fbefore);   boolean lbefore = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();   System.out.print("\nBefore : Last Name Text box enabled condition is : "+lbefore);      Thread.sleep(2000);      //To disable First Name text box   JavascriptExecutor javascript = (JavascriptExecutor) driver;   String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');";   javascript.executeScript(todisable);   Thread.sleep(2000);      //To enable Last Name text box   String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');";   javascript.executeScript(toenable);   Thread.sleep(3000);         boolean fafter = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();   System.out.print("\nAfter : First Name Text box enabled condition is : "+fafter);   boolean lafter = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();   System.out.print("\nAfter : Last Name Text box enabled condition is : "+lafter);        }
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post