Javascript is the only programming language that can be used for both frontend and backend development, hence it is safe to say that it is the “programming language of the web” as 100% of responsive websites use Javascript. We use Selenium to do automated testing of web apps or websites or to just automate the web browser. However, the Selenium testing framework is built with Java, C#, Ruby, and Python.
For specific test case scenarios like automatic scrolling and waiting on page load, there is a need for us to write custom Javascript code within Selenium. To achieve this, we typically have to use Python to run Javascript within the Selenium webdriver using the Javascript executor method. The Document Object Model communicates with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking in the argument in the execute_script method (the commands to be executed are passed as arguments to the method).
Upon page load, sample code to create the alert “Page Loaded successfully” can be found below:
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www-stage.testup.io/documentation/") # to scroll till page bottom driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www-stage.testup.io/documentation/") # to scroll till page bottom driver.execute_script("alert('Page Loaded successfully')")
Asynchronous Javascript
Asynchronous Javascript functions typically use the “async” and “await” keywords to categorize functions as asynchronous and wait for the function to run respectively. Async Javascript is typically used for API calls to a database or general CRUD functions.
To run asynchronous Javascript functions in Selenium, you would need to run the executeAsyncScript() method which takes in both the Javascript function you would like to run and the specific wait time by which the function is expected to have been run. This wait time is usually less than 5 seconds (or 5000 milliseconds).
An example code implementation to sleep a browser after 5 seconds of visiting Testup.io can be found below:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class JavaSE_Test { @Test public void Login() { WebDriver driver= new FirefoxDriver(); //Creating the JavascriptExecutor interface object by Type casting JavascriptExecutor js = (JavascriptExecutor)driver; //Launching the Site. driver.get("https://www-stage.testup.io/ /"); //Maximize window driver.manage().window().maximize(); //Set the Script Timeout to 20 seconds driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS); //Declare and set the start time long start_time = System.currentTimeMillis(); //Call executeAsyncScript() method to wait for 5 seconds js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);"); //Get the difference (currentTime - startTime) of times. System.out.println("Passed time: " + (System.currentTimeMillis() - start_time)); } }