我如何使用BeforeMethod注释中定义的变量和方法

时间:2015-10-31 12:59:52

标签: selenium testing selenium-webdriver automation testng

我已经定义了要打开的webdriver(firefox / chrome / IE),并且该方法是我在BeforeMethod注释中创建的驱动程序(),它正在创建驱动对象。现在我想在我的afterMethod和测试注释中使用相同的WebDriver对象。我怎样才能实现这一目标?

public WebDriver driver;   
@Test
 public void f() throws IOException {
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.navigate().refresh();
  LoginPage.link_Guest(driver).click();
    try{
        Assert.assertTrue(driver.findElement(By.xpath(".//* [@id='main-menu-    ist']")).isDisplayed());
    }catch (Exception e){
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("C:\\User\\src\\ScreenShots\\TC_GS_01_GuestHomePage.png"));
    }
}
@BeforeMethod
public void beforeMethod() throws IOException {
    DOMConfigurator.configure("log4j.xml");

    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("C:\\Users\\src\\constants\\dataFile.properties");
    prop.load(fis);


    Reporter.log("Browser has been initiated");
    WebDriver driver = Constants.driver();
    driver.get(prop.getProperty("testUrl"));
  }

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

2 个答案:

答案 0 :(得分:0)

 class Test {

  private WebDriver driver;

  @Test
  public void f() throws IOException {
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.navigate().refresh();
    LoginPage.link_Guest(driver).click();
    try{
        Assert.assertTrue(driver.findElement(By.xpath(".//* [@id='main-menu-    ist']")).isDisplayed());
    }catch (Exception e){
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("C:\\User\\src\\ScreenShots\\TC_GS_01_GuestHomePage.png"));
    }
  }

  @BeforeMethod
  public void beforeMethod() throws IOException {
    DOMConfigurator.configure("log4j.xml");

    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("C:\\Users\\src\\constants\\dataFile.properties");
    prop.load(fis);

    Reporter.log("Browser has been initiated");
    driver = Constants.driver();
    driver.get(prop.getProperty("testUrl"));
  }

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

答案 1 :(得分:0)

     class Test {

  public static WebDriver driver =null;

  @Test
  public void f() throws IOException {
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.navigate().refresh();
    LoginPage.link_Guest(driver).click();
    try{
        Assert.assertTrue(driver.findElement(By.xpath(".//* [@id='main-menu-    ist']")).isDisplayed());
    }catch (Exception e){
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("C:\\User\\src\\ScreenShots\\TC_GS_01_GuestHomePage.png"));
    }
  }

  @BeforeMethod
  public void beforeMethod() throws IOException {
    DOMConfigurator.configure("log4j.xml");

    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("C:\\Users\\src\\constants\\dataFile.properties");
    prop.load(fis);

    Reporter.log("Browser has been initiated");
    driver = Constants.driver();
    driver.get(prop.getProperty("testUrl"));
  }

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