Search code examples
seleniumselenium-webdriverfirefoxgeckodriverselenium-firefoxdriver

Selenium: JavaScript warning: unreachable code after return statement


I could use some advice on a specific Selenium script error. Some context: I'm trying to verify an former colleague's old script is still workable. Unfortunately that colleague is no longer around to ask, so I'm having to do a lot of guess work.

His script is a simple demo script (in Java) that's supposed to go WhatsApp web and verify that the scan code displayed there is regularly refreshing. Here's the script:

import java.util.function.Supplier;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class WhatsAppTest {
    @Test
    public void testScanCodeRenew() throws InterruptedException {
        
        System.setProperty("webdriver.gecko.driver","D:\\WebDriver\\geckodriver.exe");
        
        WebDriver driver=new FirefoxDriver();
        
        driver.get("https://web.whatsapp.com/");
        
        By xpath = By.xpath("//div[contains(@class,'_2EZ_m')]");
        WebElement scanCode = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.presenceOfElementLocated(xpath));
        
        Supplier<String> getScanCode = 
               () -> scanCode.getAttribute("data-ref");
        
        String oldCode = getScanCode.get();
                
        for(int index = 0 ; index < 3 ; index++) {
            
            Thread.sleep(30_000);
            
            String newCode = getScanCode.get();
            
            Assert.assertNotEquals(oldCode, newCode);
            
            oldCode = newCode;
        }
    }

}

When I attempt to run this script in Eclipse, it fails with this error:

JavaScript warning: https://web.whatsapp.com/app.91e887b2b63434f2a2e6.js, line 1: unreachable code after return statement

My layman's guess (I'm by no means a scripting expert) is the error is being thrown by the site's own JavaScript, not the Selenium script, but I may be completely misinterpreting that. Here's the full error output seen in the Eclipse console:

1605553468566   geckodriver INFO    Listening on 127.0.0.1:15630
1605553469174   mozrunner::runner   INFO    Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "--marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\doubl\\AppData\\Local\\Temp\\rust_mozprofilehOBapN"
Can't find symbol 'eglSwapBuffersWithDamageEXT'.
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.warn: SearchSettings: "get: No settings file exists, new profile?" (new Error("", "(unknown module)"))
1605553472011   Marionette  INFO    Listening on port 51076
1605553472311   Marionette  WARN    TLS certificate errors will be ignored for this session
Nov 16, 2020 1:04:32 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
JavaScript warning: https://web.whatsapp.com/app.91e887b2b63434f2a2e6.js, line 1: unreachable code after return statement
JavaScript warning: https://web.whatsapp.com/app.91e887b2b63434f2a2e6.js, line 1: unreachable code after return statement

Might this script be salvagable, or have I hit a brick wall?


Solution

  • To start with Idon't see any tangible error within the logs. The concerned logs:

    JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
    console.warn: SearchSettings: "get: No settings file exists, new profile?" (new Error("", "(unknown module)"))
    .
    JavaScript warning: https://web.whatsapp.com/app.91e887b2b63434f2a2e6.js, line 1: unreachable code after return statement
    JavaScript warning: https://web.whatsapp.com/app.91e887b2b63434f2a2e6.js, line 1: unreachable code after return statement
    

    ...implies that there was a JavaScript error while GeckoDriver initiated/spawned a new Browsing Context i.e. Firefox browsing session.


    When Selenium driven GeckoDriver initiates a Browsing Context there can be a couple of JavaScript related WARNINGS and ERRORS within the TRACE level logs during initialization. You can safely ignore those initialization errors till GeckoDriver is successfully able to initiate a Firefox Browsing session.


    Conclusion

    When the createSession is successful and W3C dialect is detected you can safely ignore the errors.