Java 要在TestNG中使用@BeforeMethod,必须使用公共WebDriver驱动程序;公共字符串baseUrl;首先被创造?

Java 要在TestNG中使用@BeforeMethod,必须使用公共WebDriver驱动程序;公共字符串baseUrl;首先被创造?,java,eclipse,testng,Java,Eclipse,Testng,要在TestNG中使用@BeforeMethod,必须首先创建它吗 public WebDriver driver; public String baseUrl; 这起到了作用: public class TestClass { public WebDriver driver; public String baseUrl; @BeforeMethod public void Homepage() { driver = new FirefoxDriver();

要在TestNG中使用@BeforeMethod,必须首先创建它吗

public WebDriver driver;    
public String baseUrl; 
这起到了作用:

public class TestClass {
  public WebDriver driver;
  public String baseUrl;

  @BeforeMethod
  public void Homepage() {
  driver = new FirefoxDriver();
  baseUrl = "http://www.indeed.co.uk";
}
由于驱动程序没有从@beforethod通过@Test,下面的拒绝工作

public class TestClass {
  @BeforeMethod
  public void home() {
    WebDriver driver = new FirefoxDriver();
  }

  @Test
  public void Setup() {
    driver.get("http://www.indeed.co.uk");`
  }
}

@before方法中的驱动程序没有红色的曲线。@test中的一个不

是的,我们需要全局或类级别声明驱动程序对象。如下

 public class TestClass {

 public WebDriver driver; 

 //...... }
public class TestClass {

public String baseUrl="http://www.indeed.co.uk";

//use this baseUrl in @test like below
@Test
public void Setup() {

driver.get(baseUrl);`
} 
public class TestClass {

public WebDriver driver; public String baseUrl;

@BeforeMethod
public void Homepage() {

driver = new FirefoxDriver();
baseUrl = "http://www.indeed.co.uk";

}

// as we declared baseUrl globally and assigned value in beforetest, so we use this variable in @tests now

 @Test
public void Setup() {

driver.get(baseUrl);`
} 
}
若我们在这里声明任何方法中的驱动程序对象都可能是home(),那个么它将仅成为home的本地对象,并且在类中的所有方法中都不具有可见性或可访问性。有关变量,请参阅

//**更新URL上的答案

在下面的@Test中,我们直接提供URL。所以需要公共字符串baseUrl变量

 @Test
 public void Setup() {

driver.get("http://www.indeed.co.uk");`
}
如果您想创建变量并愿意在@Test中使用该变量,那么可以将baseUrl创建为全局变量,您可以直接赋值,如下所示

 public class TestClass {

 public WebDriver driver; 

 //...... }
public class TestClass {

public String baseUrl="http://www.indeed.co.uk";

//use this baseUrl in @test like below
@Test
public void Setup() {

driver.get(baseUrl);`
} 
public class TestClass {

public WebDriver driver; public String baseUrl;

@BeforeMethod
public void Homepage() {

driver = new FirefoxDriver();
baseUrl = "http://www.indeed.co.uk";

}

// as we declared baseUrl globally and assigned value in beforetest, so we use this variable in @tests now

 @Test
public void Setup() {

driver.get(baseUrl);`
} 
}
此外,我们还可以全局创建baseUrl,并可以在BeforeTest中赋值,如下所示

 public class TestClass {

 public WebDriver driver; 

 //...... }
public class TestClass {

public String baseUrl="http://www.indeed.co.uk";

//use this baseUrl in @test like below
@Test
public void Setup() {

driver.get(baseUrl);`
} 
public class TestClass {

public WebDriver driver; public String baseUrl;

@BeforeMethod
public void Homepage() {

driver = new FirefoxDriver();
baseUrl = "http://www.indeed.co.uk";

}

// as we declared baseUrl globally and assigned value in beforetest, so we use this variable in @tests now

 @Test
public void Setup() {

driver.get(baseUrl);`
} 
}
谢谢,,
穆拉里

这是有道理的,穆拉里;然而,还必须在类级别声明的公共字符串baseUrl是什么?URL也是如此,若您想在类中的所有方法中使用baseUrl,那个么就声明为全局变量。如果您愿意在get like this driver.get(“);”`中直接传递URL,那么也不需要创建baseURL变量。我希望您能得到它。我们的消息交叉了。为了清楚起见,您说可以在@Test with driver.get(“”)中直接将URL作为本地对象传递,而无需在类级别创建baseURL变量?请参阅