Javascript Method
Javascript Method
Since Selenium 3, maximum browser are providing their respective browser driver
which implements WebDriver API. Selenium communicates to these browser drivers
through HTTP commands and these drivers communicates natively with browser.
Official documentation of Selenium says:-
“Selenium-WebDriver makes direct calls to the browser using each browser’s native
support for automation. “
So we can say that the way chrome browser allows a click on a web element may be
different from other browsers.
So, JavaScript could click on a web element which may not be clickable by WebDriver
API. So use WebDriver click as much as possible and go for JavaScript click when
WebDriver click does not work.
WebDriver APIs are designed to simulate the actions which a real user will perform
on front end of application. When we go for JavaScript, this is not taking into
consideration. JavaScript ways may not catch the bugs which is main goal of
automation scripting.
JavaScript operations may not call actions sometimes. Suppose when you click on a
button, some popup should appear. That can be relate as onClick event. You may
encounter these issues. These issues will not come if you use WebDriver APIs.
I would like to share a problem I faced while automating. There was a Date of birth
field which takes input from user and calculates actual age. I typed DOB using
Javascript way and it was not calculating actual age and event set on DOB field was
not called.
------------------------------------------------------------------------------
how to take Scrren shot of whole page
// Down casting driver to JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor) driver;
// It returns height of view part. You can say it as page height. When
you click on page down key
// Page scroll by one page.
long pageHeight= (long)js.executeScript("return window.innerHeight");
System.out.println("Page height: "+pageHeight);
// It is how much you can scroll. It is height if you scroll, you will
reach to bottom of page.
long scrollableHeight= (long)js.executeScript("return
document.body.scrollHeight");
System.out.println("Total scrollable height: "+scrollableHeight);
-----------------------------------------------
how to set calender date if it allows you to put date manually as well as using
calender
public static void setDateUsingJavaScriptInCalendar(WebDriver driver, String value,
WebElement calLocator)
{
JavascriptExecutor jse= (JavascriptExecutor)driver;
String script= "arguments[0].setAttribute('value','"+value+"');";
jse.executeScript(script, calLocator);
}
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/resources/demos/datepicker/dropdown-
month-year.html");
HandlingAnyCalendarUsingJavaScript.setDateUsingJavaScriptInCalendar(driver,"10/09/2
017",departCal);