Tuesday, 27 November 2012

Wait for an AJAX call to complete with Selenium 2 WebDriver


private ExpectedCondition WaitForAjax(final long timeout) {
return new ExpectedCondition() {
public Boolean apply(WebDriver driver) {
final long startTime = System.currentTimeMillis();
final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
while ((startTime + timeout) >= System.currentTimeMillis()) {
final Boolean scriptResult = (Boolean) javascriptExecutor.executeScript("return jQuery.active == 0");
if (scriptResult)
return true;
delay(100);
}
return false;
}
};
}
private void delay(final long amount) {
try {
Thread.sleep(amount);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
view raw gistfile1.java hosted with ❤ by GitHub

here is a sample to use this method:

public class SeleniumTest {
private final static String BASE_URL = "http://localhost:8080";
private final static WebDriver driver = new FirefoxDriver();
@Before
public void startUp() {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().setPosition(new Point(0, 0));
driver.manage().window().setSize(new Dimension(800, 600));
}
@Test
public void testMyPage() {
final Wait wait = new WebDriverWait(driver, 60);
driver.get(BASE_URL + "/myweb/");
boolean isListPopulated = wait.until(WaitForAjax(60000));
Assert.assertTrue(isListPopulated);
}
@After
public void tearDown() {
// Close the browser
driver.quit();
}
}

Friday, 23 November 2012

JSF - Show content only if Messages is empty

<h:panelgroup rendered="#{facesContext.validationFailed or empty facesContext.messageList}">



. . .

</h:panelgroup>

Thursday, 15 November 2012

Import a UTF-8 SQL file into MySQL Server

importing a SQLFile into MySQL works only if the .sql file itself is encoded with default (ISO-????) character set. But if you have file with UTF-8 encoding, it won't work. Saving the file in UTF-8 encoding will not work as well, since mysql doesn't know which encoding is enabled and interprets it as default encoding. Using this command will tell to MySQL to use UTF-8 encoding while importing the Data:

>> mysql -h host -u username -ppassword --default_character_set=utf8 database < myfile.sql