javaleanft

Trying to suppress errors while attaching browser


EDIT: I'm using the LeanFT Java SDK 14.50 EDIT2: for text clarification

I'm writing test scripts for a web application that sometimes opens popup browsers for specific actions. So natually when that happens, I will attach the new browser using BrowserFactory.attach(...). The problem is that leanFT does not seem to have a way to validate that the browser exists before attaching it, and if I try to attach it too early, it will fail. And I don't like to use an arbitrairy wait/sleep time as I can never really know how much time it's going to take for the browser to get be ready. So my solution to this is below

private Browser attachPopUpBrowser(BrowserType bt, RegExpProperty url){
    Browser browser = null;
    int iteration = 0;

    //TimeoutLimit.SHORT = 15000
    while (browser == null && iteration < TimeoutLimit.SHORT.getLimit()) {
        try {
            Reporter.setReportLevel(ReportLevel.Off);
            browser = BrowserFactory.attach(
                    new BrowserDescription.Builder()
                            .type(bt)
                            .url(url)
                            .build()
            );
            Reporter.setReportLevel(ReportLevel.All);
        } catch (GeneralLeanFtException e) {
            try {
                Thread.sleep(1000);
                iteration += 1000;
            } catch (InterruptedException e1) {
            }
        }
    }
    return browser;
}

Now, this works wonderfully with one exception. It generates errors in the leanft test result. Errors that I want to ignore because I know that it will fail a few times before it will succeed. As you can see, I've tried changing the ReportLevel while doing this in order to suppress the error logging, but it doesn't work. I've tried using

Browser[] browsers = BrowserFactory.getallOpenBrowsers(BrowserDescription);

thinking that it will return an empty Array if it finds nothing, but I still get errors while the browser is not ready. Does anyone have suggestions as to how I could work around this?

TL;DR

I'm looking for a way to either suppress the errors generated within my While..Loop or to validate that the browser is ready before attaching it. All of that, so that I can have a nice and clean Run Result at the end of my test (because these errors will present false negatives in all nearly all of my tests)

Addendum

Also, when the attach fails for the first time, I get a an exception

com.hp.lft.sdk.ReplayObjectNotFoundException: attachApplication

as expected, but all subsequent failures are throwing

com.hp.lft.sdk.GeneralLeanFtException: Cannot read property 'match' of null

I've compared both stack traces and they are identical except for the last 2 lines which happen within the ReplayExceptionFactory.CreateDefault() so I think that there is something that gets corrupted during the exception generation, but that is within the leanft.sdk.internal package so there might not be a lot we can do about it right now.I'm guessing that if I did not get that second "cannot read property" exception, I would correctly get the ReplayObjectNotFoundException until the browser is correctly attached.


Solution

  • I'd rather not force an attach endlessly until it works. Even if we'd solve the false negatives, we'd still have a not so good approach to the problem.

    The cleanest solution would be to see if there is anything to attach to in the first place.

    And you can do just that by getting all the browser instances that meets your description.

    Browser[] browsers = BrowserFactory.getAllOpenBrowsers(new BrowserDescription.Builder().build());
    

    Any element in this collection is an already "attached" browser - you can start using it.

    If the list doesn't contain your browser instance, rerun the query.