gpt4 book ai didi

c# - 通过调用公共(public)方法将 IApp 应用程序变量传递给私有(private)方法

转载 作者:行者123 更新时间:2023-11-30 16:45:41 24 4
gpt4 key购买 nike

假设以下代码位于名为 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 中的私有(private)方法。我尝试在 LandingPage 类中设置 IApp 应用程序,但是当我的代码在 Xamarin.UITests 中执行时,应用程序被返回为未定义,并且未输入数据。我还尝试将测试类中的 IApp 应用程序设置为公开,但这也不起作用。

所需的代码看起来类似于:

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"));
}
}

最佳答案

你需要在构造函数中注入(inject)应用

IApp _app;
public LandingPage(IApp app)
{
_app = app;
}

然后是方法..

private void LandingPageDisplayed()
{
_app.WaitForElement(c => c.Marked("txtLogin").Text("Login"));
}

然后在你的测试中

var  landingpage = new LandingPage(app);

此外,如果您的测试类确实是LandingPageTests,那么您可能需要

public class LandingPageTests
{
IApp app;
Platform platform;
LandingPage _landingPage;

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

[SetUp]
public void BeforeEachTest()
{
app = AppInitializer.StartApp(platform);
_landingPage = new LandingPage(app);
}

[Test]
public void AttemptInvalidLogin()
{
_landingPage.Login(app, "test123", "test123");
}
}

关于c# - 通过调用公共(public)方法将 IApp 应用程序变量传递给私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41686414/

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