java - 页面对象模型未从类扩展驱动程序

标签 java selenium-webdriver testng pageobjects

基类:

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/

相关文章:

java - 调整 TableView 大小以适合 ScrollPane

java - 碧 Jade 报告 : filling a report throws an exception "Class not found when loading object from file"

c# - 比较两个列表 - 检查一个列表是否包含第二个值

java - TestNG ExpectedException 未找到抛出的异常

java - Spring 服务的可选嵌套注入(inject)

java - 我会让 jBoss 5.1 显示管理控制台

javascript - Selenium 网络驱动程序 : Can't access element containing onclick and text attributes

html - 如何获得具有相同属性的同一级别的元素或数据列表?

maven - 使用 testng 提供程序在surefire中禁用junit执行

java - TestNG - 如果满足条件,如何从 BeforeSuite 注释强制结束整个测试套件