C# 将IApp app变量从调用公共方法传递给私有方法

C# 将IApp app变量从调用公共方法传递给私有方法,c#,xamarin.uitest,C#,Xamarin.uitest,给定以下代码位于名为Tests.cs的文件中: public class Tests { IApp app; Platform platform; public Tests(Platform platform) { this.platform = platform; } [SetUp] public void BeforeEachTest() { app = AppInitializer.Star

给定以下代码位于名为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"));
    }
}

您需要将应用程序注入构造函数中

IApp _app;
public LandingPage(IApp app) 
{
   _app = app;
}
然后是方法

private void LandingPageDisplayed()
{
   _app.WaitForElement(c => c.Marked("txtLogin").Text("Login"));
}
然后在你的测试中

var  landingpage = new LandingPage(app);
另外,如果您的tests类实际上是
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");
    }
}

非常感谢你,基思。你的回答让我成功了。为了确保我理解正确,这段代码:IApp\u app;公共登录页(IApp-app){u-app=app;}指示此类接受名为app的对象,并将输入分配给_-app。通过以下方式调用:_landingPage=新的landingPage(app);将UI树传递到方法要使用的类中。我的思维过程是否正确?一般来说,为了更好的设计,通过构造函数和接口将依赖项注入。这将导致使用依赖注入框架等