Use of javascript in selenium



Use of Javascript


       JavascriptExecutor js = (JavascriptExecutor)driver;


//Call executeAsyncScript() method to wait for 5 seconds  
          js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");



//Perform Click on LOGIN button using JavascriptExecutor  
        js.executeScript("arguments[0].click();", button);


//Click on dynamic element.
int b=0;
 String elementToClick="arguments["+i+"].click();";
 js.executeScript(elementToClick, links);


//Fetching the Domain Name of the site. Tostring() change object to name.  
        String DomainName = js.executeScript("return document.domain;").toString


//Fetching the URL of the site. Tostring() change object to name  
        String url = js.executeScript("return document.URL;").toString();


//Method document.title fetch the Title name of the site. Tostring() change object to name  
       String TitleName = js.executeScript("return document.title;").toString();


//Navigate to new Page i.e to generate access page. (launch new url)  
      js.executeScript("window.location = 'http://demo.guru99.com/'");


//Vertical scroll down by 600  pixels  
        js.executeScript("window.scrollBy(0,600)");


//Scroll till element
jse.executeScript("arguments[0].scrollIntoView(true);",ElementName);


//Send key
js.executeScript(“document.getElementsById(‘some_id’).value=’Yogendrasingh’;”);

js.executeScript("arguments[0].value='Yogendrasingh';", element);


//is element check
js.executeScript("document.getElementById('enter element id').checked=false;");


//To generate Alert Pop window in selenium 
js.executeScript("alert('Welcome To SoftwareTestingMaterial');");


//To refresh browser window using Javascript
js.executeScript("history.go(0)");


//To refresh browser window using Javascript 
js.executeScript("window.location = 'https://www.softwaretestingmaterial.com");

No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...