C # da birinchi darajadan ikkinchi usulni qanday chaqirish mumkin

C # da birinchi sinfdan ikkinchi sinfda qanday usulni chaqirish mumkin

Mening ikkinchi sinfimda yana foydalanuvchi nomini, parolni yoki kompaniya nomini kiritish o'rniga, men faqat birinchi sinf nomini chaqirishni istardim.

Buni qanday amalga oshirishim mumkin? Men eng yaxshi ishlarni qildim, ammo buni hal qila olmaymanmi? Ko'pgina sinfi fayllarida yana bir xil kodni yozib qo'yishimga to'g'ri keladi, buning o'rniga faqat metod nomini chaqirishni xohladim.

Birinchi sinf:

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

  }
  }


}

Ikkinchi daraja:

namespace Example.UITest
 {
 public class AppoinmentTest : AbstractSetup
 {
    public AppoinmentTest(Platform platform) : base(platform)
    {
    }
    [Test]
    public void CreateAppoinment() { 
       //Here i want to call the method
        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"));
        }}
}
1
Birinchi klass namunasini yarating va usulni chaqiring. Muammo qaerda?
qo'shib qo'ydi muallif HimBromBeere, manba
Birinchi klass namunasini yarating va usulni chaqiring. Muammo qaerda?
qo'shib qo'ydi muallif HimBromBeere, manba
@ ragesh-ragav-1993 javobimni to'g'ri deb belgilasangiz, buni qadrlaydi.
qo'shib qo'ydi muallif jzeferino, manba
@ ragesh-ragav-1993 javobimni to'g'ri deb belgilasangiz, buni qadrlaydi.
qo'shib qo'ydi muallif jzeferino, manba

6 javoblar

Kirish mantiqini muayyan usulga aylantirib, uni Login_SuccessfullAuthentication_SuccessfullLogin va CreateAppoinment ilovasidan foydalaning.

Buni AbstractSetup ga ko'chirishni tavsiya qilaman, chunki u ko'proq joylarga kerak bo'ladi.

Misol:

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
qo'shib qo'ydi

Kirish mantiqini muayyan usulga aylantirib, uni Login_SuccessfullAuthentication_SuccessfullLogin va CreateAppoinment ilovasidan foydalaning.

Buni AbstractSetup ga ko'chirishni tavsiya qilaman, chunki u ko'proq joylarga kerak bo'ladi.

Misol:

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
qo'shib qo'ydi

Uchrashuv sinfida LoginTests sinfining ob'ektini sinab ko'ring. Kodingizda bir necha o'zgarishlar qildim. Umid qilamanki ular siz uchun ishlaydi!

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"));
        }}
}
0
qo'shib qo'ydi
Bu mening savolimning dublikati. Iltimos, buni olib tashlang. Rahmat.
qo'shib qo'ydi muallif jzeferino, manba

Uchrashuv sinfida LoginTests sinfining ob'ektini sinab ko'ring. Kodingizda bir necha o'zgarishlar qildim. Umid qilamanki ular siz uchun ishlaydi!

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"));
        }}
}
0
qo'shib qo'ydi
Bu mening savolimning dublikati. Iltimos, buni olib tashlang. Rahmat.
qo'shib qo'ydi muallif jzeferino, manba
  1. Try To Mark your method as Static in the first class like so:

    public static void Login_SuccessfullAuthentication_SuccessfullLogin() {...}

So'ngra siz chaqirishingiz mumkin:

LoginTests.Login_SuccessfullAuthentication_SuccessfullLogin();
  1. Or Create an object from the first class and call its method:

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

0
qo'shib qo'ydi
Bu mening savolimning dublikati. Iltimos, buni olib tashlang.
qo'shib qo'ydi muallif jzeferino, manba
Bu mening javobim emas edi. Mening javobimdan foydalanmoqdamisiz? kodni almashing.
qo'shib qo'ydi muallif jzeferino, manba
@jzeferino xatosi ko'rsatgan "Platform platforma - bu kontekstda haqiqiy emas"
qo'shib qo'ydi muallif ragesh-ragav 1993, manba
  1. Try To Mark your method as Static in the first class like so:

    public static void Login_SuccessfullAuthentication_SuccessfullLogin() {...}

So'ngra siz chaqirishingiz mumkin:

LoginTests.Login_SuccessfullAuthentication_SuccessfullLogin();
  1. Or Create an object from the first class and call its method:

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

0
qo'shib qo'ydi
Bu mening savolimning dublikati. Iltimos, buni olib tashlang.
qo'shib qo'ydi muallif jzeferino, manba
Bu mening javobim emas edi. Mening javobimdan foydalanmoqdamisiz? kodni almashing.
qo'shib qo'ydi muallif jzeferino, manba
@jzeferino xatosi ko'rsatgan "Platform platforma - bu kontekstda haqiqiy emas"
qo'shib qo'ydi muallif ragesh-ragav 1993, manba