我有一个使用Appium和TestNG开发的脚本设置。TestNG xml包含多个类的执行,每个类都有多个测试用例。
示例:
Class1:
-Class1_Test1
-Class1_Test2
-Class1_Test3
Class2:
-Class2_Test1
-Class2_Test2
-Class2_Test3
我尝试集成IRetryAnalyzer,但这只是调用失败的测试用例。要求是执行完整的Class1,以防Class1_Test2Class1在进入Class2之前失败?
询问的原因是应用程序是一个媒体播放器,如果由于网络/服务器问题导致媒体播放失败,则不需要执行前进和倒带的下一个测试用例,它需要重新启动应用程序并重试所有步骤,然后再执行进一步的测试。
根据TestNg留档,没有办法做到这一点,可能下面的答案可以帮助你
重试逻辑-如果一个测试失败,重试整个类-selenium
我正在使用小组测试。即使课堂上有一些测试失败,它也会继续测试。
在您的类文件中,您可以像以下一样定义组。
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中测试方法的更多信息,请参阅此链接。
终于找到了重新运行整个类的解决方法。我会称之为解决方法,因为从技术上讲,TestNG没有提供一种在任何时间点发生故障时重新执行@BeforeTest的方法。
我发现最好的方法是没有@BeforeTest部分,只有一个@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();
}
}