在决定将项目转换为这种设计模式之后,我还创建了4个类。我将相关类中的代码移到了方法中。编译时我遇到了失败,我也不知道为什么。

主班

GidiyorTest.java

public class GidiyorTest {
  protected WebDriver driver;
  protected String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  static GidiyorTest gittiGidiyor = new GidiyorTest();
  static String generatedMail = gittiGidiyor.generateString();
  static String generatedUsername = gittiGidiyor.generateString();

  static RegisterPage  registerPage = new RegisterPage();
  static LoginPage loginPage = new LoginPage();
  static SearchPage searchPage = new SearchPage();
  static DiscountsPage discountsPage = new DiscountsPage();


  public String generateString(){
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 7; i++) {
            char c = chars[random.nextInt(chars.length)];
            sb.append(c);
        }
        String output = sb.toString();
        return output;
    }


  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.gittigidiyor.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  @Test
  public void testGidiyor() throws Exception {
    registerPage.Register();
    loginPage.Login();
    searchPage.Search();
    discountsPage.Discount();
}
@After
....


RegisterPage.java(四个新类之一,仅共享一个)

public class RegisterPage extends GidiyorTest {

    public void Register() throws InterruptedException {

        driver.get(baseUrl + "/kayit-ol");
        driver.findElement(By.name("name")).clear();
        driver.findElement(By.name("name")).sendKeys("murat");
        driver.findElement(By.name("surname")).clear();
        driver.findElement(By.name("surname")).sendKeys("yilmaz");
        Thread.sleep(300);
        driver.findElement(By.id("suggestion_email_input_verifier")).clear();
        driver.findElement(By.id("suggestion_email_input_verifier")).sendKeys(
                generatedMail + "@gmail.com");
        driver.findElement(By.id("nickname")).clear();
        driver.findElement(By.id("nickname")).sendKeys(generatedUsername);
        Thread.sleep(300);
        driver.findElement(By.name("passwd")).clear();
        driver.findElement(By.name("passwd")).sendKeys("123456abc");
        driver.findElement(By.name("passwd2")).clear();
        driver.findElement(By.name("passwd2")).sendKeys("123456abc");
        Thread.sleep(300);
        driver.findElement(By.id("cepgsm")).clear();
        driver.findElement(By.id("cepgsm")).sendKeys("531");
        driver.findElement(By.id("cep")).clear();
        driver.findElement(By.id("cep")).sendKeys("600 29 79");
        Thread.sleep(1000);
        driver.findElement(By.id("SubmitForm")).click();

    }
}


并且错误始于registerPage.Register();。线。另一个是java.lang.NullPointerException。

希望能对您有所帮助。

最佳答案

您创建PageObject的方式不正确。您不应该扩展测试,要点之一是PageObject应该不了解有关测试的任何信息,而应该只知道expose the services offered by the page
另一方面,您的测试应保留断言和其他与测试相关的逻辑。

第二件错误的事情是您应该使用PageFactory实例化页面对象,以便可以利用延迟绑定机制。所以换成这样

public class RegisterPage {

    private WebDriver driver;

    public RegisterPage(WebDriver driver) {
        this.driver = driver;
    }

   // The rest of your class
 }


并使用PageFactory在测试内部实例化

PageFactory.initElements(driver, RegisterPage.class);


此外,为了简化维护并从惰性元素绑定中受益,您可以考虑将元素添加为字段,并通过注释对其进行标记,这样它们也可以由PageFactory填充。

public class RegisterPage {

    private WebDriver driver;

    public RegisterPage(WebDriver driver) {
        this.driver = driver;
    }

    @FindBy(name = "name")
    private WebElement name;

    ...


    }
}

09-04 09:28