繁体   English   中英

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

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

我对Selenium UI自动化非常陌生,并且正在尝试使用一个简单的应用程序。 我将Java与testNG一起使用。 我需要将此测试与CI集成在一起,并且每个部署的测试环境URL都会有所不同。 我将其作为参数传递,测试和产品环境之间的区别在于,在测试中,不会有任何登录屏幕,因此不需要进行身份验证,但是我的测试将验证登录和登录方法。 我需要能够根据提供的URL跳过此测试。 这是我的代码,问题是testNG总是表明测试已被跳过,但是我可以看到它正在执行login方法。 请帮助我纠正或理解我犯的错误。

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

}

不抛出SkipException的情况下很好地解释非常精确的用例。

想法是为要跳过的方法创建自定义批注,并使用IMethodInterceptor读取方法-决定是否在后台执行。


针对评论部分中的问题进行了更新:

您不必担心'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