casper.js 87.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
/*!
 * Casper is a navigation utility for PhantomJS.
 *
 * Documentation: http://casperjs.org/
 * Repository:    http://github.com/n1k0/casperjs
 *
 * Copyright (c) 2011-2012 Nicolas Perriault
 *
 * Part of source code is Copyright Joyent, Inc. and other Node contributors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

/*global CasperError, console, exports, phantom, __utils__, patchRequire, require:true*/

var require = patchRequire(require);
var colorizer = require('colorizer');
var events = require('events');
var fs = require('fs');
var http = require('http');
var mouse = require('mouse');
var pagestack = require('pagestack');
var qs = require('querystring');
var tester = require('tester');
var utils = require('utils');
var f = utils.format;


var defaultUserAgent = phantom.defaultPageSettings.userAgent
    .replace(/(PhantomJS|SlimerJS)/, f("CasperJS/%s", phantom.casperVersion) + '+$&');

exports.create = function create(options) {
    "use strict";
    // This is a bit of a hack to check if one is trying to override the preconfigured
    // casper instance from within a test environment.
    if (phantom.casperTest && window.casper) {
        console.error("Fatal: you can't override the preconfigured casper instance in a test environment.");
        console.error("Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options");
        phantom.exit(1);
    }
    return new Casper(options);
};

/**
 * Shortcut to build an XPath selector object.
 *
 * @param  String  expression  The XPath expression
 * @return Object
 * @see    http://casperjs.org/selectors.html
 */
function selectXPath(expression) {
    "use strict";
    return {
        type: 'xpath',
        path: expression,
        toString: function() {
            return this.type + ' selector: ' + this.path;
        }
    };
}
exports.selectXPath = selectXPath;

/**
 * Main Casper object.
 *
 * @param  Object  options  Casper options
 */
var Casper = function Casper(options) {
    "use strict";
    /*jshint maxstatements:40*/
    // init & checks
    if (!(this instanceof Casper)) {
        return new Casper(options);
    }
    // default options
    this.defaults = {
        clientScripts:       [],
        colorizerType:       'Colorizer',
        exitOnError:         true,
        logLevel:            "error",
        httpStatusHandlers:  {},
        safeLogs:            true,
        onAlert:             null,
        onDie:               null,
        onError:             null,
        onLoadError:         null,
        onPageInitialized:   null,
        onResourceReceived:  null,
        onResourceRequested: null,
        onRunComplete:       function _onRunComplete() {
            this.exit();
        },
        onStepComplete:      null,
        onStepTimeout:       function _onStepTimeout(timeout, stepNum) {
            this.die("Maximum step execution timeout exceeded for step " + stepNum);
        },
        onTimeout:           function _onTimeout(timeout) {
            this.die(f("Script timeout of %dms reached, exiting.", timeout));
        },
        onWaitTimeout:       function _onWaitTimeout(timeout) {
            this.die(f("Wait timeout of %dms expired, exiting.", timeout));
        },
        page:                null,
        pageSettings:        {
            localToRemoteUrlAccessEnabled: true,
            userAgent:                     defaultUserAgent
        },
        remoteScripts:       [],
        silentErrors:        false,
        stepTimeout:         null,
        timeout:             null,
        verbose:             false,
        retryTimeout:        20,
        waitTimeout:         5000,
        clipRect : null,
        viewportSize : null
    };
    // options
    this.options = utils.mergeObjects(this.defaults, options);
    // factories
    this.cli = phantom.casperArgs;
    this.options.logLevel = this.cli.get('log-level', this.options.logLevel);
    if (!this.options.verbose) {
        this.options.verbose = this.cli.has('direct') || this.cli.has('verbose');
    }
    this.colorizer = this.getColorizer();
    this.mouse = mouse.create(this);
    this.popups = pagestack.create();
    // properties
    this.checker = null;
    this.currentResponse = {};
    this.currentUrl = 'about:blank';
    this.currentHTTPStatus = null;
    this.history = [];
    this.loadInProgress = false;
    this.navigationRequested = false;
    this.browserInitializing = false;
    this.logFormats = {};
    this.logLevels = ["debug", "info", "warning", "error"];
    this.logStyles = {
        debug:   'INFO',
        info:    'PARAMETER',
        warning: 'COMMENT',
        error:   'ERROR'
    };
    this.page = null;
    this.pendingWait = false;
    this.requestUrl = 'about:blank';
    this.resources = [];
    this.result = {
        log:    [],
        status: "success",
        time:   0
    };
    this.started = false;
    this.step = -1;
    this.steps = [];
    this.waiters = [];
    this._test = undefined;
    this.__defineGetter__('test', function() {
        if (!phantom.casperTest) {
            throw new CasperError('casper.test property is only available using the `casperjs test` command');
        }
        if (!utils.isObject(this._test)) {
            this._test = tester.create(this);
        }
        return this._test;
    });

    // init phantomjs error handler
    this.initErrorHandler();

    this.on('error', function(msg, backtrace) {
        var c = this.getColorizer();
        var match = /^(.*): __mod_error(.*):: (.*)/.exec(msg);
        var notices = [];
        if (match && match.length === 4) {
            notices.push('  in module ' + match[2]);
            msg = match[3];
        }
        console.error(c.colorize(msg, 'RED_BAR', 80));
        notices.forEach(function(notice) {
            console.error(c.colorize(notice, 'COMMENT'));
        });
        (backtrace || []).forEach(function(item) {
            var message = fs.absolute(item.file) + ":" + c.colorize(item.line, "COMMENT");
            if (item['function']) {
                message += " in " + c.colorize(item['function'], "PARAMETER");
            }
            console.error("  " + message);
        });
    });

    // deprecated feature event handler
    this.on('deprecated', function onDeprecated(message) {
        this.warn('[deprecated] ' + message);
    });

    // dispatching an event when instance has been constructed
    this.emit('init');

    // deprecated direct option
    if (this.cli.has('direct')) {
        this.emit("deprecated", "--direct option has been deprecated since 1.1; you should use --verbose instead.")
    }
};

// Casper class is an EventEmitter
utils.inherits(Casper, events.EventEmitter);

/**
 * Go a step back in browser's history
 *
 * @return Casper
 */
Casper.prototype.back = function back() {
    "use strict";
    this.checkStarted();
    return this.then(function _step() {
        this.emit('back');
        this.evaluate(function _evaluate() {
            history.back();
        });
    });
};

/**
 * Encodes a resource using the base64 algorithm synchronously using
 * client-side XMLHttpRequest.
 *
 * NOTE: we cannot use window.btoa() for some strange reasons here.
 *
 * @param  String  url     The url to download
 * @param  String  method  The method to use, optional: default GET
 * @param  String  data    The data to send, optional
 * @return string          Base64 encoded result
 */
Casper.prototype.base64encode = function base64encode(url, method, data) {
    "use strict";
    return this.callUtils("getBase64", url, method, data);
};

/**
 * Bypasses `nb` steps.
 *
 * @param  Integer  nb  Number of steps to bypass
 */
Casper.prototype.bypass = function bypass(nb) {
    "use strict";
    var step = this.step,
        steps = this.steps,
        last = steps.length;
    this.checkStarted();
    this.step = Math.min(step + nb, last);
    return this;
};

/**
 * Invokes a client side utils object method within the remote page, with arguments.
 *
 * @param  {String}   method  Method name
 * @return {...args}          Arguments
 * @return {Mixed}
 * @throws {CasperError}      If invokation failed.
 */
Casper.prototype.callUtils = function callUtils(method) {
    "use strict";
    var args = [].slice.call(arguments, 1);
    var result = this.evaluate(function(method, args) {
        return __utils__.__call(method, args);
    }, method, args);
    if (utils.isObject(result) && result.__isCallError) {
        throw new CasperError(f("callUtils(%s) with args %s thrown an error: %s",
                              method, args, result.message));
    }
    return result;
};

/**
 * Proxy method for WebPage#render. Adds a clipRect parameter for
 * automatically set page clipRect setting values and sets it back once
 * done. If the cliprect parameter is omitted, the full page viewport
 * area will be rendered.
 *
 * @param  String  targetFile  A target filename
 * @param  mixed   clipRect    An optional clipRect object (optional)
 * @return Casper
 */
Casper.prototype.capture = function capture(targetFile, clipRect, imgOptions) {
    "use strict";
    /*jshint maxstatements:20*/
    this.checkStarted();
    var previousClipRect;
    targetFile = fs.absolute(targetFile);
    if (clipRect) {
        if (!utils.isClipRect(clipRect)) {
            throw new CasperError("clipRect must be a valid ClipRect object.");
        }
        previousClipRect = this.page.clipRect;
        this.page.clipRect = clipRect;
        this.log(f("Capturing page to %s with clipRect %s", targetFile, JSON.stringify(clipRect)), "debug");
    } else {
        this.log(f("Capturing page to %s", targetFile), "debug");
    }
    if (!this.page.render(this.filter('capture.target_filename', targetFile) || targetFile, imgOptions)) {
        this.log(f("Failed to save screenshot to %s; please check permissions", targetFile), "error");
    } else {
        this.log(f("Capture saved to %s", targetFile), "info");
        this.emit('capture.saved', targetFile);
    }
    if (previousClipRect) {
        this.page.clipRect = previousClipRect;
    }
    return this;
};

/**
 * Returns a Base64 representation of a binary image capture of the current
 * page, or an area within the page, in a given format.
 *
 * Supported image formats are `bmp`, `jpg`, `jpeg`, `png`, `ppm`, `tiff`,
 * `xbm` and `xpm`.
 *
 * @param  String                   format    The image format
 * @param  String|Object|undefined  selector  DOM CSS3/XPath selector or clipRect object (optional)
 * @return Casper
 */
Casper.prototype.captureBase64 = function captureBase64(format, area) {
    "use strict";
    /*jshint maxstatements:20*/
    this.checkStarted();
    var base64, previousClipRect, formats = ['bmp', 'jpg', 'jpeg', 'png', 'ppm', 'tiff', 'xbm', 'xpm'];
    if (formats.indexOf(format.toLowerCase()) === -1) {
        throw new CasperError(f('Unsupported format "%s"', format));
    }
    if (utils.isClipRect(area)) {
        // if area is a clipRect object
        this.log(f("Capturing base64 %s representation of %s", format, utils.serialize(area)), "debug");
        previousClipRect = this.page.clipRect;
        this.page.clipRect = area;
        base64 = this.page.renderBase64(format);
    } else if (utils.isValidSelector(area)) {
        // if area is a selector string or object
        this.log(f("Capturing base64 %s representation of %s", format, area), "debug");
        base64 = this.captureBase64(format, this.getElementBounds(area));
    } else {
        // whole page capture
        this.log(f("Capturing base64 %s representation of page", format), "debug");
        base64 = this.page.renderBase64(format);
    }
    if (previousClipRect) {
        this.page.clipRect = previousClipRect;
    }
    return base64;
};

/**
 * Captures the page area matching the provided selector.
 *
 * @param  String  targetFile  Target destination file path.
 * @param  String  selector    DOM CSS3/XPath selector
 * @return Casper
 */
Casper.prototype.captureSelector = function captureSelector(targetFile, selector, imgOptions) {
    "use strict";
    return this.capture(targetFile, this.getElementBounds(selector), imgOptions);
};

/**
 * Checks for any further navigation step to process.
 *
 * @param  Casper    self        A self reference
 * @param  function  onComplete  An options callback to apply on completion
 */
Casper.prototype.checkStep = function checkStep(self, onComplete) {
    "use strict";
    if (self.pendingWait || self.loadInProgress || self.navigationRequested || self.browserInitializing) {
        return;
    }
    var step = self.steps[self.step++];
    if (utils.isFunction(step)) {
        return self.runStep(step);
    }
    self.result.time = new Date().getTime() - self.startTime;
    self.log(f("Done %s steps in %dms", self.steps.length, self.result.time), "info");
    clearInterval(self.checker);
    self.step -= 1;
    self.emit('run.complete');
    try {
        if (utils.isFunction(onComplete)) {
            onComplete.call(self, self);
        } else if (utils.isFunction(self.options.onRunComplete)) {
            self.options.onRunComplete.call(self, self);
        }
    } catch (error) {
        self.emit('complete.error', error);
        if (!self.options.silentErrors) {
            throw error;
        }
    }
};

/**
 * Checks if this instance is started.
 *
 * @return Boolean
 * @throws CasperError
 */
Casper.prototype.checkStarted = function checkStarted() {
    "use strict";
    if (!this.started) {
        throw new CasperError(f("Casper is not started, can't execute `%s()`",
                                checkStarted.caller.name));
    }
};

/**
 * Clears the current page execution environment context. Useful to avoid
 * having previously loaded DOM contents being still active (refs #34).
 *
 * Think of it as a way to stop javascript execution within the remote DOM
 * environment.
 *
 * @return Casper
 */
Casper.prototype.clear = function clear() {
    "use strict";
    this.checkStarted();
    this.page.content = '';
    return this;
};

/**
 * Emulates a click on the element from the provided selector using the mouse
 * pointer, if possible.
 *
 * In case of success, `true` is returned, `false` otherwise.
 *
 * @param  String   selector  A DOM CSS3 compatible selector
 * @return Boolean
 */
Casper.prototype.click = function click(selector) {
    "use strict";
    this.checkStarted();
    var success = this.mouseEvent('mousedown', selector) && this.mouseEvent('mouseup', selector);
    success = success && this.mouseEvent('click', selector);
    this.evaluate(function(selector) {
        var element = __utils__.findOne(selector);
        if (element) {
            element.focus();
        }
    }, selector);
    this.emit('click', selector);
    return success;
};

/**
 * Emulates a click on the element having `label` as innerText. The first
 * element matching this label will be selected, so use with caution.
 *
 * @param  String   label  Element innerText value
 * @param  String   tag    An element tag name (eg. `a` or `button`) (optional)
 * @return Boolean
 */
Casper.prototype.clickLabel = function clickLabel(label, tag) {
    "use strict";
    this.checkStarted();
    tag = tag || "*";
    var escapedLabel = utils.quoteXPathAttributeString(label);
    var selector = selectXPath(f('//%s[text()=%s]', tag, escapedLabel));
    return this.click(selector);
};

/**
 * Configures HTTP authentication parameters. Will try parsing auth credentials from
 * the passed location first, then check for configured settings if any.
 *
 * @param  String  location  Requested url
 * @param  Object  settings  Request settings
 * @return Casper
 */
Casper.prototype.configureHttpAuth = function configureHttpAuth(location, settings) {
    "use strict";
    var httpAuthMatch = location.match(/^https?:\/\/(.+):(.+)@/i);
    this.checkStarted();
    if (httpAuthMatch) {
        this.page.settings.userName = httpAuthMatch[1];
        this.page.settings.password = httpAuthMatch[2];
    } else if (utils.isObject(settings) && settings.username) {
        this.page.settings.userName = settings.username;
        this.page.settings.password = settings.password;
    } else {
        return;
    }
    this.emit('http.auth', this.page.settings.userName, this.page.settings.password);
    this.log("Setting HTTP authentication for user " + this.page.settings.userName, "info");
    return this;
};

/**
 * Creates a step definition.
 *
 * @param  Function  fn       The step function to call
 * @param  Object    options  Step options
 * @return Function  The final step function
 */
Casper.prototype.createStep = function createStep(fn, options) {
    "use strict";
    if (!utils.isFunction(fn)) {
        throw new CasperError("createStep(): a step definition must be a function");
    }
    fn.options = utils.isObject(options) ? options : {};
    this.emit('step.created', fn);
    return fn;
};

/**
 * Logs the HTML code of the current page.
 *
 * @param  String   selector  A DOM CSS3/XPath selector (optional)
 * @param  Boolean  outer     Whether to fetch outer HTML contents (default: false)
 * @return Casper
 */
Casper.prototype.debugHTML = function debugHTML(selector, outer) {
    "use strict";
    this.checkStarted();
    return this.echo(this.getHTML(selector, outer));
};

/**
 * Logs the textual contents of the current page.
 *
 * @return Casper
 */
Casper.prototype.debugPage = function debugPage() {
    "use strict";
    this.checkStarted();
    this.echo(this.evaluate(function _evaluate() {
        return document.body.textContent || document.body.innerText;
    }));
    return this;
};

/**
 * Exit phantom on failure, with a logged error message.
 *
 * @param  String  message  An optional error message
 * @param  Number  status   An optional exit status code (must be > 0)
 * @return Casper
 */
Casper.prototype.die = function die(message, status) {
    "use strict";
    this.result.status = "error";
    this.result.time = new Date().getTime() - this.startTime;
    if (!utils.isString(message) || !message.length) {
        message = "Suite explicitely interrupted without any message given.";
    }
    this.log(message, "error");
    this.echo(message, "ERROR");
    this.emit('die', message, status);
    if (utils.isFunction(this.options.onDie)) {
        this.options.onDie.call(this, this, message, status);
    }
    return this.exit(~~status > 0 ? ~~status : 1);
};

/**
 * Downloads a resource and saves it on the filesystem.
 *
 * @param  String  url         The url of the resource to download
 * @param  String  targetPath  The destination file path
 * @param  String  method      The HTTP method to use (default: GET)
 * @param  String  data        Optional data to pass performing the request
 * @return Casper
 */
Casper.prototype.download = function download(url, targetPath, method, data) {
    "use strict";
    this.checkStarted();
    var cu = require('clientutils').create(utils.mergeObjects({}, this.options));
    try {
        fs.write(targetPath, cu.decode(this.base64encode(url, method, data)), 'wb');
        this.emit('downloaded.file', targetPath);
        this.log(f("Downloaded and saved resource in %s", targetPath));
    } catch (e) {
        this.log(f("Error while downloading %s to %s: %s", url, targetPath, e), "error");
    }
    return this;
};

/**
 * Iterates over the values of a provided array and execute a callback for each
 * item.
 *
 * @param  Array     array
 * @param  Function  fn     Callback: function(casper, item, index)
 * @return Casper
 */
Casper.prototype.each = function each(array, fn) {
    "use strict";
    if (!utils.isArray(array)) {
        this.log("each() only works with arrays", "error");
        return this;
    }
    array.forEach(function _forEach(item, i) {
        fn.call(this, this, item, i);
    }, this);
    return this;
};

/**
 * Iterates over the values of a provided array and adds a step for each item.
 *
 * @param  Array     array
 * @param  Function  then   Step: function(response); item will be attached to response.data
 * @return Casper
 */
Casper.prototype.eachThen = function each(array, then) {
    "use strict";
    if (!utils.isArray(array)) {
        this.log("eachThen() only works with arrays", "error");
        return this;
    }
    array.forEach(function _forEach(item) {
        this.then(function() {
            this.then(this.createStep(then, {data: item}));
        });
    }, this);
    return this;
};

/**
 * Prints something to stdout.
 *
 * @param  String  text   A string to echo to stdout
 * @param  String  style  An optional style name
 * @param  Number  pad    An optional pad value
 * @return Casper
 */
Casper.prototype.echo = function echo(text, style, pad) {
    "use strict";
    if (!utils.isString(text)) {
        try {
            text = text.toString();
        } catch (e) {
            try {
                text = utils.serialize(text);
            } catch (e2) {
                text = '';
            }
        }
    }
    var message = style ? this.colorizer.colorize(text, style, pad) : text;
    console.log(this.filter('echo.message', message) || message);
    return this;
};

/**
 * Evaluates an expression in the page context, a bit like what
 * WebPage#evaluate does, but the passed function can also accept
 * parameters if a context Object is also passed:
 *
 *     casper.evaluate(function(username, password) {
 *         document.querySelector('#username').value = username;
 *         document.querySelector('#password').value = password;
 *         document.querySelector('#submit').click();
 *     }, 'Bazoonga', 'baz00nga');
 *
 * @param  Function  fn       The function to be evaluated within current page DOM
 * @param  Object    context  Object containing the parameters to inject into the function
 * @return mixed
 * @see    WebPage#evaluate
 */
Casper.prototype.evaluate = function evaluate(fn, context) {
    "use strict";
    this.checkStarted();
    // check whether javascript is enabled !!
    if (this.options.pageSettings.javascriptEnabled === false) {
        throw new CasperError("evaluate() requires javascript to be enabled");
    }
    // preliminary checks
    if (!utils.isFunction(fn) && !utils.isString(fn)) { // phantomjs allows functions defs as string
        throw new CasperError("evaluate() only accepts functions or strings");
    }
    // ensure client utils are always injected
    this.injectClientUtils();
    // function context
    if (arguments.length === 1) {
        return utils.clone(this.page.evaluate(fn));
    } else if (arguments.length === 2) {
        // check for closure signature if it matches context
        if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) {
            context = utils.objectValues(context);
        } else {
            context = [context];
        }
    } else {
        // phantomjs-style signature
        context = [].slice.call(arguments, 1);
    }
    return utils.clone(this.page.evaluate.apply(this.page, [fn].concat(context)));
};

/**
 * Evaluates an expression within the current page DOM and die() if it
 * returns false.
 *
 * @param  function  fn       The expression to evaluate
 * @param  String    message  The error message to log
 * @param  Number  status   An optional exit status code (must be > 0)
 *
 * @return Casper
 */
Casper.prototype.evaluateOrDie = function evaluateOrDie(fn, message, status) {
    "use strict";
    this.checkStarted();
    if (!this.evaluate(fn)) {
        return this.die(message, status);
    }
    return this;
};

/**
 * Checks if an element matching the provided DOM CSS3/XPath selector exists in
 * current page DOM.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return Boolean
 */
Casper.prototype.exists = function exists(selector) {
    "use strict";
    this.checkStarted();
    return this.callUtils("exists", selector);
};

/**
 * Exits phantom.
 *
 * @param  Number  status  Status
 * @return Casper
 */
Casper.prototype.exit = function exit(status) {
    "use strict";
    this.emit('exit', status);
    phantom.exit(status);
};

/**
 * Fetches plain text contents contained in the DOM element(s) matching a given CSS3/XPath
 * selector.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return String
 */
Casper.prototype.fetchText = function fetchText(selector) {
    "use strict";
    this.checkStarted();
    return this.callUtils("fetchText", selector);
};

/**
 * Fills a form with provided field values.
 *
 * @param  String selector  A DOM CSS3/XPath selector to the target form to fill
 * @param  Object vals      Field values
 * @param  Object options   The fill settings (optional)
 */
Casper.prototype.fillForm = function fillForm(selector, vals, options) {
    "use strict";
    this.checkStarted();

    var selectorType = options && options.selectorType || "names",
        submit = !!(options && options.submit);

    this.emit('fill', selector, vals, options);

    var fillResults = this.evaluate(function _evaluate(selector, vals, selectorType) {
        try {
            return __utils__.fill(selector, vals, selectorType);
        } catch (exception) {
            return {exception: exception.toString()};
        }
    }, selector, vals, selectorType);

    if (!fillResults) {
        throw new CasperError("Unable to fill form");
    } else if (fillResults && fillResults.exception) {
        throw new CasperError("Unable to fill form: " + fillResults.exception);
    } else if (fillResults.errors.length > 0) {
        throw new CasperError(f('Errors encountered while filling form: %s',
                              fillResults.errors.join('; ')));
    }

    // File uploads
    if (fillResults.files && fillResults.files.length > 0) {
        if (utils.isObject(selector) && selector.type === 'xpath') {
            this.warn('Filling file upload fields is currently not supported using ' +
                      'XPath selectors; Please use a CSS selector instead.');
        } else {
            fillResults.files.forEach(function _forEach(file) {
                if (!file || !file.path) {
                    return;
                }
                if (!fs.exists(file.path)) {
                    throw new CasperError('Cannot upload nonexistent file: ' + file.path);
                }
                var fileFieldSelector;
                if (file.type === "names") {
                    fileFieldSelector = [selector, 'input[name="' + file.selector + '"]'].join(' ');
                } else if (file.type === "css") {
                    fileFieldSelector = [selector, file.selector].join(' ');
                }
                this.page.uploadFile(fileFieldSelector, file.path);
            }.bind(this));
        }
    }
    // Form submission?
    if (submit) {
        this.evaluate(function _evaluate(selector) {
            var form = __utils__.findOne(selector);
            var method = (form.getAttribute('method') || "GET").toUpperCase();
            var action = form.getAttribute('action') || "unknown";
            __utils__.log('submitting form to ' + action + ', HTTP ' + method, 'info');
            var event = document.createEvent('Event');
            event.initEvent('submit', true, true);
            if (!form.dispatchEvent(event)) {
                __utils__.log('unable to submit form', 'warning');
                return;
            }
            if (typeof form.submit === "function") {
                form.submit();
            } else {
                // http://www.spiration.co.uk/post/1232/Submit-is-not-a-function
                form.submit.click();
            }
        }, selector);
    }
}

/**
 * Fills a form with provided field values using the Name attribute.
 *
 * @param  String  formSelector  A DOM CSS3/XPath selector to the target form to fill
 * @param  Object  vals          Field values
 * @param  Boolean submit        Submit the form?
 */
Casper.prototype.fillNames = function fillNames(formSelector, vals, submit) {
    "use strict";
    return this.fillForm(formSelector, vals, {
        submit: submit,
        selectorType: 'names'
    });
};

/**
 * Fills a form with provided field values using CSS3 selectors.
 *
 * @param  String  formSelector  A DOM CSS3/XPath selector to the target form to fill
 * @param  Object  vals          Field values
 * @param  Boolean submit        Submit the form?
 */
Casper.prototype.fillSelectors = function fillSelectors(formSelector, vals, submit) {
    "use strict";
    return this.fillForm(formSelector, vals, {
        submit: submit,
        selectorType: 'css'
    });
};

/**
 * Fills a form with provided field values using the Name attribute by default.
 *
 * @param  String  formSelector  A DOM CSS3/XPath selector to the target form to fill
 * @param  Object  vals          Field values
 * @param  Boolean submit        Submit the form?
 */
Casper.prototype.fill = Casper.prototype.fillNames;

/**
 * Fills a form with provided field values using XPath selectors.
 *
 * @param  String  formSelector  A DOM CSS3/XPath selector to the target form to fill
 * @param  Object  vals          Field values
 * @param  Boolean submit        Submit the form?
 */
Casper.prototype.fillXPath = function fillXPath(formSelector, vals, submit) {
    "use strict";
    return this.fillForm(formSelector, vals, {
        submit: submit,
        selectorType: 'xpath'
    });
};

/**
 * Go a step forward in browser's history
 *
 * @return Casper
 */
Casper.prototype.forward = function forward() {
    "use strict";
    this.checkStarted();
    return this.then(function _step() {
        this.emit('forward');
        this.evaluate(function _evaluate() {
            history.forward();
        });
    });
};

/**
 * Creates a new Colorizer instance. Sets `Casper.options.type` to change the
 * colorizer type name (see the `colorizer` module).
 *
 * @return Object
 */
Casper.prototype.getColorizer = function getColorizer() {
    "use strict";
    return colorizer.create(this.options.colorizerType || 'Colorizer');
};

/**
 * Retrieves current page contents, dealing with exotic other content types than HTML.
 *
 * @return String
 */
Casper.prototype.getPageContent = function getPageContent() {
    "use strict";
    this.checkStarted();
    var contentType = utils.getPropertyPath(this, 'currentResponse.contentType');
    if (!utils.isString(contentType)) {
        return this.page.frameContent;
    }
    // for some reason (qt)webkit/Gecko will always enclose non text/html body contents within an html
    // structure like this:
    // webkit: <html><head></head><body><pre style="(...)">content</pre></body></html>
    // gecko: <html><head><link rel="alternate stylesheet" type="text/css" href="resource://gre-resources/plaintext.css" title="..."></head><body><pre>document.write('foo');\n</pre></body></html>
    var sanitizedHtml = this.evaluate(function checkHtml() {
        var head = __utils__.findOne('head'),
            body = __utils__.findOne('body');
        if (!head || !body) {
            return null;
        }
        // for content in Webkit
        if (head.childNodes.length === 0 &&
            body.childNodes.length === 1 &&
            __utils__.findOne('body pre[style]')) {
            return __utils__.findOne('body pre').textContent.trim();
        }
        // for content in Gecko
        if (head.childNodes.length === 1 &&
            body.childNodes.length === 1 &&
            head.childNodes[0].localName === 'link' &&
            head.childNodes[0].getAttribute('href') === 'resource://gre-resources/plaintext.css' &&
            body.childNodes[0].localName === 'pre' ) {
            return body.childNodes[0].textContent.trim();
        }
        return null;
    });
    return sanitizedHtml ? sanitizedHtml : this.page.frameContent;
};

/**
 * Retrieves current document url.
 *
 * @return String
 */
Casper.prototype.getCurrentUrl = function getCurrentUrl() {
    "use strict";
    this.checkStarted();
    try {
        if (this.options.pageSettings.javascriptEnabled === false) {
            return this.page.url;
        } else {
            return utils.decodeUrl(this.evaluate(function _evaluate() {
                return document.location.href;
            }));
        }
    } catch (e) {
        // most likely the current page object has been "deleted" (think closed popup)
        if (/deleted QObject/.test(e.message))
            return "";
        throw e;
    }
};

/**
 * Retrieves the value of an attribute on the first element matching the provided
 * DOM CSS3/XPath selector.
 *
 * @param  String  selector   A DOM CSS3/XPath selector
 * @param  String  attribute  The attribute name to lookup
 * @return String  The requested DOM element attribute value
 */
Casper.prototype.getElementAttribute =
Casper.prototype.getElementAttr = function getElementAttr(selector, attribute) {
    "use strict";
    this.checkStarted();
    return this.evaluate(function _evaluate(selector, attribute) {
        return __utils__.findOne(selector).getAttribute(attribute);
    }, selector, attribute);
};

/**
 * Retrieves the value of an attribute for each element matching the provided
 * DOM CSS3/XPath selector.
 *
 * @param  String  selector   A DOM CSS3/XPath selector
 * @param  String  attribute  The attribute name to lookup
 * @return Array
 */
Casper.prototype.getElementsAttribute =
Casper.prototype.getElementsAttr = function getElementsAttr(selector, attribute) {
    "use strict";
    this.checkStarted();
    return this.evaluate(function _evaluate(selector, attribute) {
        return [].map.call(__utils__.findAll(selector), function(element) {
            return element.getAttribute(attribute);
        });
    }, selector, attribute);
}

/**
 * Retrieves boundaries for a DOM element matching the provided DOM CSS3/XPath selector.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return Object
 */
Casper.prototype.getElementBounds = function getElementBounds(selector) {
    "use strict";
    this.checkStarted();
    if (!this.exists(selector)) {
        throw new CasperError("No element matching selector found: " + selector);
    }
    var clipRect = this.callUtils("getElementBounds", selector);
    if (!utils.isClipRect(clipRect)) {
        throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector);
    }
    return clipRect;
};

/**
 * Retrieves information about the node matching the provided selector.
 *
 * @param  String|Objects  selector  CSS3/XPath selector
 * @return Object
 */
Casper.prototype.getElementInfo = function getElementInfo(selector) {
    "use strict";
    this.checkStarted();
    if (!this.exists(selector)) {
        throw new CasperError(f("Cannot get informations from %s: element not found.", selector));
    }
    return this.callUtils("getElementInfo", selector);
};

/**
 * Retrieves information about the nodes matching the provided selector.
 *
 * @param String|Objects  selector  CSS3/XPath selector
 * @return Array
 */
Casper.prototype.getElementsInfo = function getElementsInfo(selector) {
    "use strict";
    this.checkStarted();
    if (!this.exists(selector)) {
        throw new CasperError(f("Cannot get information from %s: no elements found.", selector));
    }
    return this.callUtils("getElementsInfo", selector);
};

/**
 * Retrieves boundaries for all the DOM elements matching the provided DOM CSS3/XPath selector.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return Array
 */
Casper.prototype.getElementsBounds = function getElementBounds(selector) {
    "use strict";
    this.checkStarted();
    if (!this.exists(selector)) {
        throw new CasperError("No element matching selector found: " + selector);
    }
    return this.callUtils("getElementsBounds", selector);
};

/**
 * Retrieves a given form all of its field values.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return Object
 */
Casper.prototype.getFormValues = function(selector) {
    "use strict";
    this.checkStarted();
    if (!this.exists(selector)) {
        throw new CasperError(f('Form matching selector "%s" not found', selector));
    }
    return this.callUtils("getFormValues", selector);
};

/**
 * Retrieves global variable.
 *
 * @param  String  name  The name of the global variable to retrieve
 * @return mixed
 */
Casper.prototype.getGlobal = function getGlobal(name) {
    "use strict";
    this.checkStarted();
    var result = this.evaluate(function _evaluate(name) {
        var result = {};
        try {
            result.value = JSON.stringify(window[name]);
        } catch (e) {
            var message = "Unable to JSON encode window." + name + ": " + e;
            __utils__.log(message, "error");
            result.error = message;
        }
        return result;
    }, name);
    if (!utils.isObject(result)) {
        throw new CasperError(f('Could not retrieve global value for "%s"', name));
    } else if ('error' in result) {
        throw new CasperError(result.error);
    } else if (utils.isString(result.value)) {
        return JSON.parse(result.value);
    }
};

/**
 * Retrieves current HTML code matching the provided CSS3/XPath selector.
 * Returns the HTML contents for the whole page if no arg is passed.
 *
 * @param  String   selector  A DOM CSS3/XPath selector
 * @param  Boolean  outer     Whether to fetch outer HTML contents (default: false)
 * @return String
 */
Casper.prototype.getHTML = function getHTML(selector, outer) {
    "use strict";
    this.checkStarted();
    if (!selector) {
        return this.page.frameContent;
    }
    if (!this.exists(selector)) {
        throw new CasperError("No element matching selector found: " + selector);
    }
    return this.evaluate(function getSelectorHTML(selector, outer) {
        var element = __utils__.findOne(selector);
        return outer ? element.outerHTML : element.innerHTML;
    }, selector, !!outer);
};

/**
 * Retrieves current page title, if any.
 *
 * @return String
 */
Casper.prototype.getTitle = function getTitle() {
    "use strict";
    this.checkStarted();
    return this.evaluate(function _evaluate() {
        return document.title;
    });
};

/**
 * Handles received HTTP resource.
 *
 * @param  Object  resource  PhantomJS HTTP resource
 */
Casper.prototype.handleReceivedResource = function(resource) {
    "use strict";
    /*jshint maxstatements:20*/
    if (resource.stage !== "end") {
        return;
    }
    this.resources.push(resource);
    if (utils.decodeUrl(resource.url) !== this.requestUrl) {
        return;
    }
    this.currentHTTPStatus = null;
    this.currentResponse = {};
    if (utils.isHTTPResource(resource)) {
        this.emit('page.resource.received', resource);
        this.currentResponse = resource;
        this.currentHTTPStatus = resource.status;
        this.emit('http.status.' + resource.status, resource);
        if (utils.isObject(this.options.httpStatusHandlers) &&
            resource.status in this.options.httpStatusHandlers &&
            utils.isFunction(this.options.httpStatusHandlers[resource.status])) {
            this.options.httpStatusHandlers[resource.status].call(this, this, resource);
        }
    }
    this.currentUrl = resource.url;
    this.emit('location.changed', resource.url);
};

/**
 * Initializes PhantomJS error handler.
 *
 */
Casper.prototype.initErrorHandler = function initErrorHandler() {
    "use strict";
    var casper = this;
    phantom.onError = function phantom_onError(msg, backtrace) {
        casper.emit('error', msg, backtrace);
        if (casper.options.exitOnError === true) {
            casper.exit(1);
        }
    };
};

/**
 * Injects configured local client scripts.
 *
 * @return Casper
 */
Casper.prototype.injectClientScripts = function injectClientScripts() {
    "use strict";
    this.checkStarted();
    if (!this.options.clientScripts) {
        return;
    }
    if (utils.isString(this.options.clientScripts)) {
        this.options.clientScripts = [this.options.clientScripts];
    }
    if (!utils.isArray(this.options.clientScripts)) {
        throw new CasperError("The clientScripts option must be an array");
    }
    this.options.clientScripts.forEach(function _forEach(script) {
        if (this.page.injectJs(script)) {
            this.log(f('Automatically injected %s client side', script), "debug");
        } else {
            this.warn('Failed injecting %s client side', script);
        }
    }.bind(this));
    return this;
};

/**
 * Injects Client-side utilities in current page context.
 *
 */
Casper.prototype.injectClientUtils = function injectClientUtils() {
    "use strict";
    this.checkStarted();
    var clientUtilsInjected = this.page.evaluate(function() {
        return typeof __utils__ === "object";
    });
    if (true === clientUtilsInjected) {
        return;
    }
    var clientUtilsPath = require('fs').pathJoin(phantom.casperPath, 'modules', 'clientutils.js');
    if (true === this.page.injectJs(clientUtilsPath)) {
        this.log("Successfully injected Casper client-side utilities", "debug");
    } else {
        this.warn("Failed to inject Casper client-side utilities");
    }
    // ClientUtils and Casper shares the same options
    // These are not the lines I'm the most proud of in my life, but it works.
    /*global __options*/
    this.page.evaluate(function() {
        window.__utils__ = new window.ClientUtils(__options);
    }.toString().replace('__options', JSON.stringify(this.options)));
};

/**
 * Loads and include remote client scripts to current page.
 *
 * @return Casper
 */
Casper.prototype.includeRemoteScripts = function includeRemoteScripts() {
    "use strict";
    var numScripts = this.options.remoteScripts.length, loaded = 0;
    if (numScripts === 0) {
        return this;
    }
    this.waitStart();
    this.options.remoteScripts.forEach(function(scriptUrl) {
        this.log(f("Loading remote script: %s", scriptUrl), "debug");
        this.page.includeJs(scriptUrl, function() {
            loaded++;
            this.log(f("Remote script %s loaded", scriptUrl), "debug");
            if (loaded === numScripts) {
                this.log("All remote scripts loaded.", "debug");
                this.waitDone();
            }
        }.bind(this));
    }.bind(this));
    return this;
};

/**
 * Logs a message.
 *
 * @param  String  message  The message to log
 * @param  String  level    The log message level (from Casper.logLevels property)
 * @param  String  space    Space from where the logged event occurred (default: "phantom")
 * @return Casper
 */
Casper.prototype.log = function log(message, level, space) {
    "use strict";
    level = level && this.logLevels.indexOf(level) > -1 ? level : "debug";
    space = space ? space : "phantom";
    if (level === "error" && utils.isFunction(this.options.onError)) {
        this.options.onError.call(this, this, message, space);
    }
    if (this.logLevels.indexOf(level) < this.logLevels.indexOf(this.options.logLevel)) {
        return this; // skip logging
    }
    var entry = {
        level:   level,
        space:   space,
        message: message,
        date:    new Date().toString()
    };
    if (level in this.logFormats && utils.isFunction(this.logFormats[level])) {
        message = this.logFormats[level](message, level, space);
    } else {
        message = f('%s [%s] %s',
                    this.colorizer.colorize(f('[%s]', level), this.logStyles[level]),
                    space,
                    message);
    }
    if (this.options.verbose) {
        this.echo(this.filter('log.message', message) || message); // direct output
    }
    this.result.log.push(entry);
    this.emit('log', entry);
    return this;
};

/**
 * Emulates an event on the element from the provided selector using the mouse
 * pointer, if possible.
 *
 * In case of success, `true` is returned, `false` otherwise.
 *
 * @param  String   type      Type of event to emulate
 * @param  String   selector  A DOM CSS3 compatible selector
 * @return Boolean
 */
Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
    "use strict";
    this.checkStarted();
    this.log("Mouse event '" + type + "' on selector: " + selector, "debug");
    if (!this.exists(selector)) {
        throw new CasperError(f("Cannot dispatch %s event on nonexistent selector: %s", type, selector));
    }
    if (this.callUtils("mouseEvent", type, selector)) {
        return true;
    }
    // fallback onto native QtWebKit mouse events
    try {
        return this.mouse.processEvent(type, selector);
    } catch (e) {
        this.log(f("Couldn't emulate '%s' event on %s: %s", type, selector, e), "error");
    }
    return false;
};

/**
 * Performs an HTTP request, with optional settings.
 *
 * Available settings are:
 *
 * - String  method:   The HTTP method to use
 * - Object  data:     The data to use to perform the request, eg. {foo: 'bar'}
 * - Object  headers:  Custom request headers object, eg. {'Cache-Control': 'max-age=0'}
 *
 * @param  String  location  The url to open
 * @param  Object  settings  The request settings (optional)
 * @return Casper
 */
Casper.prototype.open = function open(location, settings) {
    "use strict";
    /*jshint maxstatements:30*/
    var baseCustomHeaders = this.page.customHeaders,
        customHeaders = settings && settings.headers || {};
    this.checkStarted();
    settings = utils.isObject(settings) ? settings : {};
    settings.method = settings.method || "get";
    // http method
    // taken from https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L302
    var methods = ["get", "head", "put", "post", "delete"];
    if (settings.method && (!utils.isString(settings.method) || methods.indexOf(settings.method.toLowerCase()) === -1)) {
        throw new CasperError("open(): settings.method must be part of " + methods.join(', '));
    }
    // http data
    if (settings.data) {
        if (utils.isObject(settings.data)) { // query object
            if (settings.headers && settings.headers["Content-Type"].match(/application\/json/)) {
                settings.data = JSON.stringify(settings.data); // convert object to JSON notation
            } else {
                settings.data = qs.encode(settings.data); // escapes all characters except alphabetic, decimal digits and ,-_.!~*'()
            }
        } else if (!utils.isString(settings.data)) {
            throw new CasperError("open(): invalid request settings data value: " + settings.data);
        }
    }
    // clean location
    location = utils.cleanUrl(location);
    // current request url
    this.configureHttpAuth(location, settings);
    this.requestUrl = this.filter('open.location', location) || location;
    this.emit('open', this.requestUrl, settings);
    this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug");
    // reset resources
    this.resources = [];
    // custom headers
    this.page.customHeaders = utils.mergeObjects(utils.clone(baseCustomHeaders), customHeaders);
    // perfom request
    this.browserInitializing = true;
    this.page.openUrl(this.requestUrl, {
        operation: settings.method,
        data:      settings.data
    }, this.page.settings);
    // revert base custom headers
    this.page.customHeaders = baseCustomHeaders;
    return this;
};

/**
 * Reloads current page.
 *
 * @param  Function  then  a next step function
 * @return Casper
 */
Casper.prototype.reload = function reload(then) {
    "use strict";
    this.checkStarted();
    // window.location.reload() is broken under phantomjs
    this.then(function() {
        this.open(this.getCurrentUrl());
    });
    if (utils.isFunction(then)) {
        this.then(this.createStep(then));
    }
    return this;
};

/**
 * Repeats a step a given number of times.
 *
 * @param  Number    times  Number of times to repeat step
 * @aram   function  then   The step closure
 * @return Casper
 * @see    Casper#then
 */
Casper.prototype.repeat = function repeat(times, then) {
    "use strict";
    for (var i = 0; i < times; i++) {
        this.then(then);
    }
    return this;
};

/**
 * Checks if a given resource was loaded by the remote page.
 *
 * @param  Function/String/RegExp  test  A test function, string or regular expression.
 *                                       In case a string is passed, url matching will be tested.
 * @return Boolean
 */
Casper.prototype.resourceExists = function resourceExists(test) {
    "use strict";
    this.checkStarted();
    var testFn;
    switch (utils.betterTypeOf(test)) {
        case "string":
            testFn = function _testResourceExists_String(res) {
                return res.url.search(test) !== -1 && res.status !== 404;
            };
            break;
        case "regexp":
            testFn = function _testResourceExists_Regexp(res) {
                return test.test(res.url) && res.status !== 404;
            };
            break;
        case "function":
            testFn = test;
            if (phantom.casperEngine !== "slimerjs")
                testFn.name = "_testResourceExists_Function";
            break;
        default:
            throw new CasperError("Invalid type");
    }
    return this.resources.some(testFn);
};

/**
 * Runs the whole suite of steps.
 *
 * @param  function  onComplete  an optional callback
 * @param  Number    time        an optional amount of milliseconds for interval checking
 * @return Casper
 */
Casper.prototype.run = function run(onComplete, time) {
    "use strict";
    this.checkStarted();
    if (!this.steps || this.steps.length < 1) {
        throw new CasperError('No steps defined, aborting');
    }
    this.log(f("Running suite: %d step%s", this.steps.length, this.steps.length > 1 ? "s" : ""), "info");
    this.emit('run.start');
    this.checker = setInterval(this.checkStep, (time ? time: this.options.retryTimeout), this, onComplete);
    return this;
};

/**
 * Runs a step.
 *
 * @param  Function  step
 */
Casper.prototype.runStep = function runStep(step) {
    "use strict";
    /*jshint maxstatements:20*/
    this.checkStarted();
    var skipLog = utils.isObject(step.options) && step.options.skipLog === true,
        stepInfo = f("Step %s %d/%d", step.name || "anonymous", this.step, this.steps.length),
        stepResult;
    function getCurrentSuiteId(casper) {
        try {
            return casper.test.getCurrentSuiteId();
        } catch (e) {
            return casper.step;
        }
    }
    if (!skipLog && /^http/.test(this.getCurrentUrl())) {
        this.log(stepInfo + f(' %s (HTTP %d)', this.getCurrentUrl(), this.currentHTTPStatus), "info");
    }
    if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) {
        var stepTimeoutCheckInterval = setInterval(function _check(self, start, stepNum) {
            if (new Date().getTime() - start > self.options.stepTimeout) {
                if (getCurrentSuiteId(self) === stepNum) {
                    self.emit('step.timeout', stepNum, self.options.onStepTimeout);
                    if (utils.isFunction(self.options.onStepTimeout)) {
                        self.options.onStepTimeout.call(self, self.options.stepTimeout, stepNum);
                    }
                }
                clearInterval(stepTimeoutCheckInterval);
            }
        }, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteId(this));
    }
    this.emit('step.start', step);
    if (this.currentResponse) {
        this.currentResponse.data = step.options && step.options.data || null;
    }
    try {
        stepResult = step.call(this, this.currentResponse);
        if (utils.isFunction(this.options.onStepComplete)) {
            this.options.onStepComplete.call(this, this, stepResult);
        }
    } catch (err) {
        this.emit('step.error', err);
        if (!this.options.silentErrors) {
            throw err;
        }
    }
    if (!skipLog) {
        this.emit('step.complete', stepResult);
        this.log(stepInfo + f(": done in %dms.", new Date().getTime() - this.startTime), "info");
    }
};

/**
 * Sends keys to given element.
 *
 * Options:
 *
 * - eventType: "keypress", "keyup" or "keydown" (default: "keypress")
 * - modifiers: a string defining the key modifiers, eg. "alt", "alt+shift"
 *
 * @param  String  selector  A DOM CSS3 compatible selector
 * @param  String  keys      A string representing the sequence of char codes to send
 * @param  Object  options   Options
 * @return Casper
 */
Casper.prototype.sendKeys = function(selector, keys, options) {
    "use strict";
    this.checkStarted();
    options = utils.mergeObjects({
        eventType: 'keypress',
        reset: false
    }, options || {});
    var elemInfos = this.getElementInfo(selector),
        tag = elemInfos.nodeName.toLowerCase(),
        type = utils.getPropertyPath(elemInfos, 'attributes.type'),
        supported = ["color", "date", "datetime", "datetime-local", "email",
                     "hidden", "month", "number", "password", "range", "search",
                     "tel", "text", "time", "url", "week"],
        isTextInput = false,
        isTextArea = tag === 'textarea',
        isValidInput = tag === 'input' && (typeof type === 'undefined' || supported.indexOf(type) !== -1),
        isContentEditable = !!elemInfos.attributes.contenteditable;

    if (isTextArea || isValidInput || isContentEditable) {
        // clicking on the input element brings it focus
        isTextInput = true;
        this.click(selector);
    }
    if (options.reset) {
        this.evaluate(function(selector) {
            __utils__.setField(__utils__.findOne(selector), '');
        }, selector);
        this.click(selector);
    }
    var modifiers = utils.computeModifier(options && options.modifiers,
                                          this.page.event.modifier)
    this.page.sendEvent(options.eventType, keys, null, null, modifiers);
    if (isTextInput && !options.keepFocus) {
        // remove the focus
        this.evaluate(function(selector) {
            __utils__.findOne(selector).blur();
        }, selector);
    }
    return this;
};

/**
 * Scrolls current document to x, y coordinates.
 *
 * @param  {Number} x X position
 * @param  {Number} y Y position
 * @return {Casper}
 */
Casper.prototype.scrollTo = function(x, y) {
    "use strict";
    this.callUtils("scrollTo", x, y);
    return this;
};

/**
 * Scrolls current document up to its bottom.
 *
 * @return {Casper}
 */
Casper.prototype.scrollToBottom = function scrollToBottom() {
    "use strict";
    this.callUtils("scrollToBottom");
    return this;
};

/**
 * Sets current page content.
 *
 * @param  String  content  Desired page content
 * @return Casper
 */
Casper.prototype.setContent = function setContent(content) {
    "use strict";
    this.checkStarted();
    this.page.content = content;
    return this;
};

/**
 * Sets current WebPage instance the credentials for HTTP authentication.
 *
 * @param  String  username
 * @param  String  password
 * @return Casper
 */
Casper.prototype.setHttpAuth = function setHttpAuth(username, password) {
    "use strict";
    this.checkStarted();
    this.page.settings.userName = username;
    this.page.settings.password = password;
    return this;
};

/**
 * Configures and starts Casper.
 *
 * @param  String   location  An optional location to open on start
 * @param  function then      Next step function to execute on page loaded (optional)
 * @return Casper
 */
Casper.prototype.start = function start(location, then) {
    "use strict";
    /*jshint maxstatements:30*/
    this.emit('starting');
    this.log('Starting...', "info");
    this.startTime = new Date().getTime();
    this.currentResponse = {};
    this.history = [];
    this.popups = pagestack.create();
    this.steps = [];
    this.step = 0;
    // Option checks
    if (this.logLevels.indexOf(this.options.logLevel) < 0) {
        this.log(f("Unknown log level '%d', defaulting to 'warning'", this.options.logLevel), "warning");
        this.options.logLevel = "warning";
    }
    if (!utils.isWebPage(this.page)) {
        this.page = this.mainPage = utils.isWebPage(this.options.page) ? this.options.page : createPage(this);
    }
    this.page.settings = utils.mergeObjects(this.page.settings, this.options.pageSettings);
    if (utils.isClipRect(this.options.clipRect)) {
        this.page.clipRect = this.options.clipRect;
    }
    if (utils.isObject(this.options.viewportSize)) {
        this.page.viewportSize = this.options.viewportSize;
    }
    // timeout handling
    if (utils.isNumber(this.options.timeout) && this.options.timeout > 0) {
        this.log(f("Execution timeout set to %dms", this.options.timeout), "info");
        setTimeout(function _check(self) {
            self.emit('timeout');
            if (utils.isFunction(self.options.onTimeout)) {
                self.options.onTimeout.call(self, self.options.timeout);
            }
        }, this.options.timeout, this);
    }
    this.started = true;
    this.emit('started');
    if (utils.isString(location) && location.length > 0) {
        return this.thenOpen(location, utils.isFunction(then) ? then : this.createStep(function _step() {
            this.log("start page is loaded", "debug");
        }, {skipLog: true}));
    }
    return this;
};

/**
 * Returns the current status of current instance
 *
 * @param  Boolean  asString  Export status object as string
 * @return Object
 */
Casper.prototype.status = function status(asString) {
    "use strict";
    var properties = ['currentHTTPStatus', 'loadInProgress', 'navigationRequested',
                      'options', 'pendingWait', 'requestUrl', 'started', 'step', 'url'];
    var currentStatus = {};
    properties.forEach(function(property) {
        currentStatus[property] = this[property];
    }.bind(this));
    return asString === true ? utils.dump(currentStatus) : currentStatus;
};

/**
 * Schedules the next step in the navigation process.
 *
 * @param  function  step  A function to be called as a step
 * @return Casper
 */
Casper.prototype.then = function then(step) {
    "use strict";
    this.checkStarted();
    if (!utils.isFunction(step)) {
        throw new CasperError("You can only define a step as a function");
    }
    // check if casper is running
    if (this.checker === null) {
        // append step to the end of the queue
        step.level = 0;
        this.steps.push(step);
    } else {
        // insert substep a level deeper
        try {
            step.level = this.steps[this.step - 1].level + 1;
        } catch (e) {
            step.level = 0;
        }
        var insertIndex = this.step;
        while (this.steps[insertIndex] && step.level === this.steps[insertIndex].level) {
            insertIndex++;
        }
        this.steps.splice(insertIndex, 0, step);
    }
    this.emit('step.added', step);
    return this;
};

/**
 * Adds a new navigation step for clicking on a provided link selector
 * and execute an optional next step.
 *
 * @param  String   selector        A DOM CSS3 compatible selector
 * @param  Function then            Next step function to execute on page loaded (optional)
 * @return Casper
 * @see    Casper#click
 * @see    Casper#then
 */
Casper.prototype.thenClick = function thenClick(selector, then) {
    "use strict";
    this.checkStarted();
    this.then(function _step() {
        this.click(selector);
    });
    return utils.isFunction(then) ? this.then(then) : this;
};

/**
 * Adds a new navigation step to perform code evaluation within the
 * current retrieved page DOM.
 *
 * @param  function  fn       The function to be evaluated within current page DOM
 * @param  object    context  Optional function parameters context
 * @return Casper
 * @see    Casper#evaluate
 */
Casper.prototype.thenEvaluate = function thenEvaluate(fn, context) {
    "use strict";
    this.checkStarted();
    var args = [fn].concat([].slice.call(arguments, 1));
    return this.then(function _step() {
        this.evaluate.apply(this, args);
    });
};

/**
 * Adds a new navigation step for opening the provided location.
 *
 * @param  String   location  The URL to load
 * @param  function then      Next step function to execute on page loaded (optional)
 * @return Casper
 * @see    Casper#open
 */
Casper.prototype.thenOpen = function thenOpen(location, settings, then) {
    "use strict";
    this.checkStarted();
    if (!(settings && !utils.isFunction(settings))) {
      then = settings;
      settings = null;
    }
    this.then(this.createStep(function _step() {
        this.open(location, settings);
    }, {
        skipLog: true
    }));
    return utils.isFunction(then) ? this.then(then) : this;
};

/**
 * Adds a step which bypasses `nb` steps.
 *
 * @param  Integer  nb  Number of steps to bypass
 */
Casper.prototype.thenBypass = function thenBypass(nb) {
    "use strict";
    return this.then(function _thenBypass() {
        this.bypass(nb);
    });
};

/**
 * Bypass `nb` steps if condition is true.
 *
 * @param  Mixed    condition  Test condition
 * @param  Integer  nb         Number of steps to bypass
 */
Casper.prototype.thenBypassIf = function thenBypassIf(condition, nb) {
    "use strict";
    return this.then(function _thenBypassIf() {
        if (utils.isFunction(condition)) {
            condition = condition.call(this);
        }
        if (utils.isTruthy(condition)) {
            this.bypass(nb);
        }
    });
};

/**
 * Bypass `nb` steps if condition is false.
 *
 * @param Mixed    condition  Test condition
 * @param Integer  nb         Number of tests to bypass
 */
Casper.prototype.thenBypassUnless = function thenBypassUnless(condition, nb) {
    "use strict";
    return this.then(function _thenBypassUnless() {
        if (utils.isFunction(condition)) {
            condition = condition.call(this);
        }
        if (utils.isFalsy(condition)) {
            this.bypass(nb);
        }
    });
};

/**
 * Adds a new navigation step for opening and evaluate an expression
 * against the DOM retrieved from the provided location.
 *
 * @param  String    location  The url to open
 * @param  function  fn        The function to be evaluated within current page DOM
 * @param  object    context   Optional function parameters context
 * @return Casper
 * @see    Casper#evaluate
 * @see    Casper#open
 */
Casper.prototype.thenOpenAndEvaluate = function thenOpenAndEvaluate(location, fn, context) {
    "use strict";
    this.checkStarted();
    return this.thenOpen(location).thenEvaluate(fn, context);
};

/**
 * Returns a string representation of current instance
 *
 * @return String
 */
Casper.prototype.toString = function toString() {
    "use strict";
    return '[object Casper], currently at ' + this.getCurrentUrl();
};

/**
 * Clear all wait processes.
 *
 * @return Casper
 */
Casper.prototype.unwait = function unwait() {
    "use strict";
    this.waiters.forEach(function(interval) {
        if (interval) {
            clearInterval(interval);
        }
    });
    this.waiters = [];
    return this;
};

/**
 * Sets the user-agent string currently used when requesting urls.
 *
 * @param  String  userAgent  User agent string
 * @return String
 */
Casper.prototype.userAgent = function userAgent(agent) {
    "use strict";
    this.options.pageSettings.userAgent = agent;
    if (this.started && this.page) {
        this.page.settings.userAgent = agent;
    }
    return this;
};

/**
 * Changes the current viewport size. That operation is asynchronous as the page
 * has to reflow its contents accordingly.
 *
 * @param  Number    width   The viewport width, in pixels
 * @param  Number    height  The viewport height, in pixels
 * @param  Function  then    Next step to process (optional)
 * @return Casper
 */
Casper.prototype.viewport = function viewport(width, height, then) {
    "use strict";
    this.checkStarted();
    if (!utils.isNumber(width) || !utils.isNumber(height) || width <= 0 || height <= 0) {
        throw new CasperError(f("Invalid viewport: %dx%d", width, height));
    }

    this.page.viewportSize = {
        width: width,
        height: height
    };
    // setting the viewport could cause a redraw and it can take
    // time. At least for Gecko, we should wait a bit, even
    // if this time could not be enough.
    var time = (phantom.casperEngine === 'slimerjs'?400:100);
    return this.then(function _step() {
        this.waitStart();
        setTimeout(function _check(self) {
            self.waitDone();
            self.emit('viewport.changed', [width, height]);
            if (utils.isFunction(then)){
                self.then(then);
            }
        }, time, this);
    });
};

/**
 * Checks if an element matching the provided DOM CSS3/XPath selector is visible
 * current page DOM by checking that offsetWidth and offsetHeight are
 * both non-zero.
 *
 * @param  String  selector  A DOM CSS3/XPath selector
 * @return Boolean
 */
Casper.prototype.visible = function visible(selector) {
    "use strict";
    this.checkStarted();
    return this.callUtils("visible", selector);
};

/**
 * Displays a warning message onto the console and logs the event. Also emits a
 * `warn` event with the message passed.
 *
 * @param  String  message
 * @return Casper
 */
Casper.prototype.warn = function warn(message) {
    "use strict";
    this.log(message, "warning", "phantom");
    var formatted = f.apply(null, ["⚠  " + message].concat([].slice.call(arguments, 1)));
    this.emit('warn', message);
    return this.echo(formatted, 'COMMENT');
};

/**
 * Adds a new step that will wait for a given amount of time (expressed
 * in milliseconds) before processing an optional next one.
 *
 * @param  Number    timeout  The max amount of time to wait, in milliseconds
 * @param  Function  then     Next step to process (optional)
 * @return Casper
 */
Casper.prototype.wait = function wait(timeout, then) {
    "use strict";
    this.checkStarted();
    timeout = ~~timeout;
    if (timeout < 1) {
        throw new CasperError("wait() only accepts a positive integer > 0 as a timeout value");
    }
    if (then && !utils.isFunction(then)) {
        throw new CasperError("wait() a step definition must be a function");
    }
    return this.then(function _step() {
        this.waitStart();
        setTimeout(function _check(self) {
            self.log(f("wait() finished waiting for %dms.", timeout), "info");
            if (then) {
                try {
                    then.call(self, self);
                } catch (error) {
                    self.emit('wait.error', error);
                    if (!self.options.silentErrors) {
                        throw error;
                    }
                }
            }
            self.waitDone();
        }, timeout, this);
    });
};

Casper.prototype.waitStart = function waitStart() {
    "use strict";
    this.emit('wait.start');
    this.pendingWait = true;
};

Casper.prototype.waitDone = function waitDone() {
    "use strict";
    this.emit('wait.done');
    this.pendingWait = false;
};

/**
 * Waits until a function returns true to process a next step.
 *
 * @param  Function  testFx     A function to be evaluated for returning condition satisfecit
 * @param  Function  then       The next step to perform (optional)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @param  Object    details    A property bag of information about the condition being waited on (optional)
 * @return Casper
 */
Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout, details) {
    "use strict";
    this.checkStarted();
    timeout = timeout || this.options.waitTimeout;
    details = details || { testFx: testFx };
    if (!utils.isFunction(testFx)) {
        throw new CasperError("waitFor() needs a test function");
    }
    if (then && !utils.isFunction(then)) {
        throw new CasperError("waitFor() next step definition must be a function");
    }
    return this.then(function _step() {
        this.waitStart();
        var start = new Date().getTime();
        var condition = false;
        var interval = setInterval(function _check(self) {
            /*jshint maxstatements:20*/
            if ((new Date().getTime() - start < timeout) && !condition) {
                condition = testFx.call(self, self);
                return;
            }
            self.waitDone();
            if (!condition) {
                self.log("Casper.waitFor() timeout", "warning");
                var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
                self.emit('waitFor.timeout', timeout, details);
                clearInterval(interval); // refs #383
                if (!utils.isFunction(onWaitTimeout)) {
                    throw new CasperError('Invalid timeout function');
                }
                try {
                    return onWaitTimeout.call(self, timeout, details);
                } catch (error) {
                    self.emit('waitFor.timeout.error', error);
                    if (!self.options.silentErrors) {
                        throw error;
                    }
                } finally {
                    return;
                }
            }
            self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
            clearInterval(interval);
            if (then) {
                self.then(then);
            }
        }, this.options.retryTimeout, this);
        this.waiters.push(interval);
    });
};

/**
 * Waits until any alert is triggered.
 *
 * @param  Function  then       The next step to perform (required)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForAlert = function(then, onTimeout, timeout) {
    "use strict";
    if (!utils.isFunction(then)) {
        throw new CasperError("waitForAlert() needs a step function");
    }
    var message;
    function alertCallback(msg) {
        message = msg;
    }
    this.once("remote.alert", alertCallback);
    return this.waitFor(function isAlertReceived() {
        return message !== undefined;
    }, function onAlertReceived() {
        this.then(this.createStep(then, {data: message}));
    }, onTimeout, timeout);
};

/**
 * Waits for a popup page having its url matching the provided pattern to be opened
 * and loaded.
 *
 * @param  String|RegExp  urlPattern  The popup url pattern
 * @param  Function       then        The next step function (optional)
 * @param  Function       onTimeout   Function to call on operation timeout (optional)
 * @param  Number         timeout     Timeout in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForPopup = function waitForPopup(urlPattern, then, onTimeout, timeout) {
    "use strict";
    return this.waitFor(function() {
        try {
            this.popups.find(urlPattern);
            return true;
        } catch (e) {
            return false;
        }
    }, then, onTimeout, timeout, { popup: urlPattern });
};

/**
 * Waits until a given resource is loaded
 *
 * @param  String/Function/RegExp  test       A function to test if the resource exists.
 *                                            A string will be matched against the resources url.
 * @param  Function                then       The next step to perform (optional)
 * @param  Function                onTimeout  A callback function to call on timeout (optional)
 * @param  Number                  timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForResource = function waitForResource(test, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        return this.resourceExists(test);
    }, then, onTimeout, timeout, { resource: test });
};

/**
 * Waits for a given url to be loaded.
 *
 * @param  String|RegExp  url  The url to wait for
 * @param  Function         then       The next step to perform (optional)
 * @param  Function         onTimeout  A callback function to call on timeout (optional)
 * @return Casper
 */
Casper.prototype.waitForUrl = function waitForUrl(url, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        if (utils.isString(url)) {
            return this.getCurrentUrl().indexOf(url) !== -1;
        } else if (utils.isRegExp(url)) {
            return url.test(this.getCurrentUrl());
        }
        throw new CasperError('invalid url argument');
    }, then, onTimeout, timeout, { url: url });
};

/**
 * Waits until an element matching the provided DOM CSS3/XPath selector exists in
 * remote DOM to process a next step.
 *
 * @param  String    selector   A DOM CSS3/XPath selector
 * @param  Function  then       The next step to perform (optional)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        return this.exists(selector);
    }, then, onTimeout, timeout, { selector: selector });
};

/**
 * Waits until the page contains given HTML text or matches a given RegExp.
 *
 * @param  String|RegExp  pattern    Text or RegExp to wait for
 * @param  Function       then       The next step to perform (optional)
 * @param  Function       onTimeout  A callback function to call on timeout (optional)
 * @param  Number         timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        var content = this.getPageContent();
        if (utils.isRegExp(pattern)) {
            return pattern.test(content);
        }
        return content.indexOf(pattern) !== -1;
    }, then, onTimeout, timeout, { text: pattern });
};

/**
 * Waits until the text on an element matching the provided DOM CSS3/XPath selector
 * is changed to a different value.
 *
 * @param String    selector    A DOM CSS3/XPath selector
 * @param Function  then        The next step to preform (optional)
 * @param Function  onTimeout   A callback function to call on timeout (optional)
 * @param Number    timeout     The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitForSelectorTextChange = function(selector, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    var currentSelectorText = this.fetchText(selector);
    return this.waitFor(function _check() {
        return currentSelectorText !== this.fetchText(selector);
    }, then, onTimeout, timeout, { selectorTextChange: selector });
};

/**
 * Waits until an element matching the provided DOM CSS3/XPath selector does not
 * exist in the remote DOM to process a next step.
 *
 * @param  String    selector   A DOM CSS3/XPath selector
 * @param  Function  then       The next step to perform (optional)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitWhileSelector = function waitWhileSelector(selector, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        return !this.exists(selector);
    }, then, onTimeout, timeout, {
        selector: selector,
        waitWhile: true
    });
};

/**
 * Waits until an element matching the provided DOM CSS3/XPath selector is
 * visible in the remote DOM to process a next step.
 *
 * @param  String    selector   A DOM CSS3/XPath selector
 * @param  Function  then       The next step to perform (optional)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitUntilVisible = function waitUntilVisible(selector, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        return this.visible(selector);
    }, then, onTimeout, timeout, { visible: selector });
};

/**
 * Waits until an element matching the provided DOM CSS3/XPath selector is no
 * longer visible in remote DOM to process a next step.
 *
 * @param  String    selector   A DOM CSS3/XPath selector
 * @param  Function  then       The next step to perform (optional)
 * @param  Function  onTimeout  A callback function to call on timeout (optional)
 * @param  Number    timeout    The max amount of time to wait, in milliseconds (optional)
 * @return Casper
 */
Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, onTimeout, timeout) {
    "use strict";
    this.checkStarted();
    timeout = timeout ? timeout : this.options.waitTimeout;
    return this.waitFor(function _check() {
        return !this.visible(selector);
    }, then, onTimeout, timeout, {
        visible: selector,
        waitWhile: true
    });
};

/**
 * Makes the provided frame page as the currently active one. Note that the
 * active page will be reverted when finished.
 *
 * @param  String|Number    frameInfo  Target frame name or number
 * @param  Function  then       Next step function
 * @return Casper
 */
Casper.prototype.withFrame = function withFrame(frameInfo, then) {
    "use strict";
    this.then(function _step() {
        if (utils.isNumber(frameInfo)) {
            if (frameInfo > this.page.childFramesCount() - 1) {
                throw new CasperError(f('Frame number "%d" is out of bounds.', frameInfo));
            }
        } else if (this.page.childFramesName().indexOf(frameInfo) === -1) {
            throw new CasperError(f('No frame named "%s" was found.', frameInfo));
        }
        // make the frame page the currently active one
        this.page.switchToChildFrame(frameInfo);
    });
    try {
        this.then(then);
    } catch (e) {
        // revert to main page on error
        this.warn("Error while processing frame step: " + e);
        this.page.switchToParentFrame();
        throw e;
    }
    return this.then(function _step() {
        // revert to main page
        this.page.switchToParentFrame();
    });
};

/**
 * Makes the provided frame page as the currently active one. Note that the
 * active page will be reverted when finished.
 *
 * @param  String|RegExp|WebPage  popup  Target frame page information
 * @param  Function               then   Next step function
 * @return Casper
 */
Casper.prototype.withPopup = function withPopup(popupInfo, then) {
    "use strict";
    this.then(function _step() {
        var popupPage = this.popups.find(popupInfo);
        if (!utils.isFunction(then)) {
            throw new CasperError("withPopup() requires a step function.");
        }
        // make the popup page the currently active one
        this.page = popupPage;
    });
    try {
        this.then(then);
    } catch (e) {
        // revert to main page on error
        this.log("error while processing popup step: " + e, "error");
        this.page = this.mainPage;
        throw e;
    }
    return this.then(function _step() {
        // revert to main page
        this.page = this.mainPage;
    });
};

/**
 * Changes the current page zoom factor.
 *
 * @param  Number  factor  The zoom factor
 * @return Casper
 */
Casper.prototype.zoom = function zoom(factor) {
    "use strict";
    this.checkStarted();
    if (!utils.isNumber(factor) || factor <= 0) {
        throw new CasperError("Invalid zoom factor: " + factor);
    }
    this.page.zoomFactor = factor;
    return this;
};

/**
 * Extends Casper's prototype with provided one.
 *
 * @param  Object  proto  Prototype methods to add to Casper
 * @deprecated
 * @since 0.6
 */
Casper.extend = function(proto) {
    "use strict";
    this.emit("deprecated", "Casper.extend() has been deprecated since 0.6; check the docs")
    if (!utils.isObject(proto)) {
        throw new CasperError("extends() only accept objects as prototypes");
    }
    utils.mergeObjects(Casper.prototype, proto);
};

exports.Casper = Casper;

/**
 * Creates a new WebPage instance for Casper use.
 *
 * @param  Casper  casper  A Casper instance
 * @return WebPage
 */
function createPage(casper) {
    /*jshint maxstatements:20*/
    "use strict";
    var page = require('webpage').create();
    page.onAlert = function onAlert(message) {
        casper.log('[alert] ' + message, "info", "remote");
        casper.emit('remote.alert', message);
        if (utils.isFunction(casper.options.onAlert)) {
            casper.options.onAlert.call(casper, casper, message);
        }
    };
    page.onConfirm = function onConfirm(message) {
        if ('page.confirm' in casper._filters) {
            return casper.filter('page.confirm', message);
        }
        return true;
    };
    page.onConsoleMessage = function onConsoleMessage(msg) {
        // client utils casper console message
        var consoleTest = /^\[casper\.echo\]\s?([\s\S]*)/.exec(msg);
        if (consoleTest && consoleTest.length === 2) {
            casper.echo(consoleTest[1]);
            return; // don't trigger remote.message event for these
        }
        // client utils log messages
        var logLevel = "info",
            logTest = /^\[casper:(\w+)\]\s?([\s\S]*)/m.exec(msg);
        if (logTest && logTest.length === 3) {
            logLevel = logTest[1];
            msg = logTest[2];
            casper.log(msg, logLevel, "remote");
        } else {
            casper.emit('remote.message', msg);
        }
    };

    page.onCallback = function onCallback(data){
        casper.emit('remote.callback',data);
    };

    page.onError = function onError(msg, trace) {
        casper.emit('page.error', msg, trace);
    };
    page.onInitialized = function onInitialized() {
        casper.emit('page.initialized', page);
        if (utils.isFunction(casper.options.onPageInitialized)) {
            casper.log("Post-configuring WebPage instance", "debug");
            casper.options.onPageInitialized.call(casper, page);
        }
    };
    page.onLoadStarted = function onLoadStarted() {
        // in some case, there is no navigation requested event, so
        // be sure that browserInitializing is false to not block checkStep()
        casper.browserInitializing = false;
        casper.loadInProgress = true;
        casper.emit('load.started');
    };
    page.onLoadFinished = function onLoadFinished(status) {
        /*jshint maxstatements:20*/
        if (status !== "success") {
            casper.emit('load.failed', {
                status:      status,
                http_status: casper.currentHTTPStatus,
                url:         casper.requestUrl
            });
            var message = 'Loading resource failed with status=' + status;
            if (casper.currentHTTPStatus) {
                message += f(' (HTTP %d)', casper.currentHTTPStatus);
            }
            message += ': ' + casper.requestUrl;
            casper.log(message, "warning");
            casper.navigationRequested = false;
            casper.browserInitializing = false;
            if (utils.isFunction(casper.options.onLoadError)) {
                casper.options.onLoadError.call(casper, casper, casper.requestUrl, status);
            }
        }
        // local client scripts
        casper.injectClientScripts();
        // remote client scripts
        casper.includeRemoteScripts();
        // Client-side utils injection
        casper.injectClientUtils();
        // history
        casper.history.push(casper.getCurrentUrl());
        casper.emit('load.finished', status);
        casper.loadInProgress = false;
    };
    page.onNavigationRequested = function onNavigationRequested(url, type, willNavigate, isMainFrame) {
        casper.log(f('Navigation requested: url=%s, type=%s, willNavigate=%s, isMainFrame=%s',
                     url, type, willNavigate, isMainFrame), "debug");
        casper.browserInitializing = false;
        if (isMainFrame && casper.requestUrl !== url) {
            var currentUrl = casper.requestUrl;
            var newUrl = url;
            var pos = currentUrl.indexOf('#')
            if (pos !== -1) {
                currentUrl = currentUrl.substring(0, pos);
            }
            pos = newUrl.indexOf('#')
            if (pos !== -1) {
                newUrl = newUrl.substring(0, pos);
            }
            // for URLs that are only different by their hash part
            // or if navigation locked (willNavigate == false)
            // don't turn navigationRequested to true, because
            // there will not be loadStarted, loadFinished events
            // so it could cause issues (for exemple, checkStep that
            // do no execute the next step -> infinite loop on checkStep)
            if (willNavigate && currentUrl !== newUrl)
                casper.navigationRequested  = true;

            if (willNavigate) {
                casper.requestUrl = url;
            }
        }
        casper.emit('navigation.requested', url, type, willNavigate, isMainFrame);
    };
    page.onPageCreated = function onPageCreated(popupPage) {
        casper.emit('popup.created', popupPage);
        popupPage.onLoadFinished = function onLoadFinished() {
            // SlimerJS needs this line of code because of issue
            // https://github.com/laurentj/slimerjs/issues/48
            // else checkStep turns into an infinite loop
            // after clicking on an <a target="_blank">
            casper.navigationRequested  = false;
            casper.popups.push(popupPage);
            casper.emit('popup.loaded', popupPage);
        };
        popupPage.onClosing = function onClosing(closedPopup) {
            casper.popups.clean(closedPopup);
            casper.emit('popup.closed', closedPopup);
        };
    };
    page.onPrompt = function onPrompt(message, value) {
        return casper.filter('page.prompt', message, value);
    };
    page.onResourceReceived = function onResourceReceived(resource) {
        http.augmentResponse(resource);
        casper.emit('resource.received', resource);
        if (utils.isFunction(casper.options.onResourceReceived)) {
            casper.options.onResourceReceived.call(casper, casper, resource);
        }
        casper.handleReceivedResource(resource);
    };
    page.onResourceRequested = function onResourceRequested(requestData, request) {
        casper.emit('resource.requested', requestData, request);
        if (requestData.url === casper.requestUrl) {
            casper.emit('page.resource.requested', requestData, request);
        }
        if (utils.isFunction(casper.options.onResourceRequested)) {
            casper.options.onResourceRequested.call(casper, casper, requestData, request);
        }
    };
    page.onResourceError = function onResourceError(resourceError) {
        casper.emit('resource.error', resourceError);
    };
    page.onUrlChanged = function onUrlChanged(url) {
        casper.log(f('url changed to "%s"', url), "debug");
        casper.navigationRequested = false;
        casper.emit('url.changed', url);
    };
    casper.emit('page.created', page);
    return page;
}