BrowserMob Proxy Server and GebConfig.groovy objects access

346 views
Skip to first unread message

michal.k...@airhelp.com

unread,
Jun 5, 2017, 3:57:35 AM6/5/17
to Geb User Mailing List
Hello,

I would like to add BrowserMob Proxy Server to our project, that I can sniff network, take some HAR files etc. And got a basic problem with accessing to the Proxy object or even Har object created in GebConfig.groovy in just some external listener class.

So my approach was based on getters for rawConfig way like here: https://stackoverflow.com/questions/15395379/geb-configuration. So I tried do it this way:
GebConfig.groovy
//imports + basic configuration etc
//...

//Decleration of our Har and BrowserMobProxy objects
Har harFile = new Har();
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer()

//Setup proxy server
DesiredCapabilities browserMobProxyCapabilities(DesiredCapabilities capabilities) {
    browserMobProxy
.start(0)
   
String port = browserMobProxy.getPort()
    println
("Proxy started at: " + port)

   
String PROXY = "localhost:" + port;

    org
.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy
.setHttpProxy(PROXY)
           
.setFtpProxy(PROXY)
           
.setSslProxy(PROXY);

    harFile
= browserMobProxy.newHar("SomeData")

    capabilities
.setCapability(CapabilityType.PROXY, proxy);
    capabilities
}

//Customize chrome profile
DesiredCapabilities chromeCapabilities(){
   
ChromeOptions opt = createChromeOptions()
   
DesiredCapabilities capabilities = DesiredCapabilities.chrome()
    capabilities
.setCapability(ChromeOptions.CAPABILITY, opt)
    browserMobProxyCapabilities
(capabilities)
    capabilities
}

//Finally chrome env
environments {

   
// See: http://code.google.com/p/selenium/wiki/ChromeDriver
    chrome {
        driver
= {
           
new ChromeDriver(chromeCapabilities (new DesiredCapabilities()))
       
}
   
}
//...

Now in project testNGlistener want to access to shutdown the proxy and attach the har file on to report on failure:
@Slf4j
class GebTestNGListener implements ITestListener, IConfigurationListener {
@Override
void onTestFailure(ITestResult result) {
   
if (result != null) {
       
log.error("FAILURE: " + getTestDescription(result));
        grabScreenshots
(result)

       
//quit proxy etc.
        browser
.config.rawConfig.get("browserMobProxy")
       
       
//attach harfile etc.
        browser
.config.rawConfig.get("harFile")
   
}
}

Basically, the question is:  Is it a right approach? Currently I see that  browser.config.rawConfig.get is working, but browserMobProxy object in GebConfig.groovy is not working as it should. It's not found inside browserMobProxyCapabilities method.

Marcin Erdmann

unread,
Jun 5, 2017, 4:28:55 PM6/5/17
to Geb User Mailing List
Hi Michał,

There is a problem with your approach in that the lifecycle of GebConfig and the driver are different if driver caching is enabled (which is the default). GebConfig.groovy script is evaluated once per test class while there is only a single driver instance created if driver caching is enabled.

My approach would be:
- create the proxy as a static field on a class that is loaded before GebConfig is executed (an example would be a base test class) and start it on a fixed port by passing a non-zero value to BrowserMobProxy.start() (e.g. in a static class initialiser) 
- in GebConfig.groovy setup proxy capabilities of the browser by using the same fixed port as when starting BrowserMobProxy in your base test class
- in your listener access the proxy via the static field of your base class

Hope this helps,
Marcin

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+unsubscribe@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/284c4f39-9cf6-4151-b437-b7db14e10e24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages