基类:
protected WebDriver driver;
protected String URL = "https://www.example.com/";
public static String SignupURL = "https://www.example.com/login";
public Login loginpage;
@BeforeClass
public void setup()
{
System.setProperty("webdriver.chrome.driver","E:\\Selenium-Webdriver\\Chrome_Driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.navigate().to(URL);
loginpage = PageFactory.initElements(driver,Login.class);
}
登录类:
protected WebDriver driver;
public Login(WebDriver driver) {
this.driver = driver;}
public Login Method1()
{
//Logic
}
登录测试类:
public class LoginTest extends Base {
@Test
public void method1()
{
setup() //Have to Call it
//Logic
}
@Test
public void method2
{
setup() //Have to Call it
//Logic
}
}
问题是为什么需要为测试类中的每个方法调用setup()方法。
我已经扩展了类,然后驱动程序应该自动调用,但事实并非如此。当我不调用 setup() 时,会出现空指针异常,如果我调用它,则系统会为每个方法打开新的浏览器。
最佳答案
您在基类中使用@BeforeClass,如果将其更改为@BeforeSuite,则无需调用该设置方法。一般来说,我会在 mybase 类中使用 @BeforeSuite 并将其扩展到所有测试类以在该浏览器上工作。
下面一个对我有用
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
public class Base {
protected WebDriver driver;
protected String URL = "https://www.google.com/";
public static String SignupURL = "https://www.google.com/";
//public Login loginpage;
@BeforeSuite
public void setup()
{
// System.setProperty("webdriver.chrome.driver","E:\\Selenium-Webdriver\\Chrome_Driver\\chromedriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.navigate().to(URL);
// loginpage = PageFactory.initElements(driver,Login.class);
}
}
import org.testng.annotations.Test;
public class TestCasePage1 extends Base{
@Test
public void testit(){
System.out.println(driver.getTitle());
}
@Test
public void testit1(){
System.out.println(driver.getTitle());
}
}
谢谢你, 穆拉利
关于java - 页面对象模型未从类扩展驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37133570/