对于页面对象模型,最好在testNG中的@Test注释之前使用哪个注释?

时间:2019-05-18 07:00:57

标签: selenium testng

我正在使用testNG为Page对象模型运行我的硒代码,@ BeforeTest将对每个测试仅运行一次,而@BeforeMethod将对每个测试每次运行。那么哪种注释更适合用于页面对象模型?

@BeforeMethod
public void setUp() throws IOException
{
  initialisation();
  loginPage = new Login();
  loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
  ot = new OpenTasks();
  active = new ActiveProjects();
}


@Test(priority=1)
  public void validateTitle() throws IOException, InterruptedException
  {
    String custTitle = ot.verifyTitle();
    Assert.assertEquals(custTitle, "actiTIME - Open Tasks", "Title not matched");
  }


@Test(priority=2)
  public void verifyProjectLink() throws IOException, InterruptedException
  {
   active = ot.clickOnProjectLink();
 }

 @AfterMethod
  public void tearDown()
  {
    driver.quit(); 
 }

(或)

public static WebDriver driver;
    @BeforeTest
    public void preConfig()
    {
        if(browser.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver","C:\\Program Files\\FireFox\\geckodriver-v0.24.0-win64\\geckodriver.exe");
            driver=new FirefoxDriver();
        }
        if(browser.equals("chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Chrome\\chromedriver_win32\\chromedriver.exe");
            driver=new ChromeDriver();
        }
         driver.get("http://desktop-g53h9ip:81/login.do");
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }
    @AfterTest
    public void postConfig()
    {
        driver.close();
    } 

2 个答案:

答案 0 :(得分:0)

最好为单个测试实例化一次浏览器,而第二种方法对于任何类型的框架都更好。

答案 1 :(得分:0)

TestNG注释的以下结构

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite