【问题标题】:Which annotation is preferable to use before @Test annotation in testNG for Page Object Model?在用于页面对象模型的 testNG 中的 @Test 注释之前,最好使用哪个注释?
【发布时间】:2019-05-23 16:15:54
【问题描述】:

我正在使用 testNG 为 Page 对象模型运行我的 selenium 代码,@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();
    } 

【问题讨论】:

    标签: selenium testng


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      TestNG 注释的以下结构

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多