添加客服微信
400 035 7887
021-60725088-8054
然后,我们需要创建MainScreen和AddingItemScreen类来描述主屏幕和向我们的应用程序添加新项目的屏幕。此外,这里我们描述了一些与屏幕元素交互的方法。
此外,我们需要一个Screen将成为工厂类并返回某些页面实例的类。
这是一个工厂类,它将返回我们需要的屏幕实例。
class Screen{ private static MainScreen mainScreen; private static AddingItemScreen addingItemScreen; public static MainScreen Main => mainScreen ?? (mainScreen = new MainScreen()); public static AddingItemScreen AddingItem => addingItemScreen ?? (addingItemScreen = new AddingItemScreen());}
在MainScreen类描述我们的应用程序的主屏幕。之前,我们已经BaseScreen使用基本方法创建了类。MainScreen从BaseScreen类继承。现在我们可以使用那里描述的方法与页面元素进行交互。
class MainScreen : BaseScreen{ //Add button locator Func<AppQuery, AppQuery> addButton = x => x.Marked("Add"); //Waiting Add button public void WaitForAddButton() { WaitForElement(addButton); } //Clicking Add button public void ClickAddButton() { Tap(addButton); } // Checking is item name present in the list public bool IsItemNamePresent(string ItemName) { return IsElementPresentByText(ItemName); } // Checking is item description present in the list public bool IsItemDescriptionPresent(string ItemDescription) { return IsElementPresentByText(ItemDescription); }}
同为 AddingItemScreen
class AddingItemScreen : BaseScreen { private readonly Func<AppQuery, AppQuery> itemNameField = x => x.Marked("item_name_Container").Descendant().Index(1); private readonly Func<AppQuery, AppQuery> itemDescriptionField = x => x.Marked("item_description_Container").Descendant().Index(1); private readonly Func<AppQuery, AppQuery> saveButton = x => x.Marked("save_button"); //设置项目名称 public void SetItemName(string ItemName) { Type(ItemName, itemNameField); } //设置项目描述 public void SetItemDescription(string ItemDescription) { Type(ItemDescription, itemDescriptionField); } //点击保存按钮 public void TapSaveButton() { Tap(saveButton); } //等待保存按钮 public void WaitForSaveButton() { WaitForElement(saveButton); } }
在我们描述了返回所需类的实例的所有必要屏幕和方法之后,我们可以使用我们项目的新结构重写测试。
public class Tests : BaseTest { public Tests(Platform platform) : base(platform) { } [Test] public void ItemCreationTest() { // 使用模板生成随机项目名称和描述: // item_name + timestamp // item_description + timestamp String itemName = "item_name" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); String itemDescription = "item_description" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); //等待'添加'按钮 //app.WaitForElement(x => x.Marked("Add")); //等待“添加”按钮 Screen.Main.WaitForAddButton(); // 点击“添加”按钮 Screen.Main.ClickAddButton(); //等待“保存”按钮 Screen.AddingItem.WaitForSaveButton(); //将文本设置为“项目名称”文本字段 Screen.AddingItem.SetItemName(itemName); //将文本设置为“项目描述”文本字段 Screen.AddingItem.SetItemDescription(itemDescription); //点击“保存”按钮 Screen.AddingItem.TapSaveButton(); //使用生成的itemName验证项目是否存在 Assert.IsTrue(Screen.Main.IsItemNamePresent(itemName)); //使用生成的 itemDescription 验证项目是否存在 Assert.IsTrue(Screen.Main.IsItemDescriptionPresent(itemDescription));
最后,如果我们想启动应用程序,但 Screen 类的方法是静态的,我们可以在不创建类实例的情况下请求它们,我们将获得请求的简短记录。
Screen.Main.WaitForAddButton(); Assert.IsTrue(Screen.Main.IsItemNamePresent(itemName));推荐阅读:
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-60725088-8054),我们将立即处理,马上删除。