在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"));
}
}
}
答案 0 :(得分:1)
您应该将登录逻辑提取到特定方法,然后在Login_SuccessfullAuthentication_SuccessfullLogin
和CreateAppoinment
中使用它。
我建议将其移至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)
尝试在第一个类中将您的方法标记为静态,如下所示:
public static void Login_SuccessfullAuthentication_SuccessfullLogin()
{...}
LoginTests.Login_SuccessfullAuthentication_SuccessfullLogin();
或者从第一个类创建一个对象并调用其方法:
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"));
}}
}