使用基类和ScreenObject编写Xamarin.UITest测试(一)

作者:ScreenObject编写Xamarin.UITest测试   发布时间:2021-10-12

为 Xamarin.UITest 测试创建基类

随着 Xamarin.UITest 测试数量的增加,扩展项目以及在测试类中保存测试方法和初始化应用程序启动变得越来越具有挑战性。我们必须将此初始化设置到另一个类中,以便在其他测试中重用初始化代码。通过这种方式,我们可以更轻松地扩展测试用例和扩展测试套件。

让我们首先创建一个名为的文件夹Base,我们可以在其中保存具有基本配置的类。在这个文件夹中,让我们也创建一个BaseTest具有基本配置的类。

public class BaseTest
{
  public static IApp app;
  Platform platform;

  private static ThreadLocal<IApp> concurrentApp = new ThreadLocal<IApp>();

  public static IApp GetApp()
  {
    return concurrentApp.Value;
  }

  public BaseTest(Platform platform)
  {
    this.platform = platform;
  }

  [SetUp]
  public void BeforeEachTest()
  {
    concurrentApp.Value = AppInitializer.StartApp(platform);
  }
}

private static ThreadLocal<IApp> concurrentApp = new ThreadLocal<IApp>();


使用此配置,我们可以同时运行多个测试。它还确保其他测试用例不会在到期时间之前关闭,其余代码将从测试类中移出。

使用 ScreenObject 进一步优化

为了使其更加优化,我们可以使用ScreenObject模式来描述应用程序屏幕的组件。

BaseScreen

现在让我们创建一个BaseScreen类,该类将使用 Xamarin.Forms 应用程序的元素描述基本操作。

class BaseScreen { 
  protected void Tap(Func<AppQuery, AppQuery> query) 
  { 
    BaseTest.GetApp().Tap(query); 
  } 
  protected void WaitForElement(Func<AppQuery, AppQuery> query) 
  { 
    BaseTest.GetApp().WaitForElement(query); 
  } 
  protected void Type(string text, Func<AppQuery, AppQuery> query) 
  { 
    BaseTest.GetApp().ClearText(query); 
    BaseTest.GetApp().EnterText(query, text); 
  } 
  protected bool IsElementPresentByText(string text) 
  { 
    return BaseTest.GetApp().Query(x => x.Text(text)).Any(); 
  } }

这里,Func<AppQuery, AppQuery> query是一个参数,用于描述我们需要的元素的定位器。



推荐阅读:

真机兼容测试

真机调试

掌上真机

多机联动

标准兼容性测试



本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-60725088-8054),我们将立即处理,马上删除。



沪ICP备07036474号-4 |

沪公网安备 31010702003220号

2015-2023 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd.