This page is hosted for free by zzz.com.ua, if you are owner of this page, you can remove this message and gain access to many additional features by upgrading your hosting to PRO or VIP for just 41.60 UAH.
Do you want to support owner of this site? Click here and donate to his account some amount, he will be able to use it to pay for any of our services, including removing this ad.

JS in JAVA tests

Some times we need to do something which impossible to do using java(Or possible but really hard).

JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.

1. Problem for me was “How to scrolling inside java test?”. An here the answer

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0,3500);");//Scroll down

2. Problem was “How to hover cursor on element?”

WebElement send_hover = driver.findElement(By.xpath(Variables.SEND_SERVICES_FORM.toString()));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", send_hover);
send.click();

3. How to generate Alert Pop window in selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('hello world');");

4. How to click a button in Selenium WebDriver using JavaScript?

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

5. How to click on a SubMenu which is only visible on mouse hover on Menu?

JavascriptExecutor js = (JavascriptExecutor)driver;
    //Hover on Automation Menu on the MenuBar
    js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");

Full first test



import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class JavaScriptExecuter {

 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  //Launching the browser application
  driver.get("http://www.uftHelp.com");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  //Maximize window
  driver.manage().window().maximize();
  //Creating the Javascriptexecutor interface object by Type casting
   JavascriptExecutor js = (JavascriptExecutor)driver;
  //Fetching the Domain Name 
  String sDomain = js.executeScript("return document.domain;").toString();
  System.out.println("Domain = "+sDomain);
  //Fetching the URL 
  String sURL = js.executeScript("return document.URL;").toString();
  System.out.println("URL = "+sURL);
  //Fetching the Title
  String sTitle = js.executeScript("return document.title;").toString();
  System.out.println("Title = "+sTitle);
  //Vertical scroll - down by 200  pixels
  js.executeScript("window.scrollBy(0,200)");
  System.out.println("Successfully did the vertical scroll by 200px");
 
 }

}

And full second test

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import static Enums.Variables.SEND_SERVICES_FORM;
public class Services extends Settings {
    private static Button services_link = new Button(By.xpath(Variables.OPEN_MENU_SERVISES.toString()));
    private static Button contact_us= new Button(By.xpath(Variables.CONTACTS_LINK.toString()));
    private static Button send=new Button(By.xpath(SEND_SERVICES_FORM.toString()));
    private static TextField name = new TextField(By.xpath(Variables.NAME_SERVICES_FORM.toString()));
    private static TextField email = new TextField(By.xpath(Variables.EMAIL_SERVICES_FORM.toString()));
    private static TextField phone = new TextField(By.xpath(Variables.PHONE_SERVICES_FORM.toString()));
    private static TextField description=new TextField(By.xpath(Variables.DESCRIPTION_SERVICES_FORM.toString()));
    private static Button custom=new Button(By.xpath(Variables.CUSTOM_SERVICES.toString()));
    private static Button close_subscription_popup= new Button(By.xpath(Variables.SUBSCRIPTION_POPUP.toString()));
    public static Services get_request_from_services_page()
    {
        contact_us.click();
        services_link.click();
        Settings.waitInSeconds(2);
        custom.click();
        Settings.waitInSeconds(15);
        close_subscription_popup.click();
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("scroll(0,3500);");//Scroll down
        name.enterText("DDI TEST NAME");
        phone.enterText("17183559302");
        email.enterText("test@ddi-dev.com");
        description.enterText("Automation test of services form");
        try {
            WebElement send_hover = driver.findElement(By.xpath(Variables.SEND_SERVICES_FORM.toString()));
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", send_hover);
            send.click();
        }
        catch (ElementNotVisibleException e)
        {
            Assert.fail("button is not visible it is impossible to hover on it");
        }
        return new Services();
    }
}