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