要在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";
}
以下内容拒绝工作,因为驾驶员没有通过@BeforeMethod到@Test。
public class TestClass {
@BeforeMethod
public void home() {
WebDriver driver = new FirefoxDriver();
}
@Test
public void Setup() {
driver.get("http://www.indeed.co.uk");`
}
}
@BeforeMethod中的驱动程序没有红色的曲线。@test中的一个执行
是的,我们需要全局或类级别声明驱动程序对象。像低于
public class TestClass {
public WebDriver driver;
//...... }
如果我们在这里声明任何方法中的驱动程序对象都可以是home(),那么它就变成了home的本地对象,并且在类中的所有方法中都不具有可见性或可访问性。请参考这里的变量
//**更新URL 上的答案
在下面的@Test中,我们直接提供URL。因此需要公共字符串baseUrl变量
@Test
public void Setup() {
driver.get("http://www.indeed.co.uk");`
}
如果你想创建变量并愿意在@Test中使用该变量,那么将baseUrl创建为全局,你可以直接赋值,比如下面的
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);`
}
此外,我们可以创建baseUrl作为全局,并可以在BeforeTest中分配值,如下面的
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);`
}
}
谢谢,Murali