简体   繁体   English

通过调用public方法将IApp app变量传递给私有方法

[英]Pass IApp app variable to private methods from calling public method

Given the following code is in a file named Tests.cs: 鉴于以下代码位于名为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");
    }
}

The following code is in a file named LandingPage.cs: 以下代码位于名为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"));
    }
}

I am wanting to learn of a more efficient way to pass the variable "app" to the private methods inside LandingPage.cs. 我想学习一种更有效的方法将变量“app”传递给LandingPage.cs中的私有方法。 I tried setting IApp app in the LandingPage class, but when my code executes in Xamarin.UITests, app is being returned as undefined, and data is not entered. 我尝试在LandingPage类中设置IApp应用程序,但是当我的代码在Xamarin.UITests中执行时,app将作为未定义返回,并且不会输入数据。 I also tried setting IApp app in the Tests class to public, but this does not work either. 我也尝试将Tests类中的IApp应用程序设置为public,但这也不起作用。

The desired code would look similar to this: 所需的代码看起来类似于:

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

you need to inject the app in the constructor 你需要在构造函数中注入应用程序

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

and then methods.. 然后方法..

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

then in your tests 然后在你的测试中

var  landingpage = new LandingPage(app);

Also, if your tests class is really LandingPageTests then you may want 此外,如果您的测试类确实是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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM