Skip to content

Instantly share code, notes, and snippets.

@huguogang
huguogang / SJTXE.java
Last active December 25, 2015 06:09
check runtime script error for Selenium WebDriver
//make sure it's run after every page load
public static void injectSJTXE(WebDriver driver) throws IOException {
URL url;
String js;
url = Resources.getResource(ExtJSUtil.class, "SJTXE.js");
js = Resources.toString(url, Charsets.UTF_8);
((JavascriptExecutor) driver).executeScript(js);
}
//code to check if there are ExtJS Ajax error
@huguogang
huguogang / JSError.js
Created October 11, 2013 05:12
Capture Javascript error for Selenium WebDriver
//Client side utilities for Selenium tests of ExtJS web applications
//requires ExtJS and Underscore libraries
//SJTXE - Selenium JavaScript Testing eXtension for ExtJS
(function(root, Ext) {
var root = root || window;
var me = root;
var Ext = Ext;
//private vars
var hasJSError = false;
@huguogang
huguogang / SendKeys.java
Created October 1, 2013 06:46
send keys to ExtJS combobox, textbox, datefield, ...
//find the fully qualified component query of ExtJS component cmp
String query = cmp.componentQuery;
Component parent = cmp.parent;
while (parent != null) {
query = parent.componentQuery + " " + query;
parent = parent.parent;
}
//use component query to find id of inputEl
//the only change here is the addition of inputEl before .id
@huguogang
huguogang / SJTXE.java
Last active December 24, 2015 08:59
Java code to inject javascript and check for ExtJS Ajax error
//make sure it's run after every page load
public static void injectSJTXE(WebDriver driver) throws IOException {
URL url;
String js;
url = Resources.getResource(ExtJSUtil.class, "SJTXE.js");
js = Resources.toString(url, Charsets.UTF_8);
((JavascriptExecutor) driver).executeScript(js);
}
//code to check if there are ExtJS Ajax error
@huguogang
huguogang / ExtJSError.js
Created October 1, 2013 06:26
Client side script for SJTXE (Selenium JavaScript Testing eXtension for ExtJS)
//Client side utilities for Selenium tests of ExtJS web applications
//requires ExtJS and Underscore libraries
//SJTXE - Selenium JavaScript Testing eXtension for ExtJS
(function(root, Ext) {
var root = root || window;
var me = root;
var Ext = Ext;
//private vars
var hasAjaxFailure = false;
//this example assuming storeID is known
ArrayList<Object> storeData = (ArrayList<Object>) ((JavascriptExecutor) driver)
.executeScript("return _.pluck(Ext.StoreManager.get(storeID).getRange(), 'data');", storeID);
//get number of rows in the store's data
int numberOfRows = storeData.size();
//get the first row, you are going to see a JSON string on Java console
System.out.println(storeData.get(0));
//get the first row's value for the column "name"
String name = ((Map<String, Object>) storeData.get(0)).get("name").toString();
public waitForAjax(WebDriver driver, int timeOutInSeconds) {
(new WebDriverWait(driver, timeOutInSeconds))
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (Boolean) ((JavascriptExecutor) d)
.executeScript("return !(ExtJSUtil.hasActiveAjaxCalls(Ext.Ajax.requests && !_.isEmpty(Ext.Ajax.requests));");
}
});
}
@huguogang
huguogang / ExtComponentQuery.java
Created September 27, 2013 01:48
Finding ExtJS component for Selenium WebDriver
//find the fully qualified component query of ExtJS component cmp
String query = cmp.componentQuery;
Component parent = cmp.parent;
while (parent != null) {
query = parent.componentQuery + " " + query;
parent = parent.parent;
}
//use component query to find id
String js = "return Ext.ComponentQuery.query(\"" + query + "\")[0].id;";
@Rule
public TakeScreenshotOnFailRule rule = new TakeScreenshotOnFailRule(driver, folderName);
@huguogang
huguogang / TakeScreenshotOnFailRule.java
Last active December 23, 2015 23:18
JUnit test rule to take screenshot using Selenium Web Driver when test failed
public class TakeScreenshotOnFailRule extends TestWatcher {
protected String _folder;
protected WebDriver _driver;
public TestRule(WebDriver driver, String folder) {
_folder = folder;
_driver = driver;
}
@Override
protected void failed(Throwable e, Description description) {