Use CasperJS withFrame method to simplify the code.