{"id":2114,"date":"2015-09-17T12:36:44","date_gmt":"2015-09-17T10:36:44","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2114"},"modified":"2015-09-17T12:36:44","modified_gmt":"2015-09-17T10:36:44","slug":"testing-xpages-2-browsermob-proxy","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2114","title":{"rendered":"Testing XPages (2): BrowserMob Proxy"},"content":{"rendered":"<p>When testing XPages or other web applications, you may want to have more control about the requests and responses during the JUnit testing. For example, if you want to test if a specific HTTP header exists in the response, or if it is required to add some HTTP headers to the request. But you cannot doe this out of the box with\u00a0<a href=\"http:\/\/www.seleniumhq.org\/\" target=\"_blank\">Selenium<\/a>. Instead, you have to\u00a0add a proxy to the Firefox controller, which then gives you a handle to the HTTP data\u00a0and allows you to\u00a0control the flow.<\/p>\n<p>An good solution\u00a0for this is <a href=\"http:\/\/bmp.lightbody.net\/\" target=\"_blank\">BrowserMob Proxy<\/a>, which can be used by\u00a0adding the required dependency to your Maven <em>pom.xml:<\/em><\/p>\n<pre><code>&lt;dependency&gt;\r\n    &lt;groupId&gt;net.lightbody.bmp&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;browsermob-proxy&lt;\/artifactId&gt;\r\n    &lt;version&gt;2.0.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p><em>[This is version 2.0.0, the latest stable version]<\/em><\/p>\n<p>The proxy runs locally on the\u00a0specified\u00a0port as soon the JUnit test starts and ends automatically after finishing the tests. \u00a0In order to accomplish this, the <em>setUp<\/em> method has to be modified:<\/p>\n<pre><code>\/\/ start the proxy (listens on port 4444)\r\nserver = new ProxyServer(4444);\r\nserver.start();\r\n\r\n\/\/ get the Selenium proxy object\r\nProxy proxy = server.seleniumProxy();\r\n\r\n\/\/ configure it as a desired capability\r\nDesiredCapabilities capabilities = new DesiredCapabilities();\r\ncapabilities.setCapability(CapabilityType.PROXY, proxy);<\/code><\/pre>\n<p>Now, the proxy setting must be added to the Firefox driver:<\/p>\n<pre><code>driver = new FirefoxDriver(capabilities);<\/code><\/pre>\n<p>In the test class, three global variables must\u00a0be defined; these are giving you\u00a0access to the proxy server and latest the request and response<em>\u00a0<\/em>during testing:<\/p>\n<pre><code>private ProxyServer server;\r\nprivate BrowserMobHttpResponse httpResponse;\r\n<\/code><code>private BrowserMobHttpRequest httpRequest;<\/code><\/pre>\n<p>With the help of a <em>ResponseInterceptor,<\/em> the <em>httpResponse<\/em> property is always filled with the latest response. To initialize it, an anonymous class in the <em>setUp<\/em> method has to be created:<\/p>\n<pre><code>\/\/ add a response interceptor\r\nResponseInterceptor interceptor = new ResponseInterceptor() {\r\n    public void process(BrowserMobHttpResponse response, Har har) {\r\n          httpResponse = response;\r\n    }\r\n};\r\n\r\n\/\/ add the interceptor to the server\r\nserver.addResponseInterceptor(interceptor);<\/code><\/pre>\n<p>For the <em>RequestInterceptor<\/em> it is the same procedure:<\/p>\n<pre><code>\/\/ add a request interceptor\r\nRequestInterceptor requestInterceptor = new RequestInterceptor() {\r\n    public void process(BrowserMobHttpRequest request, Har har) {\r\n       httpRequest = request;\r\n    }\r\n};\r\n\r\nserver.addRequestInterceptor(requestInterceptor);<\/code><\/pre>\n<p>Now, it is possible to use it in a JUnit test:<\/p>\n<pre><code>@Test\r\npublic void testDemo() throws Exception {\r\n \r\n    \/\/ add a request header\r\n    server.addHeader(\"X-FOO\", \"BAR\");\r\n \r\n    \/\/ load the page\r\n    reloadPage();\r\n\r\n    \/\/ TEST RESPONSE STATUS\r\n    HttpResponse httpRawResponse = httpResponse.getRawResponse();\r\n    assertTrue(\"HTTP\/1.1 200 OK\".equals(httpRawResponse.getStatusLine()\r\n       .toString()));\r\n \r\n    \/\/ TEST SERVER HEADER\r\n    assertTrue( \"Lotus-Domino\".equals( httpResponse.getHeader(\"Server\")) );\r\n \r\n}<\/code><\/pre>\n<p>The <a href=\"http:\/\/bmp.lightbody.net\/\" target=\"_blank\">Browsermob-proxy<\/a> has a lot of additional features:\u00a0You can modify the allowed speed for up- and downstreams, use basic authentication, upload files, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When testing XPages or other web applications, you may want to have more control about the requests and responses during the JUnit testing. For example, if you want to test if a specific HTTP header exists in the response, or &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2114\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,81,74],"tags":[109,7,31,106,107,108,12,3],"class_list":["post-2114","post","type-post","status-publish","format-standard","hentry","category-java","category-web","category-xpages","tag-browsermob-proxy","tag-domino","tag-java","tag-junit","tag-selenium","tag-testing","tag-web","tag-xpages"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2114"}],"version-history":[{"count":13,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2114\/revisions"}],"predecessor-version":[{"id":2149,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2114\/revisions\/2149"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}