如何从C#中的第一个类开始调用第二个方法

时间:2017-02-22 12:07:02

标签: c# xamarin automated-tests

在C#中如何从第一类调用第二类中的方法

在我的第二节课中,我不想再次输入用户名,密码或公司名称,而只是想调用第一个类名的方法。

我怎么能这样做?我尽我所能,但我无法解决这个问题?在许多类文件中,我再次编写相同的代码,而不是这样做,我只是想调用方法名称。

头等舱:

private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Image focus");
    Image img = sender as Image;
    Bgimage.Source = img.Source;
}

第二类:

namespace Example.UITest
 {
 public class LoginTests : AbstractSetup
 {
    public LoginTests(Platform platform) : base(platform)
    {
        }
     [Test]
    public void Login_SuccessfullAuthentication_SuccessfullLogin()
    {

        //Enter Username, company name & Password
        app.EnterText(x => x.Marked("Username"), "annby");
        app.EnterText(x => x.Marked("Company name"), "sara");
        app.EnterText(x => x.Marked("Password"), "sara");
        //Tapping "Sign in" button after submitting user credentials
        app.Tap(x=>x.Text("Sign in"));

  }
  }


}

3 个答案:

答案 0 :(得分:1)

您应该将登录逻辑提取到特定方法,然后在Login_SuccessfullAuthentication_SuccessfullLoginCreateAppoinment中使用它。

我建议将其移至AbstractSetup,因为您需要更多地方。

示例:

public abstract class AbstractSetup
{    
    public void DoLogin()
    {
        //Enter Username, company name & Password
        app.EnterText(x => x.Marked("Username"), "annby");
        app.EnterText(x => x.Marked("Company name"), "sara");
        app.EnterText(x => x.Marked("Password"), "sara");
        //Tapping "Sign in" button after submitting user credentials
        app.Tap(x=>x.Text("Sign in"));
    }   

}   

[Test]
public void Login_SuccessfullAuthentication_SuccessfullLogin()
{
    DoLogin();
}

[Test]
public void CreateAppoinment() 
{ 
    DoLogin();
    app.WaitForElement(x => x.Id("action_bar_title"), timeout: TimeSpan.FromSeconds(10));
    app.Tap(x => x.Id("action_bar_title"));
}

答案 1 :(得分:0)

  1. 尝试在第一个类中将您的方法标记为静态,如下所示:

    public static void Login_SuccessfullAuthentication_SuccessfullLogin() {...}

  2. 之后你可以打电话就是这样:

    LoginTests.Login_SuccessfullAuthentication_SuccessfullLogin();
    
    1. 或者从第一个类创建一个对象并调用其方法:

      AppoinmentTest myTest = new AppoinmentTest(platform); myTest.Login_SuccessfullAuthentication_SuccessfullLogin();

答案 2 :(得分:0)

尝试在Appointment Class中创建LoginTests类的对象。我在你的代码中做了一些修改。希望他们为你工作!

namespace Example.UITest
 {
 public class LoginTests : AbstractSetup
 {
    public LoginTests(Platform platform) : base(platform)
    {
        }

    public void Login_SuccessfullAuthentication_SuccessfullLogin()
    {

        //Enter Username, company name & Password
        app.EnterText(x => x.Marked("Username"), "annby");
        app.EnterText(x => x.Marked("Company name"), "sara");
        app.EnterText(x => x.Marked("Password"), "sara");
        //Tapping "Sign in" button after submitting user credentials
        app.Tap(x=>x.Text("Sign in"));

  }
  }
  }
 // THIS IS MY SECOND CLASS. 
namespace Example.UITest
 {
 public class AppoinmentTest : AbstractSetup
 {
     LoginTests lt = new LoginTests(); //object of class LoginTests

    public AppoinmentTest(Platform platform) : base(platform)
    {
    }


    public void CreateAppoinment() { 
       lt.Login_SuccessfullAuthentication_SuccessfullLogin()              //Here i want to call the login method before doing below functionality
        app.Tap(x => x.Text("Sign in"));
        app.WaitForElement(x => x.Id("action_bar_title"), timeout:     TimeSpan.FromSeconds(10));
        app.Tap(x => x.Id("action_bar_title"));
        }}
}