gpt4 book ai didi

java - 将 Selenium Java 项目转换为页面对象设计模式时出错

转载 作者:太空宇宙 更新时间:2023-11-04 06:16:00 24 4
gpt4 key购买 nike

在我决定将我的项目转换为这种设计模式之后,我还创建了 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来实例化你的页面对象,这样你就可以利用惰性绑定(bind)机制。所以改成这样

public class RegisterPage {

private WebDriver driver;

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

// The rest of your class
}

并使用PageFactory在测试中实例化

PageFactory.initElements(driver, RegisterPage.class);

此外,为了简化维护并从惰性元素绑定(bind)中受益,您可以考虑将元素添加为字段,并通过注释标记它们,以便它们也由 PageFactory 填充,例如

public class RegisterPage {

private WebDriver driver;

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

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

...


}
}

关于java - 将 Selenium Java 项目转换为页面对象设计模式时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com