gpt4 book ai didi

java - 如何创建一个传递测试脚本参数的 webdriver 函数?

转载 作者:行者123 更新时间:2023-11-28 20:06:22 27 4
gpt4 key购买 nike

我正在尝试为登录测试脚本创建一个函数,它传递用户名和密码等参数。我对此很陌生,请详细说明。这是我写的代码——

public void portalLogin(String Username, String Password){

driver.get(URL);
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys(Username);
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys(Password);
driver.findElement(By.cssSelector("input.submitBtn")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

我只是想将这个奇怪的函数调用给另一个测试类,这样我就不必一遍又一遍地写这个了。请帮忙!

最佳答案

您说您正在尝试使用页面对象模式。以下是我认为您正在寻找的内容:

public class LogonPage {
@FindBy(id="UserName")
public WebElement userNameField;
@FindBy(id = "Password")
public WebElement passwordField;
@FindBy(css = "input.submitBtn")
public WebElement submitButton;

public LogonPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
public void portalLogin(String username, String password){ //Only call this when you are already on the logon page
userNameField.clear();
userNameField.sendKeys(username);
passwordField.clear();
passwordField.sendKeys(password);
submitButton.click();
//Waiting should also be done after the function
}

然后您必须在主代码中执行此操作:

LogonPage page = new LogonPage(driver);
page.doLogin(username, password);

关于java - 如何创建一个传递测试脚本参数的 webdriver 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17791522/

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