Selenium Webdriver wait for JavaScript JQuery and Angular
Hi all, during the last two weeks I was dealing with the best solution to wait for both JQuery, Angular and JavaScript (JS) in my Selenium (Java) test codes and finally, I found a significantly stable solution without silly sleep() statements to share with you. In this period, I searched maybe more than 20 articles and sites to find best and unique solutions and in this article I combined all solutions in only one method. This is “WaitJQueryAngular()“. When you need to wait for asynchronous wait, you can use below methods as your project type.
I know that you wonder to know what it does? First, it waits JQuery is Ready? and then waits Angular is Ready? and if JQuery or Angular is ready? Then it checks JS is ready? If all of them are ready then the code will continue to run next statements. Also, I added a control that checks JQuery and Angular are defined or not. It is important that if the any of them is not defined in the page, code skips asynchronous wait statements. If this control does not exist, we will get JS error.
You can just add below methods in your code and use “WaitJQueryAngular()” for an asynchronous wait in your test automation codes.
- Add below JSWaiter utility class into your project.
- Then, send WebDriver object to JSWaiter Class in a convenient place in your project by adding this line: “JSWaiter.setDriver(driver);”
- Finally, you can use any of the methods below in you test code:
- JSWaiter.waitJQueryAngular(); –> Both waits JQuery, Angular, JS
- waitUntilAngularReady(); –> It only waits Angular and JS
- waitUntilJQueryReady(); –> It only waits JQuery and JS
Note: I added waitUntilJSReady(); call both in waitUntilJQueryReady() and waitUntilAngularReady() methods. In this way, if your test site only comprises of Angular or JQuery, you can just use the relevant method to wait asynchronous operations.
Note: You should combine this asynchronous wait solution with the synchronous explicit wait statements to get highest stability in your tests. If you face with a problem, please let me know.
Github Link of a Sample Project: https://github.com/swtestacademy/JSWaiter
JQuery, Angular, and JavaScript Waiter
public class JSWaiter { private static WebDriver jsWaitDriver;
private static WebDriverWait jsWait;
private static JavascriptExecutor jsExec; //Get the driver
public static void setDriver (WebDriver driver) {
jsWaitDriver = driver;
jsWait = new WebDriverWait(jsWaitDriver, 10);
jsExec = (JavascriptExecutor) jsWaitDriver;
} //Wait for JQuery Load
public static void waitForJQueryLoad() {
//Wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver)
.executeScript("return jQuery.active") == 0); //Get JQuery is Ready
boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0"); //Wait JQuery until it is Ready!
if(!jqueryReady) {
System.out.println("JQuery is NOT Ready!");
//Wait for jQuery to load
jsWait.until(jQueryLoad);
} else {
System.out.println("JQuery is Ready!");
}
} //Wait for Angular Load
public static void waitForAngularLoad() {
WebDriverWait wait = new WebDriverWait(jsWaitDriver,15);
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0"; //Wait for ANGULAR to load
ExpectedCondition<Boolean> angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver)
.executeScript(angularReadyScript).toString()); //Get Angular is Ready
boolean angularReady = Boolean.valueOf(jsExec.executeScript(angularReadyScript).toString()); //Wait ANGULAR until it is Ready!
if(!angularReady) {
System.out.println("ANGULAR is NOT Ready!");
//Wait for Angular to load
wait.until(angularLoad);
} else {
System.out.println("ANGULAR is Ready!");
}
} //Wait Until JS Ready
public static void waitUntilJSReady() {
WebDriverWait wait = new WebDriverWait(jsWaitDriver,15);
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; //Wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) jsWaitDriver)
.executeScript("return document.readyState").toString().equals("complete"); //Get JS is Ready
boolean jsReady = (Boolean) jsExec.executeScript("return document.readyState").toString().equals("complete"); //Wait Javascript until it is Ready!
if(!jsReady) {
System.out.println("JS in NOT Ready!");
//Wait for Javascript to load
wait.until(jsLoad);
} else {
System.out.println("JS is Ready!");
}
} //Wait Until JQuery and JS Ready
public static void waitUntilJQueryReady() {
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; //First check that JQuery is defined on the page. If it is, then wait AJAX
Boolean jQueryDefined = (Boolean) jsExec.executeScript("return typeof jQuery != 'undefined'");
if (jQueryDefined == true) {
//Pre Wait for stability (Optional)
sleep(20); //Wait JQuery Load
waitForJQueryLoad(); //Wait JS Load
waitUntilJSReady(); //Post Wait for stability (Optional)
sleep(20);
} else {
System.out.println("jQuery is not defined on this site!");
}
} //Wait Until Angular and JS Ready
public static void waitUntilAngularReady() {
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; //First check that ANGULAR is defined on the page. If it is, then wait ANGULAR
Boolean angularUnDefined = (Boolean) jsExec.executeScript("return window.angular === undefined");
if (!angularUnDefined) {
Boolean angularInjectorUnDefined = (Boolean) jsExec.executeScript("return angular.element(document).injector() === undefined");
if(!angularInjectorUnDefined) {
//Pre Wait for stability (Optional)
sleep(20); //Wait Angular Load
waitForAngularLoad(); //Wait JS Load
waitUntilJSReady(); //Post Wait for stability (Optional)
sleep(20);
} else {
System.out.println("Angular injector is not defined on this site!");
}
} else {
System.out.println("Angular is not defined on this site!");
}
} //Wait Until JQuery Angular and JS is ready
public static void waitJQueryAngular() {
waitUntilJQueryReady();
waitUntilAngularReady();
} public static void sleep (Integer seconds) {
long secondsLong = (long) seconds;
try {
Thread.sleep(secondsLong);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Selenium Webdriver wait for JavaScript JQuery and Angular的更多相关文章
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
跟你说,你总是靠那个firebug,chrome的F12啥的右击复制xpath绝对总有一天踩着地雷炸的你死活定位不到,这个时候就需要自己学会动手写xpath,人脑总比电脑聪明,开始把xpath语法给我 ...
- 转:selenium webdriver 执行javascript代码
在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代码,selenium本身就支持执行js,我们在代码中import org.openqa ...
- selenium webdriver ——执行javascript代码
在代码中import org.openqa.selenium.JavascriptExecutor;就可以使用executeScript.executeAsyncScript这两个方法了 execut ...
- selenium webdriver——JavaScript警告窗处理
在WebDriver中处理JavaScript所生成的alert.confirm以及prompt,具体方法是使用switch_to_alert()方法定位到alert.confirm以及 prompt ...
- [selenium webdriver Java]使用自定义条件同步测试
Selenium WebDriver可以结合ExpectedCondition类来定义自己期望的条件 创建一个新的ExpectedCondition接口,必须实现apply方法 等待元素出现 publ ...
- selenium webdriver (python)的基本用法一
阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...
- selenium webdriver (python)大全
webdriver的简介 硒2.0的主要新功能是集成的webdriver的API.webdriver的设计除了解决一些seleniumr-RC API的一些限制,与webdriver 的整合,将提供一 ...
- Selenium WebDriver(Python)API
1.通过示例介绍Selenium-WebDriver 一个简单的入门方法就是这个例子,它在Google上搜索术语“Cheese”,然后将结果页面的标题输出到控制台. java csharp pytho ...
- 深入理解jQuery、Angular、node中的Promise
最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...
随机推荐
- opencv之模糊处理
初学OpenCV的开发者很容易被OpenCV中各种滤波方法所困扰,不知道到底该用哪里一个来做滤波.表面原因看起来是因为OpenCV中各种滤波方式实在是太多太杂, 其背后原因是对各种滤波方法的应用场景认 ...
- hessian 在spring中的使用 (bean 如 Dao无法注入的问题)
hessian的主要结构分客户端与服务端,中间基于http传输.客户端主要做的事情是把对远程接口调用序列化为流,并传输到服务端:服务端主要做的事情是把传输过来的流反序列化为对服务的请求,调用相应服务后 ...
- Java多线程-----volatile关键字详解
volatile原理 Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为volatile类型后, 编译器与运行时都会注意 ...
- 大数据处理框架之Strom:redis storm 整合
storm 引入redis ,主要是使用redis缓存库暂存storm的计算结果,然后redis供其他应用调用取出数据. 新建maven工程 pom.xml <project xmlns=&qu ...
- AmiGO2:在线浏览和查询GO信息的利器
GO数据库的信息是非常庞大的,为了有效的检索和浏览GO数据库的信息,官方提供了AmiGO, 可以方便的浏览,查询和下载对应信息,官网如下 http://amigo.geneontology.org/a ...
- ad 原件布局布线基本规则
一.原件布局基本规则 1.按照电路模块进行布局,电路中的元件应该采用集中就近原则,同时数字电路和模拟电路分开: 2.定位孔.标准孔等周围1.27mm内不得贴元器件,安装孔周围3.5mm不得特装元件 3 ...
- XWIKI部署安装
http://www.linuxidc.com/Linux/2016-08/134408.htm
- webService入门理解
最近可能开始要搞关于远程接口调用的玩意儿,所以上网查了一些关于远程调用额东西,其中有很多写得很不错,我把其中的比较好的几个整理一下,整理到一块儿,变成个人的理解写出来. 关于所谓的webService ...
- linux 系统监控和进程管理
1.命令top,查看cpu和内存使用,主要进程列表和占用资源. 2.内存使用命令foree -g 3.查询所有java进程:pgrep -l java ------ps aux|grep .j ...
- C#中对Web.Config、App.Config字符串加密与解密的方法
我们平常的项目里面的配置文件通常都是明文形式的存在,现在就是为了项目安全性增强,同时又显得高逼格点, 我们可以采用加密的方式,而我们C#很强大,因为他内置的一些指令方式,很方便而且使用起来还不用解密, ...