JavaScriptExecutor Class

1. How to scroll until found the element?

WebElement element = driver.findElement(By.id("id"));
JavaScriptExecutor javascriptexecutor = (JavaScriptExecutor) driver;
javascriptexecutor.executeScript("arguements[0].scrollIntoView(true),element)

2. Write a few javascript code to interact with web elements

//Enter value into textbox
javascriptexecutor.executeScript("document.getElementById('email').
setAttribute('value','Hello')"
);
//another way
javascriptexecutor.
executeScript("arguments[0].setAttribute('value','Hello')",webelement)
//to get the value of attribute
javascriptexecutor.executeScript("return document.getElementById('email').
getAttribute('value')"
);
//click on the element
javascriptexecutor.executeScript("return document.
getElementById('u_0_r').click()"
);
//// Login button highlighted by yellow color and green box
javascriptexecutor.executeScript("arguments[0].setAttribute('style',
'background: yellow; border: solid 2px green');"
, webelement)

3.What is JavaScriptExecutor in Selenium?

  1. JavaScriptExecutor is an interface used to execute JavaScript code in Selenium.
  2.  It acts as a medium when WebDriver wants to interact with HTML elements we use javascript code.
  3.  JavaScript is a programming language that interacts with the web browser's HTML elements.

4. What are the methods used in the JavaScriptExecutor interface?

  1. executeScript()
  2. executeAsyncScript()

5. Write code to show the scrolling using JavaScriptExecutor

JavaScriptExecutor js = (JavaScriptExecutor) driver;
//scroll down
js.executeScript("window.scrollBy(0,300));
//scroll up
js.executeScript("
window.scrollBy(0,-300));
//scrolBy(horizontal,vertical)

Here it is asked to scroll vertically (to the bottom of the page) up to 300 pixels without changing horizontal pixels.

6. How to identify and interact with hidden elements in a web page?

  1. Elements are hidden in the web page using the below attributes
style.display = none or style.disply = block
  1. Those hidden elements are available in the dom. But it is not visually displayed on the web page
  2. We can use JavaScriptExecutor to catch the element below
JavaScriptExecutor javascriptexecutor = (JavaScriptExecutor) driver;

javascriptexecutor.
executeScript
("docuemnt.getElementById('id display in the dom').style.display='block'")

driver.findElement(By.id('id display in the dom')).click();
javascriptexecutor.executeScript("arguments[0].click()",element);

7. Is there any way to enter text without using the sendKeys() method?

Yes. Use JavaScriptExecutor interface

8. How to get the user to enter the value in an input field?

driver.findElement(By.id("id")).getAttribute('value') 

9. What is the other way to scroll down without a JavaScript executor?

sendKeys(Keys.PAGE_DOWN)

10. How will you enable a disabled textbox?

We can enable a disabled HTML element, be it a button, input field, text area, etc, by changing the attributes using the .removeAttribute(“disabled”) method. Here is a code to enable a text box using the Javascript executor.

String enable ="doc.getElementsByName("name').removeAttribute('disabled');";
javascript.executeScript(enable)

No comments:

Post a Comment

Cucumber

What is Cucumber? Definition : Cucumber is an open-source tool that supports BDD . Purpose : Bridges the communication gap between business ...