├── .gitignore ├── README.md ├── browsermob-proxy-2.0.0 ├── LICENSE.txt ├── README.md ├── README.txt ├── bin │ ├── browsermob-proxy │ ├── browsermob-proxy.bat │ └── conf │ │ └── bmp-logging.properties ├── lib │ ├── browsermob-proxy-2.0.0.pom │ └── maven-metadata-appassembler.xml └── ssl-support │ ├── blank_crl.dec │ ├── blank_crl.pem │ ├── cybervillainsCA.cer │ └── cybervillainsCA.jks ├── config.py ├── local_testing_binaries ├── linux_32 │ └── BrowserStackLocal ├── linux_64 │ └── BrowserStackLocal └── osx │ └── BrowserStackLocal ├── run.py ├── screenshots └── README.md └── tests ├── __init__.py ├── base_test.py ├── example_a.py └── example_b.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | screenshots/*.png 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python-selenium-starter 2 | ========= 3 | Starter set up for writing selenium tests with Python. The Goal is to make it easier to write and run tests 4 | using Selenium for multiple browsers and devices. With this repo, you should be able to just start writing your tests 5 | and not be concerned with the basic set up of your test framework. This has options to also run on browserstack but could easily be modified to use another service with some elementary coding. 6 | 7 | Installing Selenium 8 | ---------- 9 | - Install pip first if needed obviously (google is your friend here) 10 | - pip install -U selenium 11 | 12 | 13 | Getting Started 14 | ----------- 15 | - You can add more tests by adding modules to the tests directory. Copy the structure in the examples and your tests 16 | should be executed automagically by the above. 17 | - You will likely want to update the data in the config.py file. But should not need to update anything in the 18 | run.py file unless you are applying a general fix to this repo that you plan to contribute.. 19 | 20 | General Examples 21 | --------- 22 | To run all tests on Firefox 23 | ``` 24 | python run.py 25 | ``` 26 | To override the base url you set in run.py 27 | ``` 28 | python run.py --base_url "http://google.com" 29 | ```` 30 | To only run one test 31 | ``` 32 | python run.py --test "example_a.py" 33 | ```` 34 | To see more options 35 | ``` 36 | python run.py --help 37 | ``` 38 | 39 | Browserstack options 40 | ========= 41 | - Before anything, you need to get your Selenium automate username and automate value from the following URL.https://www.browserstack.com/accounts/automate 42 | - Once you have those values modify your ~/.bash_profile to end with the following. (use ~/.bashrc instead if on linux) 43 | ``` 44 | export SELENIUM_AUTOMATE_USERNAME="REPLACE_THIS" 45 | export SELENIUM_AUTOMATE_VALUE="REPLACE_THIS" 46 | ``` 47 | - Then run the following in your terminal (remember to modify to ~/.bashrc if on linux) 48 | ``` 49 | source ~/.bash_profile 50 | ``` 51 | 52 | Open a tab and run the following to start the local testing server. That will make sure you can still access the site via the correct IP address, etc. 53 | ``` 54 | ./local_testing_binaries/osx/BrowserStackLocal $SELENIUM_AUTOMATE_VALUE localhost,3000,0 -forcelocal -vv 55 | ``` 56 | Then Run the following to execute the tests and hook into the browser stack local testing server 57 | ``` 58 | python run.py --browserstack --use_local 59 | ``` 60 | 61 | Browserstack Examples 62 | -------- 63 | To run with only in the desktop browsers: 64 | ``` 65 | python run.py --browserstack --mobile 66 | ``` 67 | To run with only the in the desktop browsers: 68 | ``` 69 | python run.py --browserstack --desktop 70 | ``` 71 | To override the base url you set in run.py 72 | ``` 73 | python run.py --browserstack --capabilities "{'os_version': '7', 'browser_version': '8.0', 'os': 'Windows', 'resolution': '1024x768', 'browser': 'IE'}" 74 | ```` 75 | 76 | Browserstack Notes 77 | ---------- 78 | - For more browserstack options, you can see more details here https://www.browserstack.com/automate/python. 79 | - You will notice that your username and value are hard coded in those examples though. Just replace those areas with the environment variables we set up to make your code more shareable. 80 | - Local testing is VERY slow... Do as you wish 81 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | 204 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/README.md: -------------------------------------------------------------------------------- 1 | BrowserMob Proxy 2 | ================ 3 | 4 | BrowserMob Proxy is a simple utility that makes it easy to capture performance data from browsers, typically written using automation toolkits such as Selenium and Watir. 5 | 6 | Features 7 | -------- 8 | 9 | The proxy is programmatically controlled via a REST interface or by being embedded directly inside Java-based programs and unit tests. It captures performance data the [HAR format](http://groups.google.com/group/http-archive-specification). It addition it also can actually control HTTP traffic, such as: 10 | 11 | - blacklisting and whitelisting certain URL patterns 12 | - simulating various bandwidth and latency 13 | - remapping DNS lookups 14 | - flushing DNS caching 15 | - controlling DNS and request timeouts 16 | - automatic BASIC authorization 17 | 18 | REST API 19 | -------- 20 | 21 | To get started, first start the proxy by running `browsermob-proxy` or `browsermob-proxy.bat` in the bin directory: 22 | 23 | $ sh browsermob-proxy -port 9090 24 | INFO 05/31 03:12:48 o.b.p.Main - Starting up... 25 | 2011-05-30 20:12:49.517:INFO::jetty-7.3.0.v20110203 26 | 2011-05-30 20:12:49.689:INFO::started o.e.j.s.ServletContextHandler{/,null} 27 | 2011-05-30 20:12:49.820:INFO::Started SelectChannelConnector@0.0.0.0:9090 28 | 29 | Once started, there won't be an actual proxy running until you create a new proxy. You can do this by POSTing to /proxy: 30 | 31 | [~]$ curl -X POST http://localhost:9090/proxy 32 | {"port":9091} 33 | 34 | or optionally specify your own port: 35 | 36 | [~]$ curl -X POST -d 'port=9099' http://localhost:9090/proxy 37 | {"port":9099} 38 | 39 | or if running BrowserMob Proxy in a multi-homed environment, specify a desired bind address (default is `0.0.0.0`): 40 | 41 | [~]$ curl -X POST -d 'bindAddress=192.168.1.222' http://localhost:9090/proxy 42 | {"port":9096} 43 | 44 | Once that is done, a new proxy will be available on the port returned. All you have to do is point a browser to that proxy on that port and you should be able to browse the internet. The following additional APIs will then be available: 45 | 46 | - GET /proxy - get a list of ports attached to `ProxyServer` instances managed by `ProxyManager` 47 | - PUT /proxy/[port]/har - creates a new HAR attached to the proxy and returns the HAR content if there was a previous HAR. Supports the following parameters: 48 | - initialPageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page 1". 49 | - captureHeaders - Boolean, capture headers 50 | - captureContent - Boolean, capture content bodies 51 | - captureBinaryContent - Boolean, capture binary content 52 | - PUT /proxy/[port]/har/pageRef - starts a new page on the existing HAR. Supports the following parameters: 53 | - pageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page N" where N is the next page number. 54 | - DELETE /proxy/[port] - shuts down the proxy and closes the port 55 | - GET /proxy/[port]/har - returns the JSON/HAR content representing all the HTTP traffic passed through the proxy 56 | - GET /proxy/[port]/whitelist - Displays whitelisted items 57 | - PUT /proxy/[port]/whitelist - Sets a list of URL patterns to whitelist. Takes the following parameters: 58 | - regex - a comma separated list of regular expressions 59 | - status - the HTTP status code to return for URLs that do not match the whitelist 60 | - DELETE /proxy/[port]/whitelist - Clears all URL patterns from the whitelist 61 | - GET /proxy/[port]/blacklist - Displays blacklisted items 62 | - PUT /proxy/[port]/blacklist - Set a URL to blacklist. Takes the following parameters: 63 | - regex - the blacklist regular expression 64 | - status - the HTTP status code to return for URLs that are blacklisted 65 | - method - regular expression for matching method., e.g., POST. Emtpy for matching all method. 66 | - DELETE /proxy/[port]/blacklist - Clears all URL patterns from the blacklist 67 | - PUT /proxy/[port]/limit - Limit the bandwidth through the proxy. Takes the following parameters: 68 | - downstreamKbps - Sets the downstream bandwidth limit in kbps 69 | - upstreamKbps - Sets the upstream bandwidth limit kbps 70 | - downstreamMaxKB - Specifies how many kilobytes in total the client is allowed to download through the proxy. 71 | - upstreamMaxKB - Specifies how many kilobytes in total the client is allowed to upload through the proxy. 72 | - latency - Add the given latency to each HTTP request 73 | - enable - (true/false) a boolean that enable bandwidth limiter. By default the limit is disabled, although setting any of the properties above will implicitly enable throttling 74 | - payloadPercentage - a number ]0, 100] specifying what percentage of data sent is payload. e.g. use this to take into account overhead due to tcp/ip. 75 | - maxBitsPerSecond - The max bits per seconds you want this instance of StreamManager to respect. 76 | - GET /proxy/[port]/limit - Displays the amount of data remaining to be uploaded/downloaded until the limit is reached. 77 | - POST /proxy/[port]/headers - Set and override HTTP Request headers. For example setting a custom User-Agent. 78 | - Payload data should be json encoded set of headers (not url-encoded) 79 | - POST /proxy/[port]/hosts - Overrides normal DNS lookups and remaps the given hosts with the associated IP address 80 | - Payload data should be json encoded set of name/value pairs (ex: {"example.com": "1.2.3.4"}) 81 | - POST /proxy/[port]/auth/basic/[domain] - Sets automatic basic authentication for the specified domain 82 | - Payload data should be json encoded username and password name/value pairs (ex: {"username": "myUsername", "password": "myPassword"} 83 | - PUT /proxy/[port]/wait - wait till all request are being made 84 | - quietPeriodInMs - Sets quiet period in milliseconds 85 | - timeoutInMs - Sets timeout in milliseconds 86 | - PUT /proxy/[port]/timeout - Handles different proxy timeouts. Takes the following parameters: 87 | - requestTimeout - request timeout in milliseconds. A timeout value of -1 is interpreted as infinite timeout. It equals -1 by default. 88 | - readTimeout - read timeout in milliseconds. Which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). A timeout value of zero is interpreted as an infinite timeout. It equals 60000 by default 89 | - connectionTimeout - Determines the timeout in milliseconds until a connection is established. A timeout value of zero is interpreted as an infinite timeout. It eqauls 60000 by default 90 | - dnsCacheTimeout - Sets the maximum length of time that records will be stored in this Cache. A nonpositive value disables this feature (that is, sets no limit). It equals 0 y default 91 | - PUT /proxy/[port]/rewrite - Redirecting URL's 92 | - matchRegex - a matching URL regular expression 93 | - replace - replacement URL 94 | - DELETE /proxy/[port]/rewrite - Removes all URL redirection rules currently in effect 95 | - PUT /proxy/[port]/retry - Setting the retry count 96 | - retrycount - the number of times a method will be retried 97 | - DELETE /proxy/[port]/dns/cache - Empties the Cache. 98 | 99 | For example, once you've started the proxy you can create a new HAR to start recording data like so: 100 | 101 | [~]$ curl -X PUT -d 'initialPageRef=Foo' http://localhost:8080/proxy/9091/har 102 | 103 | Now when traffic goes through port 9091 it will be attached to a page reference named "Foo". Consult the HAR specification for more info on what a "pageRef" is. You can also start a new pageRef like so: 104 | 105 | [~]$ curl -X PUT -d 'pageRef=Bar' http://localhost:8080/proxy/9091/har/pageRef 106 | 107 | That will ensure no more HTTP requests get attached to the old pageRef (Foo) and start getting attached to the new pageRef (Bar). You can also get the HAR content at any time like so: 108 | 109 | [~]$ curl http://localhost:8080/proxy/9091/har 110 | 111 | Sometimes you will want to route requests through an upstream proxy server. In this case specify your proxy server by adding the httpProxy parameter to your create proxy request: 112 | 113 | [~]$ curl -X POST http://localhost:9090/proxy?httpProxy=yourproxyserver.com:8080 114 | {"port":9091} 115 | 116 | Alternatively, you can specify the upstream proxy config for all proxies created using the standard JVM [system properties for HTTP proxies](http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html). 117 | Note that you can still override the default upstream proxy via the POST payload, but if you omit the payload the JVM 118 | system properties will be used to specify the upstream proxy. 119 | 120 | *TODO*: Other REST APIs supporting all the BrowserMob Proxy features will be added soon. 121 | 122 | Command-line Arguments 123 | ---------------------- 124 | 125 | - -port \ 126 | - Port on which the API listens. Default value is 8080. 127 | - -address
128 | - Address to which the API is bound. Default value is 0.0.0.0. 129 | - -proxyPortRange \-\ 130 | - Range of ports reserved for proxies. Only applies if *port* parameter is not supplied in the POST request. Default values are \+1 to \+500+1. 131 | - -ttl \ 132 | - Proxy will be automatically deleted after a specified time period. Off by default. 133 | 134 | Embedded Mode 135 | ------------- 136 | 137 | If you're using Java and Selenium, the easiest way to get started is to embed the project directly in your test. First, you'll need to make sure that all the dependencies are imported in to the project. You can find them in the *lib* directory. Or, if you're using Maven, you can add this to your pom: 138 | 139 | 140 | net.lightbody.bmp 141 | browsermob-proxy 142 | LATEST_VERSION (ex: 2.0-beta-9) 143 | test 144 | 145 | 146 | Once done, you can start a proxy using `net.lightbody.bmp.proxy.ProxyServer`: 147 | 148 | ProxyServer server = new ProxyServer(9090); 149 | server.start(); 150 | 151 | This class supports every feature that the proxy supports. In fact, the REST API is a subset of the methods exposed here, so new features will show up here before they show up in the REST API. Consult the Javadocs for the full API. 152 | 153 | If your project already defines a Selenium dependency then you may want to exclude the version that browsermob-proxy pulls in, like so: 154 | 155 | 156 | net.lightbody.bmp 157 | browsermob-proxy 158 | LATEST_VERSION (ex: 2.0-beta-9) 159 | test 160 | 161 | 162 | org.seleniumhq.selenium 163 | selenium-api 164 | 165 | 166 | 167 | 168 | 169 | Using With Selenium 170 | ------------------- 171 | 172 | You can use the REST API with Selenium however you want. But if you're writing your tests in Java and using Selenium 2, this is the easiest way to use it: 173 | 174 | // start the proxy 175 | ProxyServer server = new ProxyServer(4444); 176 | server.start(); 177 | 178 | // get the Selenium proxy object 179 | Proxy proxy = server.seleniumProxy(); 180 | 181 | // configure it as a desired capability 182 | DesiredCapabilities capabilities = new DesiredCapabilities(); 183 | capabilities.setCapability(CapabilityType.PROXY, proxy); 184 | 185 | // start the browser up 186 | WebDriver driver = new FirefoxDriver(capabilities); 187 | 188 | // create a new HAR with the label "yahoo.com" 189 | server.newHar("yahoo.com"); 190 | 191 | // open yahoo.com 192 | driver.get("http://yahoo.com"); 193 | 194 | // get the HAR data 195 | Har har = server.getHar(); 196 | 197 | 198 | HTTP Request Manipulation 199 | ------------------- 200 | 201 | You can manipulate the requests like so: 202 | 203 | server.addRequestInterceptor(new RequestInterceptor() { 204 | @Override 205 | public void process(BrowserMobHttpRequest request, Har har) { 206 | request.getMethod().removeHeaders("User-Agent"); 207 | request.getMethod().addHeader("User-Agent", "Bananabot/1.0"); 208 | } 209 | }); 210 | 211 | You can also POST a JavaScript payload to `/:port/interceptor/request` and `/:port/interceptor/response` using the REST interface. The functions will have a `request`/`response` variable, respectively, and a `har` variable (which may be null if a HAR isn't set up yet). The JavaScript code will be run by [Rhino](https://github.com/mozilla/rhino) and have access to the same Java API in the example above: 212 | 213 | [~]$ curl -X POST -H 'Content-Type: text/plain' -d 'request.getMethod().removeHeaders("User-Agent");' http://localhost:9090/proxy/9091/interceptor/request 214 | 215 | Consult the Java API docs for more info. 216 | 217 | SSL Support 218 | ----------- 219 | 220 | While the proxy supports SSL, it requires that a Certificate Authority be installed in to the browser. This allows the browser to trust all the SSL traffic coming from the proxy, which will be proxied using a classic man-in-the-middle technique. IT IS CRITICAL THAT YOU NOT INSTALL THIS CERTIFICATE AUTHORITY ON A BROWSER THAT IS USED FOR ANYTHING OTHER THAN TESTING. 221 | 222 | If you're doing testing with Selenium, you'll want to make sure that the browser profile that gets set up by Selenium not only has the proxy configured, but also has the CA installed (Firefox set up by Selenium has installed CA by default). Unfortuantely, there is no API for doing this in Selenium, so you'll have to solve it uniquely for each browser type. We hope to make this easier in upcoming releases. 223 | 224 | NodeJS Support 225 | -------------- 226 | 227 | NodeJS bindings for browswermob-proxy are available [here](https://github.com/zzo/browsermob-node). Built-in support for [Selenium](http://seleniumhq.org) or use [CapserJS-on-PhantomJS](http://casperjs.org) or anything else to drive traffic for HAR generation. 228 | 229 | Logging 230 | ------- 231 | 232 | When running in stand-alone mode, the proxy loads the default logging configuration from the conf/bmp-logging.properties file. To increase/decrease the logging level, change the logging entry for net.lightbody.bmp. 233 | 234 | If you are running the proxy with Selenium or another application, you can configure BrowserMob Proxy to use your preferred logger. You'll need to suppress the slf4j-jdk14 dependency that is included by default: 235 | 236 | 237 | net.lightbody.bmp 238 | browsermob-proxy 239 | LATEST_VERSION (ex: 2.0-beta-9) 240 | test 241 | 242 | 243 | org.seleniumhq.selenium 244 | selenium-api 245 | 246 | 247 | org.slf4j 248 | slf4j-jdk14 249 | 250 | 251 | 252 | 253 | Native DNS Resolution 254 | --------------------- 255 | 256 | If you are having name resolution issues with the proxy, you can use native Java name resolution as a fallback to browsermob-proxy's default XBill resolution. To enable native DNS fallback, set the JVM property `bmp.allowNativeDnsFallback` to `true`. 257 | 258 | When running from the command line: 259 | 260 | $ JAVA_OPTS="-Dbmp.allowNativeDnsFallback=true" sh browsermob-proxy 261 | 262 | or in Windows: 263 | 264 | C:\browsermob-proxy\bin> set JAVA_OPTS="-Dbmp.allowNativeDnsFallback=true" 265 | C:\browsermob-proxy\bin> browsermob-proxy.bat 266 | 267 | If you are running within a Selenium test, you can set the `bmp.allowNativeDnsFallback` JVM property when you launch your Selenium tests, or programmatically when creating the Proxy: 268 | 269 | System.setProperty("bmp.allowNativeDnsFallback", "true"); 270 | ProxyServer proxyServer = new ProxyServer(0); 271 | proxyServer.start(); 272 | 273 | Creating the batch files from source 274 | ------------------------------------ 275 | 276 | You'll need maven (`brew install maven` if you're on OS X); use the `release` profile to generate the batch files from this repository. Optionally, proceed at your own risk and append the `-DskipTests` option if the tests are failing. 277 | 278 | [~]$ mvn -P release 279 | [~]$ mvn -DskipTests -P release 280 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/README.txt: -------------------------------------------------------------------------------- 1 | Please see README.md (in Markdown format) or view it at: 2 | 3 | https://github.com/lightbody/browsermob-proxy -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/bin/browsermob-proxy: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Copyright 2001-2006 The Apache Software Foundation. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # ---------------------------------------------------------------------------- 17 | 18 | # Copyright (c) 2001-2002 The Apache Software Foundation. All rights 19 | # reserved. 20 | 21 | BASEDIR=`dirname $0`/.. 22 | BASEDIR=`(cd "$BASEDIR"; pwd)` 23 | 24 | 25 | 26 | # OS specific support. $var _must_ be set to either true or false. 27 | cygwin=false; 28 | darwin=false; 29 | case "`uname`" in 30 | CYGWIN*) cygwin=true ;; 31 | Darwin*) darwin=true 32 | if [ -z "$JAVA_VERSION" ] ; then 33 | JAVA_VERSION="CurrentJDK" 34 | else 35 | echo "Using Java version: $JAVA_VERSION" 36 | fi 37 | if [ -z "$JAVA_HOME" ] ; then 38 | JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home 39 | fi 40 | ;; 41 | esac 42 | 43 | if [ -z "$JAVA_HOME" ] ; then 44 | if [ -r /etc/gentoo-release ] ; then 45 | JAVA_HOME=`java-config --jre-home` 46 | fi 47 | fi 48 | 49 | # For Cygwin, ensure paths are in UNIX format before anything is touched 50 | if $cygwin ; then 51 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 52 | [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 53 | fi 54 | 55 | # If a specific java binary isn't specified search for the standard 'java' binary 56 | if [ -z "$JAVACMD" ] ; then 57 | if [ -n "$JAVA_HOME" ] ; then 58 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 59 | # IBM's JDK on AIX uses strange locations for the executables 60 | JAVACMD="$JAVA_HOME/jre/sh/java" 61 | else 62 | JAVACMD="$JAVA_HOME/bin/java" 63 | fi 64 | else 65 | JAVACMD=`which java` 66 | fi 67 | fi 68 | 69 | if [ ! -x "$JAVACMD" ] ; then 70 | echo "Error: JAVA_HOME is not defined correctly." 71 | echo " We cannot execute $JAVACMD" 72 | exit 1 73 | fi 74 | 75 | if [ -z "$REPO" ] 76 | then 77 | REPO="$BASEDIR"/lib 78 | fi 79 | 80 | CLASSPATH=$CLASSPATH_PREFIX:"$BASEDIR"/etc:"$REPO"/slf4j-api-1.7.7.jar:"$REPO"/slf4j-jdk14-1.7.7.jar:"$REPO"/sitebricks-0.8.9.jar:"$REPO"/sitebricks-converter-0.8.9.jar:"$REPO"/sitebricks-client-0.8.9.jar:"$REPO"/xstream-1.3.1.jar:"$REPO"/xpp3_min-1.1.4c.jar:"$REPO"/sitebricks-annotations-0.8.9.jar:"$REPO"/mvel2-2.1.3.Final.jar:"$REPO"/guava-15.0.jar:"$REPO"/annotations-7.0.3.jar:"$REPO"/async-http-client-1.6.3.jar:"$REPO"/netty-3.2.4.Final.jar:"$REPO"/jsoup-1.5.2.jar:"$REPO"/validation-api-1.0.0.GA.jar:"$REPO"/guice-multibindings-3.0.jar:"$REPO"/jackson-core-2.4.4.jar:"$REPO"/jackson-databind-2.4.4.jar:"$REPO"/jackson-annotations-2.4.0.jar:"$REPO"/httpclient-4.3.4.jar:"$REPO"/httpcore-4.3.2.jar:"$REPO"/commons-logging-1.1.3.jar:"$REPO"/commons-codec-1.6.jar:"$REPO"/httpmime-4.3.4.jar:"$REPO"/jopt-simple-3.2.jar:"$REPO"/ant-1.8.2.jar:"$REPO"/ant-launcher-1.8.2.jar:"$REPO"/bcprov-jdk15on-1.47.jar:"$REPO"/jetty-server-7.3.0.v20110203.jar:"$REPO"/servlet-api-2.5.jar:"$REPO"/jetty-continuation-7.3.0.v20110203.jar:"$REPO"/jetty-http-7.3.0.v20110203.jar:"$REPO"/jetty-io-7.3.0.v20110203.jar:"$REPO"/jetty-util-7.3.0.v20110203.jar:"$REPO"/jetty-servlet-7.3.0.v20110203.jar:"$REPO"/jetty-security-7.3.0.v20110203.jar:"$REPO"/guice-3.0.jar:"$REPO"/javax.inject-1.jar:"$REPO"/aopalliance-1.0.jar:"$REPO"/guice-servlet-3.0.jar:"$REPO"/jcip-annotations-1.0.jar:"$REPO"/selenium-api-2.43.0.jar:"$REPO"/json-20080701.jar:"$REPO"/uadetector-resources-2014.10.jar:"$REPO"/uadetector-core-0.9.22.jar:"$REPO"/quality-check-1.3.jar:"$REPO"/jsr305-2.0.3.jar:"$REPO"/jsr250-api-1.0.jar:"$REPO"/arquillian-phantom-driver-1.1.1.Final.jar:"$REPO"/shrinkwrap-resolver-api-2.0.0.jar:"$REPO"/shrinkwrap-resolver-spi-2.0.0.jar:"$REPO"/shrinkwrap-resolver-api-maven-2.0.0.jar:"$REPO"/shrinkwrap-resolver-spi-maven-2.0.0.jar:"$REPO"/shrinkwrap-resolver-impl-maven-2.0.0.jar:"$REPO"/aether-api-1.13.1.jar:"$REPO"/aether-impl-1.13.1.jar:"$REPO"/aether-spi-1.13.1.jar:"$REPO"/aether-util-1.13.1.jar:"$REPO"/aether-connector-wagon-1.13.1.jar:"$REPO"/maven-aether-provider-3.0.5.jar:"$REPO"/maven-model-3.0.5.jar:"$REPO"/maven-model-builder-3.0.5.jar:"$REPO"/maven-repository-metadata-3.0.5.jar:"$REPO"/maven-settings-3.0.5.jar:"$REPO"/maven-settings-builder-3.0.5.jar:"$REPO"/plexus-interpolation-1.14.jar:"$REPO"/plexus-utils-2.0.6.jar:"$REPO"/plexus-sec-dispatcher-1.4.jar:"$REPO"/plexus-cipher-1.4.jar:"$REPO"/wagon-provider-api-2.4.jar:"$REPO"/wagon-file-2.4.jar:"$REPO"/wagon-http-lightweight-2.4.jar:"$REPO"/wagon-http-shared4-2.4.jar:"$REPO"/shrinkwrap-resolver-impl-maven-archive-2.0.0.jar:"$REPO"/shrinkwrap-impl-base-1.1.2.jar:"$REPO"/shrinkwrap-api-1.1.2.jar:"$REPO"/shrinkwrap-spi-1.1.2.jar:"$REPO"/shrinkwrap-resolver-api-maven-archive-2.0.0.jar:"$REPO"/shrinkwrap-resolver-spi-maven-archive-2.0.0.jar:"$REPO"/plexus-compiler-javac-2.1.jar:"$REPO"/plexus-compiler-api-2.1.jar:"$REPO"/plexus-component-api-1.0-alpha-33.jar:"$REPO"/plexus-classworlds-1.2-alpha-10.jar:"$REPO"/commons-io-2.4.jar:"$REPO"/browsermob-proxy-2.0.0.jar 81 | EXTRA_JVM_ARGUMENTS="" 82 | 83 | # For Cygwin, switch paths to Windows format before running java 84 | if $cygwin; then 85 | [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 86 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 87 | [ -n "$HOME" ] && HOME=`cygpath --path --windows "$HOME"` 88 | [ -n "$BASEDIR" ] && BASEDIR=`cygpath --path --windows "$BASEDIR"` 89 | [ -n "$REPO" ] && REPO=`cygpath --path --windows "$REPO"` 90 | fi 91 | 92 | exec "$JAVACMD" $JAVA_OPTS \ 93 | $EXTRA_JVM_ARGUMENTS \ 94 | -classpath "$CLASSPATH" \ 95 | -Dapp.name="browsermob-proxy" \ 96 | -Dapp.pid="$$" \ 97 | -Dapp.repo="$REPO" \ 98 | -Dbasedir="$BASEDIR" \ 99 | net.lightbody.bmp.proxy.Main \ 100 | "$@" 101 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/bin/browsermob-proxy.bat: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Copyright 2001-2004 The Apache Software Foundation. 3 | @REM 4 | @REM Licensed under the Apache License, Version 2.0 (the "License"); 5 | @REM you may not use this file except in compliance with the License. 6 | @REM You may obtain a copy of the License at 7 | @REM 8 | @REM http://www.apache.org/licenses/LICENSE-2.0 9 | @REM 10 | @REM Unless required by applicable law or agreed to in writing, software 11 | @REM distributed under the License is distributed on an "AS IS" BASIS, 12 | @REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @REM See the License for the specific language governing permissions and 14 | @REM limitations under the License. 15 | @REM ---------------------------------------------------------------------------- 16 | @REM 17 | 18 | @echo off 19 | 20 | set ERROR_CODE=0 21 | 22 | :init 23 | @REM Decide how to startup depending on the version of windows 24 | 25 | @REM -- Win98ME 26 | if NOT "%OS%"=="Windows_NT" goto Win9xArg 27 | 28 | @REM set local scope for the variables with windows NT shell 29 | if "%OS%"=="Windows_NT" @setlocal 30 | 31 | @REM -- 4NT shell 32 | if "%eval[2+2]" == "4" goto 4NTArgs 33 | 34 | @REM -- Regular WinNT shell 35 | set CMD_LINE_ARGS=%* 36 | goto WinNTGetScriptDir 37 | 38 | @REM The 4NT Shell from jp software 39 | :4NTArgs 40 | set CMD_LINE_ARGS=%$ 41 | goto WinNTGetScriptDir 42 | 43 | :Win9xArg 44 | @REM Slurp the command line arguments. This loop allows for an unlimited number 45 | @REM of arguments (up to the command line limit, anyway). 46 | set CMD_LINE_ARGS= 47 | :Win9xApp 48 | if %1a==a goto Win9xGetScriptDir 49 | set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 50 | shift 51 | goto Win9xApp 52 | 53 | :Win9xGetScriptDir 54 | set SAVEDIR=%CD% 55 | %0\ 56 | cd %0\..\.. 57 | set BASEDIR=%CD% 58 | cd %SAVEDIR% 59 | set SAVE_DIR= 60 | goto repoSetup 61 | 62 | :WinNTGetScriptDir 63 | set BASEDIR=%~dp0\.. 64 | 65 | :repoSetup 66 | 67 | 68 | if "%JAVACMD%"=="" set JAVACMD=java 69 | 70 | if "%REPO%"=="" set REPO=%BASEDIR%\lib 71 | 72 | set CLASSPATH="%BASEDIR%"\etc;"%REPO%"\slf4j-api-1.7.7.jar;"%REPO%"\slf4j-jdk14-1.7.7.jar;"%REPO%"\sitebricks-0.8.9.jar;"%REPO%"\sitebricks-converter-0.8.9.jar;"%REPO%"\sitebricks-client-0.8.9.jar;"%REPO%"\xstream-1.3.1.jar;"%REPO%"\xpp3_min-1.1.4c.jar;"%REPO%"\sitebricks-annotations-0.8.9.jar;"%REPO%"\mvel2-2.1.3.Final.jar;"%REPO%"\guava-15.0.jar;"%REPO%"\annotations-7.0.3.jar;"%REPO%"\async-http-client-1.6.3.jar;"%REPO%"\netty-3.2.4.Final.jar;"%REPO%"\jsoup-1.5.2.jar;"%REPO%"\validation-api-1.0.0.GA.jar;"%REPO%"\guice-multibindings-3.0.jar;"%REPO%"\jackson-core-2.4.4.jar;"%REPO%"\jackson-databind-2.4.4.jar;"%REPO%"\jackson-annotations-2.4.0.jar;"%REPO%"\httpclient-4.3.4.jar;"%REPO%"\httpcore-4.3.2.jar;"%REPO%"\commons-logging-1.1.3.jar;"%REPO%"\commons-codec-1.6.jar;"%REPO%"\httpmime-4.3.4.jar;"%REPO%"\jopt-simple-3.2.jar;"%REPO%"\ant-1.8.2.jar;"%REPO%"\ant-launcher-1.8.2.jar;"%REPO%"\bcprov-jdk15on-1.47.jar;"%REPO%"\jetty-server-7.3.0.v20110203.jar;"%REPO%"\servlet-api-2.5.jar;"%REPO%"\jetty-continuation-7.3.0.v20110203.jar;"%REPO%"\jetty-http-7.3.0.v20110203.jar;"%REPO%"\jetty-io-7.3.0.v20110203.jar;"%REPO%"\jetty-util-7.3.0.v20110203.jar;"%REPO%"\jetty-servlet-7.3.0.v20110203.jar;"%REPO%"\jetty-security-7.3.0.v20110203.jar;"%REPO%"\guice-3.0.jar;"%REPO%"\javax.inject-1.jar;"%REPO%"\aopalliance-1.0.jar;"%REPO%"\guice-servlet-3.0.jar;"%REPO%"\jcip-annotations-1.0.jar;"%REPO%"\selenium-api-2.43.0.jar;"%REPO%"\json-20080701.jar;"%REPO%"\uadetector-resources-2014.10.jar;"%REPO%"\uadetector-core-0.9.22.jar;"%REPO%"\quality-check-1.3.jar;"%REPO%"\jsr305-2.0.3.jar;"%REPO%"\jsr250-api-1.0.jar;"%REPO%"\arquillian-phantom-driver-1.1.1.Final.jar;"%REPO%"\shrinkwrap-resolver-api-2.0.0.jar;"%REPO%"\shrinkwrap-resolver-spi-2.0.0.jar;"%REPO%"\shrinkwrap-resolver-api-maven-2.0.0.jar;"%REPO%"\shrinkwrap-resolver-spi-maven-2.0.0.jar;"%REPO%"\shrinkwrap-resolver-impl-maven-2.0.0.jar;"%REPO%"\aether-api-1.13.1.jar;"%REPO%"\aether-impl-1.13.1.jar;"%REPO%"\aether-spi-1.13.1.jar;"%REPO%"\aether-util-1.13.1.jar;"%REPO%"\aether-connector-wagon-1.13.1.jar;"%REPO%"\maven-aether-provider-3.0.5.jar;"%REPO%"\maven-model-3.0.5.jar;"%REPO%"\maven-model-builder-3.0.5.jar;"%REPO%"\maven-repository-metadata-3.0.5.jar;"%REPO%"\maven-settings-3.0.5.jar;"%REPO%"\maven-settings-builder-3.0.5.jar;"%REPO%"\plexus-interpolation-1.14.jar;"%REPO%"\plexus-utils-2.0.6.jar;"%REPO%"\plexus-sec-dispatcher-1.4.jar;"%REPO%"\plexus-cipher-1.4.jar;"%REPO%"\wagon-provider-api-2.4.jar;"%REPO%"\wagon-file-2.4.jar;"%REPO%"\wagon-http-lightweight-2.4.jar;"%REPO%"\wagon-http-shared4-2.4.jar;"%REPO%"\shrinkwrap-resolver-impl-maven-archive-2.0.0.jar;"%REPO%"\shrinkwrap-impl-base-1.1.2.jar;"%REPO%"\shrinkwrap-api-1.1.2.jar;"%REPO%"\shrinkwrap-spi-1.1.2.jar;"%REPO%"\shrinkwrap-resolver-api-maven-archive-2.0.0.jar;"%REPO%"\shrinkwrap-resolver-spi-maven-archive-2.0.0.jar;"%REPO%"\plexus-compiler-javac-2.1.jar;"%REPO%"\plexus-compiler-api-2.1.jar;"%REPO%"\plexus-component-api-1.0-alpha-33.jar;"%REPO%"\plexus-classworlds-1.2-alpha-10.jar;"%REPO%"\commons-io-2.4.jar;"%REPO%"\browsermob-proxy-2.0.0.jar 73 | set EXTRA_JVM_ARGUMENTS= 74 | goto endInit 75 | 76 | @REM Reaching here means variables are defined and arguments have been captured 77 | :endInit 78 | 79 | %JAVACMD% %JAVA_OPTS% %EXTRA_JVM_ARGUMENTS% -classpath %CLASSPATH_PREFIX%;%CLASSPATH% -Dapp.name="browsermob-proxy" -Dapp.repo="%REPO%" -Dbasedir="%BASEDIR%" net.lightbody.bmp.proxy.Main %CMD_LINE_ARGS% 80 | if ERRORLEVEL 1 goto error 81 | goto end 82 | 83 | :error 84 | if "%OS%"=="Windows_NT" @endlocal 85 | set ERROR_CODE=1 86 | 87 | :end 88 | @REM set local scope for the variables with windows NT shell 89 | if "%OS%"=="Windows_NT" goto endNT 90 | 91 | @REM For old DOS remove the set variables from ENV - we assume they were not set 92 | @REM before we started - at least we don't leave any baggage around 93 | set CMD_LINE_ARGS= 94 | goto postExec 95 | 96 | :endNT 97 | @endlocal 98 | 99 | :postExec 100 | 101 | if "%FORCE_EXIT_ON_ERROR%" == "on" ( 102 | if %ERROR_CODE% NEQ 0 exit %ERROR_CODE% 103 | ) 104 | 105 | exit /B %ERROR_CODE% 106 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/bin/conf/bmp-logging.properties: -------------------------------------------------------------------------------- 1 | # specify the handlers to create in the root logger 2 | # (all loggers are children of the root logger) 3 | handlers=java.util.logging.ConsoleHandler 4 | 5 | # set the default logging level for the root logger 6 | .level=INFO 7 | 8 | # to suppress unwanted logging statements, set the log level for the source logger to WARNING or SEVERE. 9 | # to enable more verbose logging, set the log level to FINE, FINER, or FINEST. 10 | net.lightbody.bmp=INFO 11 | 12 | # suppress some logging noise from Jetty 13 | net.lightbody.bmp.proxy.jetty.util.ThreadedServer=WARNING 14 | 15 | # set the default logging level for new ConsoleHandler instances. 16 | java.util.logging.ConsoleHandler.level=FINEST 17 | 18 | # set the default formatter for new ConsoleHandler instances 19 | java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter 20 | 21 | # logging format string. see http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html for details. 22 | java.util.logging.SimpleFormatter.format=[%4$s %1$tF %1$tT.%1$tL %3$s] %5$s%n 23 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/lib/browsermob-proxy-2.0.0.pom: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | net.lightbody.bmp 4 | browsermob-proxy 5 | 2.0.0 6 | BrowserMob Proxy 7 | A programmatic HTTP/S designed for performance and functional testing 8 | http://bmp.lightbody.net 9 | jar 10 | 11 | 12 | org.sonatype.oss 13 | oss-parent 14 | 9 15 | 16 | 17 | 18 | 19 | The Apache Software License, Version 2.0 20 | http://www.apache.org/licenses/LICENSE-2.0.txt 21 | repo 22 | 23 | 24 | 25 | 26 | 27 | Patrick Lightbody 28 | patrick@lightbody.net 29 | http://lightbody.net 30 | PST 31 | 32 | Administrator 33 | 34 | 35 | 36 | 37 | 38 | scm:git:git@github.com:lightbody/browsermob-proxy.git 39 | scm:git:git@github.com:lightbody/browsermob-proxy.git 40 | git@github.com:lightbody/browsermob-proxy.git 41 | browsermob-proxy-2.0.0 42 | 43 | 44 | 45 | 46 | ossrh 47 | https://oss.sonatype.org/content/repositories/snapshots 48 | 49 | 50 | ossrh 51 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 52 | 53 | 54 | 55 | 56 | UTF-8 57 | UTF-8 58 | 2.43.0 59 | 2.4.4 60 | 61 | 62 | 63 | 65 | 66 | doclint-java8-disable 67 | 68 | [1.8,) 69 | 70 | 71 | -Xdoclint:none 72 | 73 | 74 | 75 | release 76 | 77 | 78 | 79 | org.codehaus.mojo 80 | appassembler-maven-plugin 81 | 1.1.1 82 | 83 | flat 84 | lib 85 | 86 | 87 | net.lightbody.bmp.proxy.Main 88 | browsermob-proxy 89 | 90 | 91 | 92 | 93 | 94 | make-assembly 95 | install 96 | 97 | assemble 98 | 99 | 100 | 101 | 102 | 103 | maven-assembly-plugin 104 | 2.4 105 | 106 | 107 | src/main/assembly.xml 108 | 109 | 110 | 111 | 112 | make-assembly 113 | install 114 | 115 | single 116 | 117 | 118 | 119 | 120 | 121 | org.apache.maven.plugins 122 | maven-release-plugin 123 | 2.5.1 124 | 125 | true 126 | false 127 | release 128 | deploy 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | src/main/resources 140 | false 141 | 142 | **/** 143 | 144 | 145 | version.prop 146 | 147 | 148 | 149 | src/main/resources 150 | true 151 | 152 | version.prop 153 | 154 | 155 | 156 | install 157 | 158 | 159 | org.apache.maven.plugins 160 | maven-compiler-plugin 161 | 2.3.2 162 | 163 | 1.7 164 | 1.7 165 | 166 | 167 | 168 | org.apache.maven.plugins 169 | maven-source-plugin 170 | 2.1.2 171 | 172 | 173 | attach-sources 174 | package 175 | 176 | jar-no-fork 177 | 178 | 179 | 180 | 181 | 182 | org.apache.maven.plugins 183 | maven-javadoc-plugin 184 | 2.10.1 185 | 186 | 187 | attach-javadocs 188 | package 189 | 190 | jar 191 | 192 | 193 | ${javadoc.opts} 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | org.apache.maven.plugins 204 | maven-gpg-plugin 205 | 1.6 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | org.slf4j 214 | slf4j-api 215 | 1.7.7 216 | 217 | 218 | 219 | org.slf4j 220 | slf4j-jdk14 221 | 1.7.7 222 | 223 | 224 | 225 | com.google.sitebricks 226 | sitebricks 227 | 0.8.9 228 | 229 | 230 | 231 | com.google.inject.extensions 232 | guice-multibindings 233 | 3.0 234 | 235 | 236 | 237 | com.fasterxml.jackson.core 238 | jackson-core 239 | ${jackson.version} 240 | 241 | 242 | 243 | com.fasterxml.jackson.core 244 | jackson-databind 245 | ${jackson.version} 246 | 247 | 248 | 249 | org.apache.httpcomponents 250 | httpclient 251 | 4.3.4 252 | 253 | 254 | org.apache.httpcomponents 255 | httpmime 256 | 4.3.4 257 | 258 | 259 | 260 | net.sf.jopt-simple 261 | jopt-simple 262 | 3.2 263 | 264 | 265 | 266 | org.apache.ant 267 | ant 268 | 1.8.2 269 | 270 | 271 | 272 | org.bouncycastle 273 | bcprov-jdk15on 274 | 1.47 275 | 276 | 277 | 278 | org.eclipse.jetty 279 | jetty-server 280 | 7.3.0.v20110203 281 | 282 | 283 | org.eclipse.jetty 284 | jetty-servlet 285 | 7.3.0.v20110203 286 | 287 | 288 | 289 | com.google.inject 290 | guice 291 | 3.0 292 | 293 | 294 | 295 | com.google.inject.extensions 296 | guice-servlet 297 | 3.0 298 | 299 | 300 | 301 | net.jcip 302 | jcip-annotations 303 | 1.0 304 | 305 | 306 | 307 | org.seleniumhq.selenium 308 | selenium-api 309 | ${selenium.version} 310 | 311 | 312 | 313 | org.seleniumhq.selenium 314 | selenium-firefox-driver 315 | ${selenium.version} 316 | test 317 | 318 | 319 | 320 | com.github.detro.ghostdriver 321 | phantomjsdriver 322 | 1.0.4 323 | test 324 | 325 | 326 | 327 | 328 | 329 | junit 330 | junit 331 | 4.11 332 | test 333 | 334 | 335 | org.mockito 336 | mockito-all 337 | 1.9.5 338 | test 339 | 340 | 341 | 342 | net.sf.uadetector 343 | uadetector-resources 344 | 2014.10 345 | 346 | 347 | org.jboss.arquillian.extension 348 | arquillian-phantom-driver 349 | 1.1.1.Final 350 | 351 | 352 | commons-io 353 | commons-io 354 | 2.4 355 | 356 | 357 | 358 | 359 | 360 | 361 | org.codehaus.mojo 362 | findbugs-maven-plugin 363 | 2.3.1 364 | 365 | Max 366 | 367 | 368 | 369 | org.codehaus.mojo 370 | cobertura-maven-plugin 371 | 2.4 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | com.google.guava 380 | guava 381 | 15.0 382 | 383 | 384 | org.seleniumhq.selenium 385 | selenium-remote-driver 386 | ${selenium.version} 387 | 388 | 389 | 390 | 391 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/lib/maven-metadata-appassembler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | net.lightbody.bmp 4 | browsermob-proxy 5 | 6 | 2.0.0 7 | 8 | 1.7.7 9 | 0.8.9 10 | 1.3.1 11 | 1.1.4c 12 | 2.1.3.Final 13 | 15.0 14 | 7.0.3 15 | 1.6.3 16 | 3.2.4.Final 17 | 1.5.2 18 | 1.0.0.GA 19 | 3.0 20 | 2.4.4 21 | 2.4.0 22 | 4.3.4 23 | 4.3.2 24 | 1.1.3 25 | 1.6 26 | 3.2 27 | 1.8.2 28 | 1.47 29 | 7.3.0.v20110203 30 | 2.5 31 | 1 32 | 1.0 33 | 2.43.0 34 | 20080701 35 | 2014.10 36 | 0.9.22 37 | 1.3 38 | 2.0.3 39 | 1.1.1.Final 40 | 2.0.0 41 | 1.13.1 42 | 3.0.5 43 | 1.14 44 | 2.0.6 45 | 1.4 46 | 2.4 47 | 1.1.2 48 | 2.1 49 | 1.0-alpha-33 50 | 1.2-alpha-10 51 | 52 | 20150131065311 53 | 54 | 55 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/ssl-support/blank_crl.dec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/browsermob-proxy-2.0.0/ssl-support/blank_crl.dec -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/ssl-support/blank_crl.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBuDCCASECAQEwDQYJKoZIhvcNAQEFBQAwWTEaMBgGA1UECgwRQ3liZXJWaWxs 3 | aWFucy5jb20xLjAsBgNVBAsMJUN5YmVyVmlsbGlhbnMgQ2VydGlmaWNhdGlvbiBB 4 | dXRob3JpdHkxCzAJBgNVBAYTAlVTFw0xMjAyMDUwMzAwMTBaFw0zMTEwMjMwMzAw 5 | MTBaoIGTMIGQMIGBBgNVHSMEejB4gBQKvBeVNGu8hxtbTP31Y4UttI/1bKFdpFsw 6 | WTEaMBgGA1UECgwRQ3liZXJWaWxsaWFucy5jb20xLjAsBgNVBAsMJUN5YmVyVmls 7 | bGlhbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxCzAJBgNVBAYTAlVTggEBMAoG 8 | A1UdFAQDAgEBMA0GCSqGSIb3DQEBBQUAA4GBAEtCmbwTrP7xgkGH3uWH7QU7i52j 9 | aqraBnfheTv6jMFvXq/Nc9hvcmhkkw/+UezjZ/tK1a8a1+vJDPYXUzeAV/eWTPWf 10 | AgWAwBlUTi5ALnRY07TCyQYN2rOb52ChO8cNIUGfEvs41I5N5Vrtvg6m10Eb4XF6 11 | M2dSJ5eLlMm40kYx 12 | -----END X509 CRL----- 13 | -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/ssl-support/cybervillainsCA.cer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/browsermob-proxy-2.0.0/ssl-support/cybervillainsCA.cer -------------------------------------------------------------------------------- /browsermob-proxy-2.0.0/ssl-support/cybervillainsCA.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/browsermob-proxy-2.0.0/ssl-support/cybervillainsCA.jks -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | """Set up all the configuration variables needed for run.py""" 2 | DEFAULT_BASE_URL = "http://www.google.com" # Set this 3 | 4 | # This has been set as an array so that we can set multiple environments and run the tests below in a for loop 5 | # to repeat the tests for different operating systems. I have set a mobile capability list and 6 | # desktop capability list to allow the use of either or with a flag. More tests can easily be added. See the 7 | # browser stack website to get more options. 8 | # The below tests IE8, IE9, IE10 and IE11 on windows and then chrome, safari and Firefox on a Mac. Pay attention to 9 | # the version numbers. 10 | DESKTOP_CAP_LIST_CONFIGS = [ 11 | {'browser': 'IE', 'browser_version': '11.0', 'os': 'Windows', 'os_version': '7', 'resolution': '1024x768'}, 12 | {'browser': 'Chrome', 'browser_version': '39.0', 'os': 'OS X', 'os_version': 'Yosemite', 13 | 'resolution': '1024x768'}, 14 | {'browser': 'Firefox', 'browser_version': '35.0', 'os': 'OS X', 'os_version': 'Yosemite', 15 | 'resolution': '1024x768'}, 16 | # Safari is not well supported with selenium 17 | # {'browser': 'Safari', 'browser_version': '8.0', 'os': 'OS X', 'os_version': 'Yosemite', 18 | # 'resolution': '1024x768'}, 19 | ] 20 | 21 | # The below tests an iPhone 5 and Samsung Galaxy S5 22 | MOBILE_CAP_LIST_CONFIGS = [ 23 | # {'browserName': 'iPhone', 'platform': 'MAC', 'device': 'iPhone 5'}, 24 | # {'browserName': 'android', 'platform': 'ANDROID', 'device': 'Samsung Galaxy S5'}, 25 | ] 26 | 27 | # URLs to black list. This will speed up tests drastically if you block certain external scripts that 28 | # are taking a while to run. These are all regular expressions. Add other URLs that you want to block. 29 | # Will not work with browserstack. 30 | PROXY_BLACKLIST = [ 31 | # all images 32 | "(.)*\.jpg.*", 33 | "(.)*\.png.*", 34 | "(.)*\.jpeg.*", 35 | "(.)*\.gif.*", 36 | ] 37 | 38 | # Default PhantomJS args. These can be any PhantomJS compatible command line arguments 39 | PHANTOMJS_DEFAULTS = [ 40 | "--ignore-ssl-errors=true" 41 | ] 42 | -------------------------------------------------------------------------------- /local_testing_binaries/linux_32/BrowserStackLocal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/local_testing_binaries/linux_32/BrowserStackLocal -------------------------------------------------------------------------------- /local_testing_binaries/linux_64/BrowserStackLocal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/local_testing_binaries/linux_64/BrowserStackLocal -------------------------------------------------------------------------------- /local_testing_binaries/osx/BrowserStackLocal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/local_testing_binaries/osx/BrowserStackLocal -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | # from selenium.webdriver.common.keys import Keys 3 | # from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 4 | import os 5 | import argparse 6 | import ast 7 | import time 8 | 9 | # Set up the command line arguments 10 | parser = argparse.ArgumentParser() 11 | parser.add_argument("--desktop", help="(boolean) Make the tests only run tests on desktop computers. BROWSERSTACK.", 12 | action="store_true") 13 | parser.add_argument("--mobile", help="(boolean) Make the tests only run tests on mobile devices. BROWSERSTACK", 14 | action="store_true") 15 | parser.add_argument("--use_local", help="(boolean) Use the Local Browserstack selenium testing. BROWSERSTACK", 16 | action="store_true") 17 | parser.add_argument("--base_url", help="A way to override the DEFAULT_BASE_URL for your tests.",) 18 | parser.add_argument("--test", help="Only run one test as specified", default="all") 19 | desired_cap_config_notes = "The available values you can pass to this command will depend on the values you have\ 20 | set in your config.py file. BROWSERSTACK." 21 | parser.add_argument("--browser", help="Desktop: Only run tests on the given browser." + 22 | desired_cap_config_notes, default=False) 23 | parser.add_argument("--browser_version", help="Desktop: Only run tests on the given browser version." + 24 | desired_cap_config_notes, default=False) 25 | parser.add_argument("--os", help="Desktop: Only run tests on the given operating system." + 26 | desired_cap_config_notes, default=False) 27 | parser.add_argument("--os_version", help="Desktop: Only run tests on the given operating system version." + 28 | desired_cap_config_notes, default=False) 29 | parser.add_argument("--resolution", help="Desktop: Only run tests on the given screen resolution." + 30 | desired_cap_config_notes, default=False) 31 | parser.add_argument("--browserName", help="Mobile: Only run tests on the given the mobile browser name." + 32 | desired_cap_config_notes, default=False) 33 | parser.add_argument("--platform", help="Mobile: Only run tests on the given the mobile platform." + 34 | desired_cap_config_notes, default=False) 35 | parser.add_argument("--device", help="Mobile: Only run tests on the given the mobile device." + 36 | desired_cap_config_notes, default=False) 37 | parser.add_argument("--capabilities", help="Example: \"{'browser': 'IE', 'browser_version': '8.0', 'os': 'Windows', " + 38 | "'os_version': '7', 'resolution': '1024x768'}\" Can be used as an alternative to adding many of" + 39 | "the arguments above such as mobile, desktop, browser, browser_version, etc.") 40 | parser.add_argument("--browserstack", help="This will make tests execute on the browserstack platform instead" + 41 | "the above flags will be ignored", action="store_true") 42 | parser.add_argument("--proxy", help="This will make the tests attempt to run through your browsermob proxy" + 43 | ". Wil not work with Browserstack.", action="store_true") 44 | parser.add_argument("--phantom", help="Will run tests via Phantom JS which will be a little faster. Will not work" + 45 | " with Browserstack.", action="store_true") 46 | args = parser.parse_args() 47 | 48 | # IMPORT ALL VARIABLES 49 | from config import * 50 | 51 | if(args.base_url is not None): 52 | # Override the DEFAULT_BASE_URL value if passed in via the command line arg 53 | BASE_URL = args.base_url # Don't touch this 54 | else: 55 | BASE_URL = DEFAULT_BASE_URL 56 | 57 | # Set up phantom js default arguments if it's being used 58 | if(args.phantom): 59 | phantomjs_args = PHANTOMJS_DEFAULTS 60 | 61 | # Set up the browsermob proxy if the argument is passed 62 | if(args.proxy): 63 | from browsermobproxy import Server 64 | path_to_bmp = os.path.abspath(os.path.join(os.path.dirname(__file__), "browsermob-proxy-2.0.0/bin")) 65 | server = Server("%s/browsermob-proxy" % path_to_bmp) 66 | server.start() 67 | proxy = server.create_proxy() 68 | proxy_url = proxy.webdriver_proxy().http_proxy 69 | 70 | # Set up the blacklist 71 | for regex in PROXY_BLACKLIST: 72 | proxy.blacklist(regex, 200) 73 | 74 | # Set up the needed arguments for either firefox or phantomjs 75 | if (args.phantom): 76 | phantomjs_args.append("--proxy=%s" % (proxy_url)) 77 | else: 78 | profile = webdriver.FirefoxProfile() 79 | profile.set_proxy(proxy.selenium_proxy()) 80 | 81 | 82 | if(args.browserstack): 83 | # Grab the authentication variables from the environment 84 | try: 85 | selenium_username = os.environ['SELENIUM_AUTOMATE_USERNAME'] 86 | selenium_value = os.environ['SELENIUM_AUTOMATE_VALUE'] 87 | except KeyError: 88 | print "You need to set the environment variables for your username and value. See the README.md for details" 89 | exit() 90 | 91 | 92 | if(BASE_URL == ""): 93 | print "You need to set your DEFAULT_BASE_URL in config.py." 94 | exit() 95 | 96 | # If browserstack is set to be used then do set up the desired capabilities array, etc. 97 | if(args.browserstack): 98 | if(args.capabilities is not None): 99 | # If the user passed --capabilities "{...}" then that will be the only platform tested on 100 | 101 | # convert the string into a python dictionary so it can be used in as a desired capability 102 | desired_cap_list = [ast.literal_eval(args.capabilities)] 103 | else: 104 | # If the user did not pass the --capabilities argumen then run all the tests listed below 105 | 106 | # This has been set as an array so that we can set multiple environments and run the tests below in a for loop 107 | # to repeat the tests for different operating systems. I have set a mobile capability list and 108 | # desktop capability list to allow the use of either or with a flag. More tests can easily be added. See the 109 | # browser stack website to get more options. 110 | # The below tests IE8, IE9, IE10 and IE11 on windows and then chrome, safari and Firefox on a Mac. 111 | # Pay attention to the version numbers. 112 | desktop_cap_list = DESKTOP_CAP_LIST_CONFIGS 113 | 114 | # The below tests an iPhone 5 and Samsung Galaxy S5. If your Selenium Automate plan doesn't include Mobile, 115 | # you will Want to change the following to mobile_cap_list = [] 116 | mobile_cap_list = MOBILE_CAP_LIST_CONFIGS 117 | 118 | # Conditionally create the desired_cap_list list. Didn't use elseif statements to allow for more user error 119 | desired_cap_list = [] 120 | if(args.desktop): 121 | # If the desktop argument has been passed, then only run the desktop tests 122 | desired_cap_list = desired_cap_list + desktop_cap_list 123 | if(args.mobile): 124 | # If the mobile argument has been passed, then only run the mobile tests 125 | desired_cap_list = desired_cap_list + mobile_cap_list 126 | if(desired_cap_list == []): 127 | # If no desktop or mobile argument has been passed, then run both the desktop and mobile tests 128 | desired_cap_list = desktop_cap_list + mobile_cap_list 129 | 130 | # If a specific filter arg was set via the command line, remove anything from the desired_capabilites list that 131 | # does not meet the requirement 132 | desired_cap_list_filters = [ 133 | "os", 134 | "browser", 135 | "browser_version", 136 | "resolution", 137 | "os_version", 138 | "browserName", 139 | "platform", 140 | "device", 141 | ] 142 | # Loops through the abev list to do this to prevent duplicate code. You will notice that append was used below, 143 | # instead of remove. Remove was not acting as hoped so to cut corners, I just did this with addition instead of 144 | # subtraction 145 | # to the same affect. 146 | for the_filter in desired_cap_list_filters: 147 | if(getattr(args, the_filter) is not False): 148 | temp_list = desired_cap_list 149 | desired_cap_list = [] 150 | for desired_cap in temp_list: 151 | if(the_filter in desired_cap and desired_cap[the_filter] == getattr(args, the_filter)): 152 | desired_cap_list.append(desired_cap) 153 | # If the --browserstack argument was not passed, just set one element in the desired_cap_list for the sake 154 | # of using the same logic below. When looping through the elements of the desired_cap_list 155 | else: 156 | if(args.phantom): 157 | desired_cap_list = [{"browser": "PhantomJs"}] 158 | else: 159 | desired_cap_list = [{"browser": "Firefox"}] 160 | 161 | # This will run the the same test code in multiple environments 162 | for desired_cap in desired_cap_list: 163 | 164 | # If the browserstack argument was passed, then use the folowing to output the test headers 165 | if(args.browserstack): 166 | # Use browser stack local testing if the argument was passed 167 | if(args.use_local): desired_cap['browserstack.local'] = True 168 | 169 | # Output a line to show what enivornment is now being tested 170 | if("browser" in desired_cap): 171 | # For desktop on browserstack 172 | print "\nStarting Tests on %s %s on %s %s with a screen resolution of %s " % (desired_cap["browser"], 173 | desired_cap["browser_version"], desired_cap["os"], desired_cap["os_version"], desired_cap["resolution"]) 174 | else: 175 | # For mobile on browserstack 176 | print "\nStarting Tests on a %s" % (desired_cap["device"]) 177 | # Otherwise, just simply output this message 178 | else: 179 | # For desktop on localmachine using firefox 180 | print "\nStarting Tests on %s on your local machine" % (desired_cap["browser"]) 181 | 182 | print "--------------------------------------------------\n" 183 | 184 | # If the browserstack argument was passed, then dynamically set up the remote driver. 185 | if(args.browserstack): 186 | driver = webdriver.Remote( 187 | command_executor="http://%s:%s@hub.browserstack.com:80/wd/hub" % (selenium_username, selenium_value), 188 | desired_capabilities=desired_cap) 189 | # Otherwise, just run firefox locally. 190 | else: 191 | if(args.proxy and args.phantom): 192 | driver = webdriver.PhantomJS(service_args=phantomjs_args) 193 | elif(args.proxy): 194 | # Defaults to firefox 195 | driver = webdriver.Firefox(firefox_profile=profile) 196 | elif(args.phantom): 197 | driver = webdriver.PhantomJS(service_args=phantomjs_args) 198 | # With Phantom js, we need te set a specific window size to prevent certain tests from failing 199 | driver.set_window_size(1124, 850) 200 | else: 201 | driver = webdriver.Firefox() 202 | 203 | tests_to_run = [] 204 | if(args.test == "all"): 205 | # if args.test == "all" then dynamically set up that list from all the file names in the test folder 206 | for file in [doc for doc in os.listdir("tests") if doc.endswith(".py") and doc != "__init__.py" 207 | and doc != "base_test.py"]: 208 | tests_to_run.append("tests." + file.split(".")[0]) # remove the .py from the test name 209 | else: 210 | # Otherwise, just add the one tests specified when passing the command 211 | tests_to_run = ["tests." + args.test.split(".")[0]] # remove the .py from the test name 212 | 213 | # The time when the test started 214 | all_tests_start_time = time.time() 215 | 216 | # Loop through all the tests_to_run and runs them 217 | for test_to_run in tests_to_run: 218 | # The time when the test started 219 | this_test_start_time = time.time() 220 | # This dynamically imports all modules in the tests_to_run list. This allows me to import a module using 221 | # a variable. This is fairly advanced and hard to follow for the beginner. 222 | current_test = getattr(__import__(test_to_run, fromlist=["Test"]), "Test") 223 | test = current_test(driver, BASE_URL, test_to_run) 224 | test.run() 225 | 226 | # If it makes it this far, this means the test passed 227 | test.passed() 228 | 229 | # Output the amount of time it took this test to run on the current platform 230 | this_test_seconds_taken = time.time() - this_test_start_time 231 | if(this_test_seconds_taken > 60): 232 | print "Time taken: " + str(this_test_seconds_taken / 60) + " minutes" 233 | else: 234 | print "Time taken: " + str(this_test_seconds_taken) + " seconds" 235 | 236 | # Output the amount of time it took all tests to run on the current platform 237 | print "--------------------------------------------------\n" 238 | all_tests_seconds_taken = (time.time() - all_tests_start_time) 239 | if(all_tests_seconds_taken > 60): 240 | print "Time taken for all tests: " + str(all_tests_seconds_taken / 60) + " minutes" 241 | else: 242 | print "Time taken for all tests: " + str(all_tests_seconds_taken) + " seconds" 243 | 244 | # Clean Up 245 | driver.quit() 246 | 247 | if(args.proxy): server.stop() 248 | -------------------------------------------------------------------------------- /screenshots/README.md: -------------------------------------------------------------------------------- 1 | - Screenshots will automatically be added to this directory when taken by your tests. 2 | - You will probably want to delete the generated screenshots periodically. 3 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chiedo/python-selenium-starter/670e3ed8cda73d7d5162c53c01ef13753235c000/tests/__init__.py -------------------------------------------------------------------------------- /tests/base_test.py: -------------------------------------------------------------------------------- 1 | """A test class for others to inherit to prevent duplicate code.""" 2 | 3 | from selenium.webdriver.support.ui import WebDriverWait 4 | from selenium.webdriver.common.by import By 5 | import time 6 | 7 | 8 | class BaseTest(object): 9 | def __init__(self, driver, base_url, module): 10 | """Init 11 | 12 | Parameters 13 | ---------- 14 | driver : object 15 | The selenium web driver 16 | base_url : string 17 | The base url of the web page we will be visiting. 18 | module 19 | The module currently being executed 20 | """ 21 | self.driver = driver 22 | self.base_url = base_url 23 | self.module = module 24 | self.wait = WebDriverWait(driver, 10) 25 | 26 | def confirm_in_url(self, path): 27 | """Confirm the path appears in the URL 28 | 29 | Parameters 30 | ------------- 31 | path: string 32 | The string that should appear in the URL 33 | """ 34 | if(self.keep_trying(lambda: (True if path not in self.driver.current_url else False), 35 | fallback=True, unsatisfactory=True)): 36 | self.failed("We did not end up on the " + path + " page when we should have") 37 | 38 | def confirm_in_page(self, text, page_class): 39 | """Confirm the path appears in the URL 40 | 41 | Parameters 42 | ------------- 43 | text: string 44 | The string that should appear in the page 45 | page_class: string 46 | The page class used for identifying the search area 47 | """ 48 | page_wrap = self.keep_trying(lambda: self.driver.find_element(By.CLASS_NAME, page_class)) 49 | 50 | if(self.keep_trying(lambda: (True if text not in page_wrap.text else False), 51 | fallback=True, unsatisfactory=True)): 52 | self.failed(text + " did not appear in the page") 53 | 54 | def failed(self, error_message): 55 | """Print a generic message when a test has failed, take a screenshot and end the test. 56 | Parameters 57 | ---------- 58 | error_message : string 59 | A message describing what went wrong with the test. 60 | """ 61 | print("Failed: " + self.module + ": " + error_message) 62 | self.take_screenshot() 63 | self.driver.quit() 64 | exit() 65 | 66 | def keep_trying(self, function, attempts=60, fallback=None, unsatisfactory=None): 67 | """Continues to try the function without errors for a number of attempts before continuing. This solves 68 | The problem of Selenium being inconsistent and erroring out because a browser is slow. 69 | 70 | Parameters 71 | ---------- 72 | assertion : lambda 73 | A lambda function that should at some point execute successfully. 74 | attempts : Integer 75 | The number of attempts to keep trying before letting the test continue 76 | unsatisfactory : Any 77 | Value that is unsatisfactory as a return value 78 | fallback : Any 79 | The fallback return value if the function did return a satisfactory value within the given 80 | number of attempts. 81 | 82 | Returns the return value of the function we are trying. 83 | """ 84 | for i in xrange(attempts): 85 | try: 86 | result = function() 87 | # It will only return if the assertion does not throw an error 88 | if(result is not unsatisfactory): return result 89 | except: 90 | pass 91 | time.sleep(1) # This makes the function wait a second between attempts 92 | 93 | return fallback 94 | 95 | def passed(self): 96 | """Print a generic message when a test has passed 97 | """ 98 | print("Passed: " + self.module) 99 | 100 | def take_screenshot(self): 101 | """Take a screenshot with a defined name based on the time and the browser""" 102 | millis = int(round(time.time() * 1000)) 103 | if(self.driver.name): 104 | driver_name = self.driver.name 105 | else: 106 | driver_name = "" 107 | self.driver.save_screenshot("screenshots/" + driver_name + "-" + str(millis) + "-screenshots.png") 108 | -------------------------------------------------------------------------------- /tests/example_a.py: -------------------------------------------------------------------------------- 1 | """An example test to show how to structure your tests""" 2 | 3 | from tests.base_test import BaseTest 4 | 5 | 6 | class Test(BaseTest): 7 | def __init__(self, driver, base_url, module): 8 | super(Test, self).__init__(driver, base_url, module) 9 | 10 | def run(self): 11 | """ 12 | Runs the tests. this is what will be getting called by run.py 13 | """ 14 | self.driver.get(self.base_url) 15 | if "Google" not in self.driver.title: 16 | raise Exception("Unable to load google page!") 17 | # Wrapping any find element call with keep_trying, will make sure selenium keeps making a number of attempts 18 | # to locate the element before giving up. 19 | elem = self.keep_trying(lambda: self.driver.find_element_by_name("q")) 20 | elem.send_keys("BrowerStack") 21 | elem.submit() 22 | print self.driver.title 23 | # No need to quit driver at the end of the test. The run.py file will 24 | # handle that 25 | -------------------------------------------------------------------------------- /tests/example_b.py: -------------------------------------------------------------------------------- 1 | """An example test to show how to structure your tests""" 2 | 3 | from tests.base_test import BaseTest 4 | 5 | 6 | class Test(BaseTest): 7 | def __init__(self, driver, base_url, module): 8 | super(Test, self).__init__(driver, base_url, module) 9 | 10 | def run(self): 11 | """ 12 | Runs the tests. this is what will be getting called by run.py 13 | """ 14 | # No need to quit driver at the end of the test. The run.py file will 15 | # handle that 16 | --------------------------------------------------------------------------------