26
Jul

Javascript alternates to “Thread.sleep(x)

Javascript wait function inplace of "Thread.sleep()"
=====================================================


package examples;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class jack {

	@Test
	public void browserWait() throws MalformedURLException {
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		String node_host = "10.174.88.159";
		String node_port = "4444";
		ChromeOptions options = new ChromeOptions();
		options.setCapability(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);
		options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
		options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
		options.addArguments("--start-maximized");
		options.addArguments("disable-infobars");

		String node_URL = "http://" + node_host + ":" + node_port + "/wd/hub";
		WebDriver driver = new RemoteWebDriver(new URL(node_URL), options);
		driver.get("https://google.com");
		// ------------ Javascript function for wait till 500 milli seconds
		long start = System.currentTimeMillis();
		((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
		System.out.println("Elapsed time: " + (System.currentTimeMillis() - start));

		// ------------
		driver.quit();

	}

}