我正在使用 Java(TestNG 和 IntelliJ)运行 Selenium 测试,目前正在研究我的报告框架。 我的测试设置看起来像这样:
@Test
public void testLogin(){
myKeyword.login();
}
myKeyword 是我创建的一类关键字,有一个名为 login 的方法。登录关键字如下所示:
public void login(){
myPage.typeInTextBox("Username");
myPage.typeInTextBox("Password");
myPage.buttonClick("Login");
}
我的验证内置于方法“buttonClick”和“typeInTextBox”中。它们看起来像:
try{
driver.findelement(By.id("myButton")).click();
}
catch (Exception e){
Assert.fail("Could not click on the button.");
}
我想知道的是,“buttonClick”方法是否可以知道调用它的测试的名称?
我想用我自己的函数替换“Assert.fail...”,该函数将创建一个文本文件,用于记录我想要的信息,该文件需要包含失败测试的名称(在本例中为“testLogin”。
最佳答案
一个选项是在某处设置当前测试的名称,可能是一个静态变量。描述了如何为 TestNG 做到这一点 here .
public class Test {
@BeforeMethod
public void handleTestMethodName(java.lang.reflect.Method method) {
GlobalVariables.currentTestName = method.getName();
}
...
}
关于java - 是否可以从被调用的方法中写入 testng 测试的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323867/