JavaScript Selenium Webdriver

 /*Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript(Script,Arguments);
script - The JavaScript to execute
Arguments - The arguments to the script.(Optional)*/
 
                JavascriptExecutor js = (JavascriptExecutor)driver;
                //Uncomment each scenario by using Ctrl + Shift + \ (backslash) and find the solution
 without using sendKeys()

  *//to type text in Selenium WebDriver without using sendKeys() method
   js.executeScript("document.getElementById('some id').value='someValue';");
                js.executeScript("document.getElementById('Email').value='SoftwareTestingMaterial.com';");*/
 
click a button
/*//to click a button in Selenium WebDriver using JavaScript
 //js.executeScript("arguments[0].click();", loginButton);
                //or
 js.executeScript("document.getElementById('enter your element id').click();");
js.executeScript("document.getElementById('next').click();");*/

handle checkbox
 
                /*//to handle checkbox
 js.executeScript("document.getElementById('enter element id').checked=false;");*/
 
Alert Pop window
/*//to generate Alert Pop window in selenium
js.executeScript("alert('hello world');");*/
 
refresh browser
/*//to refresh browser window using Javascript 
js.executeScript("history.go(0)");*/
get innertext of the entire webpage
 
/*// to get innertext of the entire webpage in Selenium
String sText =  js.executeScript("return document.documentElement.innerText;").toString();
System.out.println(sText);*/
 
 get the Title of our webpage
/*//to get the Title of our webpage
String sText =  js.executeScript("return document.title;").toString();
System.out.println(sText);*/
 
to get the domain
String sText =  js.executeScript("return document.domain;").toString();
System.out.println(sText);*/
 
get the URL of our webpage
/*//to get the URL of our webpage
String sText =  js.executeScript("return document.URL;").toString();
System.out.println(sText);*/
Scroll on application  

/*//to perform Scroll on application using  Selenium
//Vertical scroll - down by 50  pixels
js.executeScript("window.scrollBy(0,50)");

scrolling till the bottom of the page
 
// for scrolling till the bottom of the page we can use the code like
//js.executeScript("window.scrollBy(0,document.body.scrollHeight)");*/

click on a SubMenu 
 
/* // to click on a SubMenu which is only visible on mouse hover on Menu?
//Hover on Automation Menu on the MenuBar
         js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");*/

navigate to different page 
 
/*//to navigate to different page using Javascript?
         //Navigate to new Page
         js.executeScript("window.location = 'https://www.softwaretestingmaterial.com");*/

Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium