I seem to be facing some issues with SkipException in testNG. Can anyone provide guidance on what might be

As a newcomer to selenium UI automation, I am currently experimenting with a simple application using Java and TestNG. My goal is to integrate this test with Continuous Integration (CI), but the test environment URL will vary with each deployment. The key difference between the testing and production environments is that the testing environment does not have a login screen, so authentication is not required. However, my test aims to verify the login process and a login method. I need a way to skip this test based on the provided URL parameter. The issue I am facing is that TestNG always indicates that the test was skipped, yet I can see it executing the login method. Can someone please assist me in correcting or understanding the mistake I may be making?

public class Login {

  WebDriver driver;

  //Parameter - test environment URL
  String url = System.getProperty("environment");


  @Test (priority = 1)
  public void verifyLogin() {

    //Skip verifyLogin test in non prod environments
    System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get(url);

    if (url=="www.google.com")
    //If url matches production url then execute the test, if url doesn't match production URL then skip login test as there won't be any authentication/login in non prod
    {

        LoginPage login = new LoginPage(driver);
        login.signIn();
        Assert.assertEquals(driver.getTitle(), "Application Name", "Login failed,");
        String title = driver.getTitle();
        System.out.println("Page title is " + title);
        driver.close();
    }

    else if (url!="www.google.com"){

      throw new SkipException("Skipping this test");

    }

  }
  @Test(priority = 2)
  public void userKey() {

      System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);

      //If URL equals prod then call the login method to be able to login to the app else just execute the userKey method without having the need to login

      if (url=="www.google.com");
      {
        LoginPage login = new LoginPage(driver);
        login.signIn();
      }

      AccountManagementPage userKey = new AccountManagementPage(driver);
      userKey.key();
      driver.close();
  }

}

Answer №1

An in-depth explanation of the specific use case can be found here, with a focus on avoiding SkipException.

The concept entails creating a custom annotation for identifying methods to skip and utilizing IMethodInterceptor to determine execution based on certain criteria.


Regarding the question raised in the comments:

There is no need to involve 'TestEnvironment' or 'TestParameters' classes; simply implement the production check logic directly as shown below.

Predicate<IMethodInstance> isTestExecutingOnProduction = (tip) -> {
    return system.getProperty("env")
                              .equals("<production check here>");
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There was a critical error during compilation: java.lang.Il legalAccessError. It appears to be stemming from the class lombok.javac

Even after installing Lombok, updating Java, and setting all necessary System Variables for Maven and JAVA_HOME, I still encounter an error. The POM builds fine and all essential files like mvnw and mvnw.cmd are functioning properly. However, when attempti ...

Combining Spring Boot with React Using Material UI Design

Currently in the process of setting up a Spring Boot project (Prototype) using React and Material UI. To kick things off, I utilized this tutorial to successfully get Spring Boot functioning with React. Moving on to implementing Material UI, I followed st ...

Developing React applications using Behavior Driven Development with a personalized API call for simulating mock

Currently, I am utilizing BDD with cucumberjs, selenium-webdriver & react. Within a specific scenario, I wish to execute a Mock API call rather than an actual API call. For instance, I am currently making a call to: http://example.com/v1/getData However ...

I keep encountering a "map is not a function" error while working with React JS

I am attempting to retrieve and display data from a MySQL database using Spring Boot and React JS in a table format. However, I am encountering an error while trying to display the information. componentDidMount(){ const currentUser = AuthService.g ...

Guide on how to use Selenium to drag and drop a canvas web element specifically in the Chrome browser

Having trouble moving an image within the canvas web element (avatar editor) on Chrome using Selenium. Check out this canvas element: Watch a quick demo of what I'm trying to accomplish with Selenium Webdriver: Please review the code snippet below. ...

React-Collapsible Compilation Error

Currently, I am working on a project that involves Spring and ReactJS. I am still new to front-end development. One of the tasks I am trying to accomplish is creating a simple accordion using "REACT-COLLAPSIBLE". The code I have written is quite straight ...

Initiate a react change event using Appium

I'm currently working on automating a hybrid app that has a login screen implemented as a react web view, and unfortunately, I don't have control over it. The challenge I'm facing is that the Sign-in button remains disabled until something i ...