鉴于以下代码位于名为 Tests.cs 的文件中:
public class Tests
{
IApp app;
Platform platform;
public Tests(Platform platform)
{
this.platform = platform;
}
[SetUp]
public void BeforeEachTest()
{
app = AppInitializer.StartApp(platform);
}
[Test]
public void AttemptInvalidLogin()
{
LandingPage landingPageObjects = new LandingPage();
landingPageObjects.Login(app, "test123", "test123");
}
}
以下代码位于名为 LandingPage.cs 的文件中:
public class LandingPage
{
public void Login(IApp app, String Email, String Password)
{
LandingPageDisplayed(app);
EnterEmailAddress(app, Email);
EnterPassword(app, Password);
}
private void LandingPageDisplayed(IApp app)
{
app.WaitForElement(c => c.Marked("txtLogin").Text("Login"));
}
private void EnterEmailAddress(IApp app, String emailAddress)
{
app.EnterText(c => c.Marked("editvalidid"), emailAddress);
}
private void EnterPassword(IApp app, String password)
{
app.EnterText(c => c.Marked("editvalidpassword"), password);
}
private void TapLoginButton(IApp app)
{
app.Tap(c => c.Marked("btnlogin"));
}
}
我想学习一种更有效的方法来将变量“app”传递给 LandingPage.cs 中的私有方法。我尝试在 LandingPage 类中设置 IApp 应用程序,但是当我的代码在 Xamarin.UITests 中执行时,应用程序作为未定义返回,并且未输入数据。我还尝试将 Tests 类中的 IApp 应用程序设置为 public,但这也不起作用。
所需的代码将类似于以下内容:
public class LandingPage
{
IApp app = Tests.app;
public void Login(String Email, String Password)
{
LandingPageDisplayed();
EnterEmailAddress(Email);
EnterPassword(Password);
}
private void LandingPageDisplayed()
{
app.WaitForElement(c => c.Marked("txtLogin").Text("Login"));
}
private void EnterEmailAddress(String emailAddress)
{
app.EnterText(c => c.Marked("editvalidid"), emailAddress);
}
private void EnterPassword(String password)
{
app.EnterText(c => c.Marked("editvalidpassword"), password);
}
private void TapLoginButton()
{
app.Tap(c => c.Marked("btnlogin"));
}
}