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

On Mon, Jun 5, 2017 at 8:57 AM, <[email protected]> wrote:

> 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.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/geb-user/284c4f39-9cf6-4151-b437-b7db14e10e24%40googlegroups.com
> <https://groups.google.com/d/msgid/geb-user/284c4f39-9cf6-4151-b437-b7db14e10e24%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/geb-user/CA%2B52dQSniPJvN3dD0_f_dHx2bO7Y_kPMRkch_NfhwO3XuX%2B0nQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to