简体   繁体   English

在SkipException,testNG上遇到问题,我在做什么错?

[英]Having trouble with SkipException, testNG, what am I doing wrong?

I am very new to selenium UI automation and I am trying my hands on with a simple application. 我对Selenium UI自动化非常陌生,并且正在尝试使用一个简单的应用程序。 I am using java with testNG. 我将Java与testNG一起使用。 I need to integrate this test with CI and the test environment url will be different with every deployment. 我需要将此测试与CI集成在一起,并且每个部署的测试环境URL都会有所不同。 I am passing this as a parameter, difference between test and prod environment is that in the test, there won't be any login screen hence there is no need for authentication but my test verifies login and a login method. 我将其作为参数传递,测试和产品环境之间的区别在于,在测试中,不会有任何登录屏幕,因此不需要进行身份验证,但是我的测试将验证登录和登录方法。 I need to be able to skip this test based on the URL supplied. 我需要能够根据提供的URL跳过此测试。 Here is my code and the problem is testNG always suggests that the test was skipped but I can see it executing the login method. 这是我的代码,问题是testNG总是表明测试已被跳过,但是我可以看到它正在执行login方法。 Please help me correct or understand what mistake I am committing. 请帮助我纠正或理解我犯的错误。

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();
  }

}

The very exact use case is well explained here without throwing SkipException. 不抛出SkipException的情况下很好地解释非常精确的用例。

The idea is to create a custom annotation for the methods to be skipped & read the methods using IMethodInterceptor - decide to execute or not behind the scenes. 想法是为要跳过的方法创建自定义批注,并使用IMethodInterceptor读取方法-决定是否在后台执行。


Updated for the question in the comment section: 针对评论部分中的问题进行了更新:

You do not have to worry about 'TestEnvironment' or 'TestParameters' class.Just directly use the production check logic here. 您不必担心'TestEnvironment'或'TestParameters'类。只需在此处直接使用生产检查逻辑即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM