What is the correct equivalent of Java of this JS line of code?

I am trying to lessen the line of codes using the Lambda expression in Java after seeing the same code written in 1 line in JS somewhere. But i am getting this below error:

Incompatible types. Required: org.openqa.selenium.logging.LogEntries Found: void

This is what i have now.

List e =driver.manage().logs().get(LogType.BROWSER).getAll();
String s="",t ="";
if (!e.isEmpty()){
                for (Object l : e)
                {
                    t=l.toString();
                    if(t.contains("SyntaxError")){
                        s = s + t+"<BR/>";
                    }
                    else if(t.contains("EvalError")){
                        s = s + t+"<BR/>";
                    }
                    else if(t.contains("ReferenceError")){
                        s = s + t+"<BR/>";
                    }
                    else if(t.contains("RangeError")){
                        s = s + t+"<BR/>";
                    }
                    else if(t.contains("TypeError")){
                        s = s + t+"<BR/>";
                    }
                    else if(t.contains("URIError")){
                        s = s + t+"<BR/>";
                    }
                }

And this is what i am trying to get to:

List<String > errorStrings = new ArrayList<>();
            errorStrings.add("SyntaxError");
            errorStrings.add("EvalError");
            errorStrings.add("ReferenceError");
            errorStrings.add("RangeError");
            errorStrings.add("TypeError");
            errorStrings.add("URIError");
            LogEntries l = driver.manage().logs().get(LogType.BROWSER).forEach(le -> errorStrings.stream().anyMatch(er -> le.getMessage().contains(er)));

By following this line of code written is JS:

var jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser).Where(x => errorStrings.Any(e => x.Message.Contains(e)));

Please help me to understand what am i doing wrong.

-TIA

1 answer

  • answered 2019-05-15 04:05 sprinter

    Your code is

    LogEntries l = driver.manage().logs().get(LogType.BROWSER)
        .forEach(le -> errorStrings.stream()
            .anyMatch(er -> le.getMessage().contains(er)));
    

    And your error message is

    Incompatible types. Required: org.openqa.selenium.logging.LogEntries Found: void
    

    The error message is telling you that your assignment statement is expecting you to return a LogEntries but it is getting a void. It is getting a void because that is the return type of the Stream.forEach method. The forEach method is used to consume all elements of the stream and return nothing.

    I think what you are trying to do is filter a set of log entries and turn them into a LogEntries object right? If that's the case I suspect you want filter rather than forEach.

    I'm not a selenium expert so I can't advise you on how to turn a Stream<LogEntry> into aLogEntries`. I'm assuming you can use a reduce or collect operation but might be best to pose that as a separate question.