Java SkipException、testNG有问题,我做错了什么?
我对selenium UI自动化非常陌生,我正在尝试一个简单的应用程序。我将java与testNG结合使用。我需要将此测试与CI集成,每个部署的测试环境url都将不同。我把它作为一个参数传递,test和prod环境之间的区别在于,在测试中,没有任何登录屏幕,因此不需要身份验证,但我的测试验证登录和登录方法。根据提供的URL,我需要能够跳过此测试。这是我的代码,问题是testNG总是建议跳过测试,但我可以看到它正在执行登录方法。请帮助我纠正或理解我犯的错误Java SkipException、testNG有问题,我做错了什么?,java,selenium,selenium-webdriver,testng,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Testng,Selenium Chromedriver,我对selenium UI自动化非常陌生,我正在尝试一个简单的应用程序。我将java与testNG结合使用。我需要将此测试与CI集成,每个部署的测试环境url都将不同。我把它作为一个参数传递,test和prod环境之间的区别在于,在测试中,没有任何登录屏幕,因此不需要身份验证,但我的测试验证登录和登录方法。根据提供的URL,我需要能够跳过此测试。这是我的代码,问题是testNG总是建议跳过测试,但我可以看到它正在执行登录方法。请帮助我纠正或理解我犯的错误 public class 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
读取方法-决定是否在幕后执行
针对评论部分中的问题进行了更新: 您不必担心“TestenEnvironment”或“TestParameters”类。只需在此处直接使用生产检查逻辑即可
Predicate<IMethodInstance> isTestExecutingOnProduction = (tip) -> {
return system.getProperty("env").
.equals("<production check here>");
};
谓词isTestExecutingOnProduction=(提示)->{
返回system.getProperty(“env”)。
.等于(“”);
};
Hey vins,感谢您的评论,不幸的是,由于我的局限性,我认为我不太了解,您是否有一个在某处使用此实现的示例项目可以帮助我理解?非常感谢任何帮助,非常感谢thanks@mathur,查看此网站-感谢Vins,我有几个基本问题,我正在使用一个变量从maven传递url,如“String url=System.getProperty(“env”);”及其在基类中。我不清楚的是如何合并“TestenEnvironment”枚举和“TestParameter”类。“NotOnProduction.class”是保留批注吗?@mathur,无法在评论部分回答。检查更新的答案