Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从C中的第一个类调用第二个类中的方法#_C#_Xamarin_Automated Tests - Fatal编程技术网

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

C# 如何从C中的第一个类调用第二个类中的方法#,c#,xamarin,automated-tests,C#,Xamarin,Automated Tests,在C#中,如何从第一个类调用第二个类中的方法 在第二个类中,我只想调用第一个类名的方法,而不是再次输入用户名、密码或公司名称 我怎么能做到?我已经尽力了,但我不能解决这个问题?在许多类文件中,我再次编写相同的代码,而不是那样做,我只是想调用方法名 头等舱: 二等舱: 您应该将登录逻辑提取到特定的方法中,然后在登录\u SuccessfullAuthentication\u SuccessfullLogin和CreateAppoint中使用它 我建议将它移动到您的抽象设置,因为您需要更多的位置 例

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

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

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

头等舱: 二等舱:
您应该将登录逻辑提取到特定的方法中,然后在
登录\u SuccessfullAuthentication\u SuccessfullLogin
CreateAppoint
中使用它

我建议将它移动到您的
抽象设置
,因为您需要更多的位置

例如:

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"));
}
  • 尝试在第一个类中将方法标记为静态,如下所示:

    public static void Login\u SuccessfullAuthentication\u SuccessfullLogin()
    {…}

  • 之后你可以打电话,就像这样:

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

    appointtest myTest=新的appointtest(平台);
    myTest.Login_SuccessfullAuthentication_SuccessfullLogin()


  • 尝试在约会类中创建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"));
            }}
    }
    

    创建第一个类的实例并调用该方法。问题出在哪里?@ragesh-ragav-1993如果您能将我的答案标记为正确,将不胜感激。这是我问题的副本。请把这个拿走。谢谢。这是我问题的副本。请删除它。@jzeferino显示错误“平台是一种类型,在给定上下文中无效”这不是我的答案。你在用我的答案吗?请分享代码。
    LoginTests.Login_SuccessfullAuthentication_SuccessfullLogin();
    
    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"));
            }}
    }