gpt4 book ai didi

android - 通过下一个测试用例的 appium java 脚本重新打开应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:51 26 4
gpt4 key购买 nike

我成功运行了 appium java 脚本。但问题是,我的应用程序从登录页面开始。登录后,我可以自动化一些测试用例。但我想通过脚本从登录页面再次运行以运行下一个测试用例..如何关闭应用程序以及从后台模式重新打开应用程序而无需再次重新安装应用程序?因为测试用例不应该相互依赖

public class AppTest {

private static RemoteWebDriver driver;

@BeforeClass
public static void initSimulator() throws MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.0.1");
capabilities.setCapability("deviceName", "emulator-5554");
capabilities.setCapability("app", "D:\\adt-bundle-windows-x86_64-20140702\\sdk\\platform-tools\\Yr.apk");
capabilities.setCapability("app-package", "com.you.android");
capabilities.setCapability("app-activity", "com.yr.sts.SplashActivity");
capabilities.setCapability("app_wait_activity",".MainActivity");

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);

System.out.println("App Launched");
}
@AfterClass
public static void quitDriver()
{
driver.quit();
System.out.println("Driver has been Quit");
}

@Test
public void sign_in_Click() throws InterruptedException
{
WebElement sign_button = driver.findElement(By.name("Sign-in"));
sign_button.click();
WebElement usr = driver.findElement(By.id("com.you.android:id/et_login_email_or_mobile"));
usr.sendKeys("hello1@gmail.com");
Thread.sleep(2000);
WebElement passwrd = driver.findElement(By.id("com.you.android:id/et_login_pwd"));
passwrd.sendKeys("123456789");
Thread.sleep(2000);
driver.findElementByName("Sign in").click();
Thread.sleep(5000);
assertTrue( true );
}
@Test
public void second_sign_in_Click() throws InterruptedException
{
//HERE I WANT TO REOPEN THE APPLICATION WITH SIGN IN AFTER CLOSING APP.BECAUSE IN 1st TESTCASE , I HAVE SIGNED IN.this is a 2nd test case
}
}

最佳答案

@Beforeclass@AfterClass 标签分别更改为 @Before@After

现在您的 initSimulator() 方法将在每个 @Test 之前被调用,并且 quitDriver() 方法将在它之后被调用。这样,您将在每种情况下重新启动新的驱动程序(带有清晰的应用程序)。

所以 - 如您所见 - 在 JUnit 中,默认情况下每个测试用例都是独立的。

为什么会出现问题?来自@BeforeClass docs :

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class.

顺便说一句,我的 initDriver() 方法看起来像这样,这是处理退出驱动程序的更好方法:

   public void quitDriver() {
driver.closeApp();
driver.quit();
driver = null;
}

(在你的情况下它是静态的)

关于android - 通过下一个测试用例的 appium java 脚本重新打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com