gpt4 book ai didi

java - Selenium TestNG java - 参数太多

转载 作者:行者123 更新时间:2023-12-01 11:55:51 26 4
gpt4 key购买 nike

我正在使用Java语言中的TestNG做Selenium数据驱动框架

我有 pageObject Login_Page.java 来存储页面上所有可用的定位器。

然后我将 appModules Login_Action.java 作为重复登录流程的常用函数。

正如您所注意到的,ExecuteLoginAction() 中的步骤是相同的​​,但由于需要测试用例输入,因此具有不同数量的参数。在这种情况下如何优化代码?

在我的测试脚本中,我将调用 Login_Action.ExecuteLoginAction(many...parameters)

如何避免 MyTestScript_001Test()Login_Action.ExecuteLoginAction() 中出现这么长的参数列表

pageObjects Login_Page.java

package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Login_Page extends BaseClass {

private static WebElement element = null;

public Login_Page(WebDriver driver){
super(driver);
}

public static WebElement txt_enterUsername() throws Exception{
try{
element = driver.findElement(By.name("username"));
}catch (Exception e){
throw(e);
}
return element;
}

public static WebElement txt_enterPassword() throws Exception{
try{
element = driver.findElement(By.name("password"));
}catch (Exception e){
throw(e);
}
return element;
}

public static WebElement btn_clickLoginBtn() throws Exception{
try{
element = driver.findElement(By.name("loginBtn"));
}catch (Exception e){
throw(e);
}
return element;
}
}

appModules Login_Action.java

package appModules;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;

import pageObjects.Login_Page;

public class Login_Action {

public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress, String ColCountry, String ColGender) throws Exception{

// Click Login link
Home_Page.lnk_clickLoginBtn().click();

// Enter text for Username
Login_Page.txt_enterUsername().sendKeys(ColUsername);

// Enter text for Password
Login_Page.txt_enterPassword().sendKeys(ColPassword);

// Click Login submit button
Login_Page.btn_clickLoginSubmitBtn().click();

}

public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{

// Click Login link
Home_Page.lnk_clickLoginBtn().click();

// Enter text for Username
Login_Page.txt_enterUsername().sendKeys(ColUsername);

// Enter text for Password
Login_Page.txt_enterPassword().sendKeys(ColPassword);

// Click Login submit button
Login_Page.btn_clickLoginSubmitBtn().click();

}

}

主测试脚本 (MyTestScript_001)

@Test(dataProvider="MyTestScript_001Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{


// Login to web application
Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress, ColCountry, ColGender);

// Enter First Name
UpdateProfile_Page.txtbx_enterFirstName().sendKeys(ColFirstName);

// Enter Last Name
UpdateProfile_Page.txtbx_enterLastName().sendKeys(ColLastName);

Search_Action.ExecuteSearchAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress, ColCountry, ColGender);

主测试脚本 (MyTestScript_002)

Main Test Script (MyTestScript_002)

@Test(dataProvider="MyTestScript_002Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{


// Login to web application
Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress);

// Enter text for Address
UpdateProfile_Page.txtbx_enterAddress().sendKeys(ColAddress);

非常感谢您的建议!

最佳答案

发送这么多参数没有意义,我会发送一个对象:

public class UserInfo {
private String colTestCaseName;
private String coldUserName;
private String colPassword;
public String getColTestCaseName() {
return colTestCaseName;
}
public void setColTestCaseName(String colTestCaseName) {
this.colTestCaseName = colTestCaseName;
}
public String getColdUserName() {
return coldUserName;
}
public void setColdUserName(String coldUserName) {
this.coldUserName = coldUserName;
}
public String getColPassword() {
return ColPassword;
}
public void setColPassword(String colPassword) {
this.colPassword = colPassword;
}
}

调用方法:

UserInfo userInfo = new UserInfo();
//Add credentials
userInfo.setColUsername("user1");
....
Login_Action.ExecuteLoginAction(driver, userInfo);

关于java - Selenium TestNG java - 参数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449878/

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