2015-10-31 30 views
0

定義的變量和methos我已經定義要打開的的webdriver(火狐/鉻/ IE),且方法是驅動器()這是我在BeforeMethod註釋,其被創建驅動對象。現在我想在我的afterMethod和測試註釋中使用相同的WebDriver對象。我怎樣才能做到這一點?如何使用在BeforeMethod註釋

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

回答

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

我與'公共webdriver的司機試圖;'但它不作任何區別。 –

+0

問題是'beforeMethod()'中的'WebDriver driver = Constants.driver();',我用'driver = Constants.driver();'取代了。 – juherr

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