samedi 31 octobre 2015

How can i use variables and methos defined in BeforeMethod annotation

i have defined which webdriver to open (firefox/chrome/IE) and that method is driver() which i have in BeforeMethod annotation, which is creating drive object . Now i want to use same WebDriver object in my afterMethod and test annotation. How can i achieve this ?

@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();
 }

Aucun commentaire:

Enregistrer un commentaire