Как аннотация TestNg, упомянутая в одном классе, выполнялась из другого класса? - PullRequest
0 голосов
/ 08 мая 2018

Во время изучения Testng на Udemy я наткнулся на код, который я не могу понять. Инструктор создал класс с именем «testcore», в котором он определил @ BeforeMethod / @ aftermethod. Позднее он создал другой класс с именем «LoginTest», в котором он написал реальный тест с @test. Он расширил класс testcore в loginTest, чтобы инициировать переменную в классе testcore. Когда он запустил loginTest, тогда @ BeforeMethod / @ aftermethod также запускался с этим. Как эти два метода работали вместе с @test, когда эти методы находятся в другом классе. вот оба кода:

    public class testcore {

    public  static Properties config = new Properties();
    public  static Properties obj = new Properties();
    public  static Xls_Reader  xls = null;
    public static WebDriver driver;//=null;

    @BeforeMethod
    public void init()  throws Exception{

        if(driver==null) {

        // Loading Config Properties File
        File Config_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\config.properties");
        FileInputStream fs = new FileInputStream(Config_f);
        config.load(fs);

        // Loading Object Properties File
        File Obj_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\Object.properties");
        fs = new FileInputStream(Obj_f);
        obj.load(fs);

        //Loading xlsx file
        xls = new Xls_Reader(System.getProperty("user.dir")+"\\src\\dd_Properties\\Data.xlsx");

        System.out.println(config.getProperty("browerName"));

        if(config.getProperty("browerName").equalsIgnoreCase("Firefox")) {
            driver = new FirefoxDriver();   
        }
        else if(config.getProperty("browerName").equalsIgnoreCase("Chrome")) {
            driver = new ChromeDriver();
        }
        else {
            throw new Exception("Wrong/No Browswer sepcified");
        }

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }


}
    @AfterMethod
    public void quitdriver() throws EmailException {
        driver.quit();

        //monitoringMail.Sendmail();
    }

Вот класс LoginTest:

public class LoginTest extends testcore {


@Test
public void doLogin() {

    driver.findElement(By.xpath(obj.getProperty("LoginBtn"))).click();
    driver.findElement(By.xpath(obj.getProperty("username"))).sendKeys();

    driver.findElement(By.xpath(obj.getProperty("password"))).sendKeys();


}

1 Ответ

0 голосов
/ 08 мая 2018

1) Как эти два метода работали вместе с @ test.

Прежде всего LoginTest расширяет testcore .

Благодаря этому мы можем унаследовать метод testcore в нашем классе LoginTest.

2) Когда он запустил loginTest, тогда @ BeforeMethod / @ aftermethod также работал с этим.

Пожалуйста, обратитесьподробности ниже

Информация о конфигурации для класса TestNG:

@BeforeSuite: The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...