praveenLT / java-testng-js-exception-log-se4

A sample repo to help you capture JavaScript exception for automation test in Java-TestNG on LambdaTest. Run Selenium tests with TestNG on LambdaTest platform.

Home Page:https://www.lambdatest.com/java-automation-testing?utm_source=github&utm_medium=about&utm_campaign=java_testng_js_exception_log_se4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to capture JavaScript exception for automation test in Java-TestNG on LambdaTest

Environment Setup

  1. Global Dependencies
    $ install maven
    
  2. Project Dependencies
    • checkout the repository
    • Check that packages are available
    $ cd Java-TestNG-Selenium
    
    • You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
    $ mvn versions:display-dependency-updates
    

LambdaTest Authentication Credentials: Make sure you have your LambdaTest credentials with you to run test automation scripts with Jest on LambdaTest Selenium Grid. You can obtain these credentials from the LambdaTest Automation Dashboard or through LambdaTest Profile.

Set LambdaTest Username and Access Key in environment variables.

  • For Linux/macOS:

    $ export LT_USERNAME="YOUR_USERNAME"
    $ export LT_ACCESS_KEY="YOUR ACCESS KEY"
    
  • For Windows:

    $ set LT_USERNAME="YOUR_USERNAME"
    $ set LT_ACCESS_KEY="YOUR ACCESS KEY"
    

Capturing and printing JS exceptions:

        DevTools devTools = ((HasDevTools) driver).getDevTools();
        devTools.createSession();

        List<JavascriptException> jsExceptionsList = new ArrayList<>();
        Consumer<JavascriptException> addEntry = jsExceptionsList::add;
        devTools.getDomains().events().addJavascriptExceptionListener(addEntry);

        //print
        for (JavascriptException jsException : jsExceptionsList) {
            System.out.println("My JS exception message: " + jsException.getMessage());
            System.out.println("My JS exception system: " + jsException.getSystemInformation());
            jsException.printStackTrace();
            success = true;
        }

Running Tests

To run single test
    $ mvn test -D suite=single.xml -Dtestng.dtd.http=true

About LambdaTest

LambdaTest is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your selenium automation testing to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.