Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何使用BeforeMethod注释中定义的变量和方法_Selenium_Testing_Selenium Webdriver_Automation_Testng - Fatal编程技术网

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

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

我已经定义了要打开的webdriver(firefox/chrome/IE),该方法是driver(),我在BeforeMethod注释中使用了该方法,该方法正在创建驱动对象。现在我想在afterMethod和test注释中使用相同的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();
 }

我试过
公共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();
  }
}