TestNG从另一个测试方法调用测试方法

TestNG从另一个测试方法调用测试方法,testng,call,Testng,Call,在loginapium类中@test的末尾,我想调用另一个类@test 逻辑类 @Test public void testLogin() { driver.findElement(By.xpath("//*[@text='Login with your LabOra Id']")).click(); new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfElementLocated(By.xpath(

在loginapium类中@test的末尾,我想调用另一个类@test 逻辑类

@Test
public void testLogin() {
    driver.findElement(By.xpath("//*[@text='Login with your LabOra Id']")).click();
    new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='Login' and @class='android.view.View']")));
    driver.findElement(By.xpath("//*[@id='username']")).sendKeys("agrando.srilanka");
    new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='password']")));
    driver.findElement(By.xpath("//*[@id='password']")).sendKeys("embla");
    driver.findElement(By.xpath("//*[@id='btnLogin']")).click();
    new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='Mönsterstad pastorat']")));
    driver.findElement(By.xpath("//*[@text='Mönsterstad pastorat']")).click();


   addAppointment addApp = new addAppointment();
   addApp.testaddAppointment();

}
我想打电话到这里

   @Test
public void testaddAppointment() {

    //Logout
    new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='android.widget.ImageView' and @width>0 and ./parent::*[@class='android.view.ViewGroup' and (./preceding-sibling::* | ./following-sibling::*)[./*[@text='Logout']]]]")));
    driver.findElement(By.xpath("//*[@class='android.widget.ImageView' and @width>0 and ./parent::*[@class='android.view.ViewGroup' and (./preceding-sibling::* | ./following-sibling::*)[./*[@text='Logout']]]]")).click();
    driver.findElement(By.xpath("//*[@text='Yes']")).click();
}

但它会在下一阶段关闭应用程序

 addAppointment addApp = new addAppointment();
 addApp.testaddAppointment();

在测试脚本开发中,在测试方法中调用测试方法不是一个好的实践

因为每种测试方法都应该独立于其他测试方法

由于一种测试方法,其他测试方法不应通过或失败。(这与dependsOnMethods和dependsOnGroup明显不同) 他们有自己的目的

测试方法不同于实例方法或静态方法

测试方法也可以具有不同的测试属性和测试属性以及测试数据

就你而言:

 addAppointment addApp = new addAppointment();
 addApp.testaddAppointment();
相反,尝试创建一个实例方法并在测试方法中调用它

希望这能解决你的问题

如果您还有任何问题,请告诉我

更新

我正在包装这行代码。请注意,我已从此方法中删除@Test注释

public void testaddAppointment() {

    //Logout
    new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='android.widget.ImageView' and @width>0 and ./parent::*[@class='android.view.ViewGroup' and (./preceding-sibling::* | ./following-sibling::*)[./*[@text='Logout']]]]")));
    driver.findElement(By.xpath("//*[@class='android.widget.ImageView' and @width>0 and ./parent::*[@class='android.view.ViewGroup' and (./preceding-sibling::* | ./following-sibling::*)[./*[@text='Logout']]]]")).click();
    driver.findElement(By.xpath("//*[@text='Yes']")).click();
}
你可以这样称呼它:

  addAppointment addApp = new addAppointment();
  addApp.testaddAppointment();

我建议从另一个类中删除
@Test annotation
,并将其称为 你必须定义


另一种方法是,您可以在基类中扩展目标类并调用该方法,而无需任何
注释依赖关系

查看addAppointment()的来源,testaddAppointment()可能有助于回答问题所在。它运行testLogin(),但随后关闭应用程序而不调用testaddAppointment()。为什么不将testaddAppointment包含提取为另一个新方法,并在testLogin()结束时调用它?@SvichkarevAnatoly我已经完成了这个'addAppointment addApp=new addAppointment();addApp.testaddappoiment();'但是它不调用。这很清楚,但是你能告诉我如何使用实例方法调用它,并在测试方法中调用它吗?@user2401106:已经很久了,让我再次回顾一下这个问题。我一直在处理其他项目,这就是为什么我不可用的原因。@user2401106:我已经在更新部分更新了我的答案。请核实一下。