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

时间:2018-04-05 12:40:47

标签: testng call

在loginAppium类中@test的结尾,我想调用另一个类@test 类loginAppium

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

2 个答案:

答案 0 :(得分:1)

在测试脚本开发中,在测试方法中调用测试方法不是一个好习惯。

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

由于一种测试方法,其他测试方法不应该通过或失败。 (这明显不同于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();

答案 1 :(得分:0)

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

另一种方法是,您可以在基类中扩展目标类,并在没有任何annotation dependency的情况下调用该方法。