我有一个使用Appium和TestNG开发的脚本设置。 TestNG xml包含多个类的执行,每个类具有多个测试用例。
示例:
Class1:
-Class1_Test1
-Class1_Test2
-Class1_Test3
Class2:
-Class2_Test1
-Class2_Test2
-Class2_Test3
我尝试集成IRetryAnalyzer,但这只是调用失败的测试用例。要求是执行完整的Class1,以防Class1_Test2在Class1失败之前立即失败,然后再继续执行Class2?
询问的原因是该应用是媒体播放器,如果由于网络/服务器问题而导致媒体播放失败,则无需执行下一个前进和后退测试案例,并且需要重新启动应用程序,然后重试所有步骤,然后再执行进一步的测试。
答案 0 :(得分:1)
根据TestNg文档无法实现此目标,可能在下面的答案可以为您提供帮助
Retry Logic - retry whole class if one tests fails - selenium
答案 1 :(得分:0)
我正在使用小组测试。即使该类中的某些测试失败,它也将继续测试。
您可以在班级文件中定义如下的组。
public class myClass(){
@Test(groups = {"abc"}, priority = 1)
public void test1(){
}
@Test(groups = {"abc"}, priority = 2)
public void test2(){
}
@Test(groups = {"abc"}, priority = 3)
public void test3(){
}
}
类似地,您可以使用相同的组名或不同的组名定义第二类。优先级确定测试用例的运行顺序。
您的Testng.xml文件如下所示:
<test name="any name">
<groups>
<run>
<include name="abc" />
<include name="any other group name" />
</run>
</groups>
<classes>
<class name="packageName.myClass"/>
<class name="your_packageName.class2"/>
</classes>
</test>
packageName是Test类所在的路径。如果您的测试类和testng.xml文件位于同一软件包中,则不需要packageName。
有关Testng中测试方法的更多信息,请参见this link。
答案 2 :(得分:0)
最后找到了重新运行整个类的解决方法。我将其称为解决方法,因为从技术上讲TestNG不能提供一种在任何时间发生故障的情况下重新执行@BeforeTest的方法。
我发现的最好的方法是没有@BeforeTest节,只有一个@Test节,并将所有测试用例作为函数,将在定义的单个@Test中调用。因此,如果在任何时间点发生故障,都将调用@Test,其中@Test包含所需功能的所有顺序,包括设置功能。一旦发现故障,重试逻辑将重新运行整个@Test部分。
示例:
更改前:
package <yourpackagename>;
<import required packages>
public class Home {
private AppiumDriver<?> driver;
private static final String url = "http://0.0.0.0:4723/wd/hub";
<define your variables>
@Parameters({"deviceOS", "DSN"})
@BeforeTest
public void setUp(String deviceOS, String DSN) throws InterruptedException, MalformedURLException, ParseException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "FireTVStick");
capabilities.setCapability("platformVersion", deviceOS);
capabilities.setCapability("udid", DSN);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("fullReset", false);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);
capabilities.setCapability("appPackage", "<your app package>");
capabilities.setCapability("appActivity", "<your launcher activity>");
driver = new AndroidDriver<>(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//End of Launch Code
System.out.println("-Testing Home Section-");
}
@Parameters({"DSN"})
@Test
public void Test1_VideoPlaybackVerification(String DSN) throws InterruptedException, ParseException{
//Video playback verification code starts
.
.
//End of code for Video playback verification
}
@Test //Test Case for Pause verification
public void Test2_PauseVerification() throws InterruptedException, ParseException{
//Video pause verification code starts
.
.
//End of code for Video pause verification
}
@AfterTest
public void End() {
driver.quit();
}
}
更改后:
package <yourpackagename>;
<import required packages>
@Listeners(MyTestListenerAdapter.class)
public class Home {
private AppiumDriver<?> driver;
<define your variables>
public void setUp(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformVersion", deviceOS);
capabilities.setCapability("deviceName", deviceName);
capabilities.setCapability("udid", DSN);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("fullReset", false);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);
capabilities.setCapability("appPackage", "<your app package>");
capabilities.setCapability("appActivity", "<your launcher activity>");
final String url = "http://127.0.0.1:"+port+"/wd/hub";
driver = new AndroidDriver<>(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
public void HomeVerification(String DSN, String deviceName) throws InterruptedException, ParseException
{
System.out.println(deviceName+": Testing Home Section-");
<--Your code to perform any additional task before execution-->
}
public void Test1_VideoPlaybackVerification(String DSN, String deviceName) throws InterruptedException, ParseException
{
//Video playback verification code starts
.
.
//End of code for Video playback verification
}
public void Test2_PauseVerification(String deviceName) throws InterruptedException, ParseException
{
//Video pause verification code starts
.
.
//End of code for Video pause verification
}
@Parameters({"port", "deviceOS", "DSN", "deviceName"})
@Test (retryAnalyzer = Retry.class)
public void TestRun(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, ParseException, MalformedURLException {
try {
setUp(port, deviceOS, DSN, deviceName);
HomeVerification(DSN, deviceName);
Test1_VideoPlaybackVerification(DSN, deviceName);
Test2_PauseVerification(deviceName);
} catch (WebDriverException e) {
// TODO Auto-generated catch block
Reporter.log(deviceName+": Error observed while executing script!", true);
Assert.assertTrue(false); //Fails the test case
}
}
@AfterTest
public void End() {
driver.quit();
}
}