Selenium 如何使用BeforeMethod注释中定义的变量和方法
我已经定义了要打开的webdriver(firefox/chrome/IE),该方法是driver(),我在BeforeMethod注释中使用了该方法,该方法正在创建驱动对象。现在我想在afterMethod和test注释中使用相同的WebDriver对象。我怎样才能做到这一点Selenium 如何使用BeforeMethod注释中定义的变量和方法,selenium,testing,selenium-webdriver,automation,testng,Selenium,Testing,Selenium Webdriver,Automation,Testng,我已经定义了要打开的webdriver(firefox/chrome/IE),该方法是driver(),我在BeforeMethod注释中使用了该方法,该方法正在创建驱动对象。现在我想在afterMethod和test注释中使用相同的WebDriver对象。我怎样才能做到这一点 public WebDriver driver; @Test public void f() throws IOException { driver.manage().timeouts().implicitl
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();
}
我试过
公共WebDriver代码>但是它没有任何区别。问题是WebDriver=Constants.driver()
在beforeMethod()
中,我将其替换为driver=Constants.driver()代码>。
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();
}
}
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();
}
}